当我在 https://xslttest.appspot.com/ 上运行 XSLT 代码时,出现此 SaxonApiException 。它返回此错误:
net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
我尝试了另一个在线测试仪 https://www.freeformatter.com/xsl-transformer.html但我得到了同样的错误。 我尝试拆分我的 XSLT 代码。第一部分是在工资中提取邮政编码的过程,第二部分是在地址中提取邮政编码的过程。 当它们分开时,两者都可以工作,所以我认为我在“选择”元素中犯了一个错误,但找不到它。
这是我的 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 为空,则输出具有最新 StartDate 的 Address 中的 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>
关于xslt-1.0 - 如何修复 XSLT 中的 'Errors were reported during stylesheet compilation'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56150005/