带有累加器的 XSLT 3.0 样式表编译错误

XSLT 3.0 stylesheet compilation error with accumulator

我正在尝试转换我的 XML,以便我可以在 sap 集成过程中轻松地将其转换为 JSON。 我收到样式表编译错误,我无法弄清楚原因。 这是我的 XML:

<groups>
        <UserGroup>
                <integrationKey>xxa</integrationKey>
                <uid>001</uid>]
        </UserGroup>
        <UserGroup>
                <integrationKey>xxb</integrationKey>
                <uid>002</uid>
        </UserGroup>
</groups>

这是我的 XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:accumulator name="UserGroupCounter" as="xs:integer" initial-value="0">
        <xsl:accumulator-rule match="//groups" select="0"/>
        <xsl:accumulator-rule match="//groups/UserGroup" select="$value + 1"/>
    </xsl:accumulator>
  
    <xsl:template match="UserGroup">
        <xsl:copy>
            <xsl:attribute name="userGroupIndex" select="accumulator-before('UserGroupCounter')"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:mode on-no-match="shallow-copy" use-accumulators="UserGroupCounter"/>
</xsl:stylesheet>

即使我只尝试声明累加器,也会出现错误。这是错误文本:

[CAMEL][IFLOW][EXCEPTION] : org.apache.camel.FailedToCreateRouteException: Failed to create route Process_817351: Route(Process_817351)[[From[direct:Process_817351]] -> [To[o... because of net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
  [CAMEL][IFLOW][CAUSE] : Cause: javax.xml.transform.TransformerConfigurationException: net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
    [CAMEL][IFLOW][CAUSE] : Cause: net.sf.saxon.s9api.SaxonApiException: Errors were reported during stylesheet compilation
      [CAMEL][IFLOW][CAUSE] : Cause: net.sf.saxon.trans.XPathException: Errors were reported during stylesheet compilation

我尝试将模式更改为 //groups/UserGroup 等,但似乎没有任何效果。我试过将 XSLT 限制为:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:accumulator name="UserGroupCounter" as="xs:integer" initial-value="0">
    </xsl:accumulator>

仍然出错。 我该怎么办?

您需要为用于整数类型声明的 xs 前缀声明模式名称空间:

xmlns:xs="http://www.w3.org/2001/XMLSchema"

添加到 xsl:accumulator 元素或添加到 xsl:stylesheet 元素之上。