如何修复 XSLT 中的 'Errors were reported during stylesheet compilation'?
How to fix 'Errors were reported during stylesheet compilation' in XSLT?
当我在 https://xslttest.appspot.com/ 上 运行 我的 XSLT 代码时,我有这个 SaxonApiException。它 return 这个错误:
net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
我尝试了另一个在线测试器 https://www.freeformatter.com/xsl-transformer.html,但我遇到了同样的错误。
我试图拆分我的 XSLT 代码。第一部分是从 Wages 中提取 ZipCode 的过程,第二部分是从 Addresses 中提取 ZipCode 的过程。
当它们分开时两者都有效,所以我想我在 'choose' 元素中犯了一个错误但找不到它。
这是我的 XSLT 代码...
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/EmployeeUDM_Response/Return/Employee">
<xsl:for-each select="./Wages/Wage">
<xsl:choose>
<xsl:when test="DissimelarZipCode != ''">
<xsl:value-of select="DissimelarZipCode" />
</xsl:when>
<otherwise>
<xsl:for-each select="./Addresses/Address" />
<!-- year -->
<xsl:sort select="substring(StartDate, 1, 4)" order="descending" data-type="number"/>
<!-- month -->
<xsl:sort select="substring(StartDate, 6, 2)" order="descending" data-type="number"/>
<!-- day -->
<xsl:sort select="substring(StartDate, 9, 2)" order="descending" data-type="number"/>
<xsl:if test="position() = 1">
<xsl:value-of select="./ZipCode" />
</xsl:if>
</otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
...和我的 XML 文件
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"?>
<EmployeeUDM_Response xmlns:ns0="http://ESB/Schemas/v2/EmployeeUDM">
<Header Type="Employee" Source="Biztalk ESB" />
<Return>
<Employee>
<Wages>
<Wage>
<StartDate>2019-04-22T00:00:00.0000000+02:00</StartDate>
<EndDate>2019-05-01T00:00:00.0000000+02:00</EndDate>
<DissimelarZipCode>5430 NU</DissimelarZipCode>
</Wage>
</Wages>
<Addresses>
<Address>
<StartDate>2014-01-01T00:00:00.0000000+02:00</StartDate>
<EndDate></EndDate>
<ZipCode>6099 EB</ZipCode>
</Address>
<Address>
<StartDate>2015-01-01T00:00:00.0000000+02:00</StartDate>
<EndDate></EndDate>
<ZipCode>5487 YR</ZipCode>
</Address>
</Addresses>
</Employee>
</Return>
</EmployeeUDM_Response>
我期望 Wage 中 ZipCode 的输出(在本例中为 5430 NU),或者如果 Wage 中 ZipCode 为空,地址中 ZipCode 的最新开始日期(在本例中为 5487 YR)
1.应该是<xsl:otherwise>
而不是<otherwise>
2. <xsl:sort>
应该在 <xsl:for-each>
中。(你已经在同一行结束了循环)
3. 要遍历 Address
,您需要 xpath ../../Addresses/Address
。因为当时正在处理<Wage>
。 (../
会将您带到父节点的一级。)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/EmployeeUDM_Response/Return/Employee">
<xsl:for-each select="Wages/Wage">
<xsl:choose>
<xsl:when test="DissimelarZipCode != ''">
<xsl:value-of select="DissimelarZipCode" />
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="../../Addresses/Address">
<!-- year -->
<xsl:sort select="substring(StartDate, 1, 4)" order="descending"
data-type="number" />
<!-- month -->
<xsl:sort select="substring(StartDate, 6, 2)" order="descending"
data-type="number" />
<!-- day -->
<xsl:sort select="substring(StartDate, 9, 2)" order="descending"
data-type="number" />
<xsl:if test="position() = 1">
<xsl:value-of select="ZipCode" />
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
当我在 https://xslttest.appspot.com/ 上 运行 我的 XSLT 代码时,我有这个 SaxonApiException。它 return 这个错误:
net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
我尝试了另一个在线测试器 https://www.freeformatter.com/xsl-transformer.html,但我遇到了同样的错误。 我试图拆分我的 XSLT 代码。第一部分是从 Wages 中提取 ZipCode 的过程,第二部分是从 Addresses 中提取 ZipCode 的过程。 当它们分开时两者都有效,所以我想我在 'choose' 元素中犯了一个错误但找不到它。
这是我的 XSLT 代码...
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/EmployeeUDM_Response/Return/Employee">
<xsl:for-each select="./Wages/Wage">
<xsl:choose>
<xsl:when test="DissimelarZipCode != ''">
<xsl:value-of select="DissimelarZipCode" />
</xsl:when>
<otherwise>
<xsl:for-each select="./Addresses/Address" />
<!-- year -->
<xsl:sort select="substring(StartDate, 1, 4)" order="descending" data-type="number"/>
<!-- month -->
<xsl:sort select="substring(StartDate, 6, 2)" order="descending" data-type="number"/>
<!-- day -->
<xsl:sort select="substring(StartDate, 9, 2)" order="descending" data-type="number"/>
<xsl:if test="position() = 1">
<xsl:value-of select="./ZipCode" />
</xsl:if>
</otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
...和我的 XML 文件
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"?>
<EmployeeUDM_Response xmlns:ns0="http://ESB/Schemas/v2/EmployeeUDM">
<Header Type="Employee" Source="Biztalk ESB" />
<Return>
<Employee>
<Wages>
<Wage>
<StartDate>2019-04-22T00:00:00.0000000+02:00</StartDate>
<EndDate>2019-05-01T00:00:00.0000000+02:00</EndDate>
<DissimelarZipCode>5430 NU</DissimelarZipCode>
</Wage>
</Wages>
<Addresses>
<Address>
<StartDate>2014-01-01T00:00:00.0000000+02:00</StartDate>
<EndDate></EndDate>
<ZipCode>6099 EB</ZipCode>
</Address>
<Address>
<StartDate>2015-01-01T00:00:00.0000000+02:00</StartDate>
<EndDate></EndDate>
<ZipCode>5487 YR</ZipCode>
</Address>
</Addresses>
</Employee>
</Return>
</EmployeeUDM_Response>
我期望 Wage 中 ZipCode 的输出(在本例中为 5430 NU),或者如果 Wage 中 ZipCode 为空,地址中 ZipCode 的最新开始日期(在本例中为 5487 YR)
1.应该是<xsl:otherwise>
而不是<otherwise>
2. <xsl:sort>
应该在 <xsl:for-each>
中。(你已经在同一行结束了循环)
3. 要遍历 Address
,您需要 xpath ../../Addresses/Address
。因为当时正在处理<Wage>
。 (../
会将您带到父节点的一级。)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/EmployeeUDM_Response/Return/Employee">
<xsl:for-each select="Wages/Wage">
<xsl:choose>
<xsl:when test="DissimelarZipCode != ''">
<xsl:value-of select="DissimelarZipCode" />
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="../../Addresses/Address">
<!-- year -->
<xsl:sort select="substring(StartDate, 1, 4)" order="descending"
data-type="number" />
<!-- month -->
<xsl:sort select="substring(StartDate, 6, 2)" order="descending"
data-type="number" />
<!-- day -->
<xsl:sort select="substring(StartDate, 9, 2)" order="descending"
data-type="number" />
<xsl:if test="position() = 1">
<xsl:value-of select="ZipCode" />
</xsl:if>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>