【问题标题】:How to fix 'Errors were reported during stylesheet compilation' in XSLT?如何修复 XSLT 中的“样式表编译期间报告错误”?
【发布时间】:2019-10-02 15:17:13
【问题描述】:

当我在https://xslttest.appspot.com/ 上运行我的 XSLT 代码时,我遇到了这个 SaxonApiException。它返回此错误:

net.sf.saxon.s9api.SaxonApiException: 样式表编译时报错

我尝试了另一个在线测试器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>

我希望输出工资中的邮政编码(本例中为 5430 NU),或者,如果工资中的邮政编码为空,则输出地址中的邮政编码,开始日期最晚(本例中为 5487 年)

【问题讨论】:

    标签: xslt-1.0 biztalk biztalk-2013


    【解决方案1】:

    1.应该有&lt;xsl:otherwise&gt;而不是&lt;otherwise&gt;

    2. &lt;xsl:sort&gt; 应该在 &lt;xsl:for-each&gt; 中。(你已经在同一行结束了循环)

    3.要遍历Address,您需要xpath ../../Addresses/Address。因为当时正在处理&lt;Wage&gt;。 (../ 会将您带到父节点的上一级。)

    <?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://xsltfiddle.liberty-development.net/pPzifpP

    【讨论】:

    • 虽然您知道如何修复代码,但问题也可能是如何找到自己的错误列表。
    猜你喜欢
    • 2020-06-21
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多