xslt transform error: XTSE0010: Element xsl:mode must not appear directly within xsl:stylesheet

xslt transform error: XTSE0010: Element xsl:mode must not appear directly within xsl:stylesheet

我希望将节点从一个值更改为另一个值。不是节点的值,而是节点的名称。不是标签里面的内容。

维基百科会将 "tags" 表示为:

Tag A tag is a markup construct that begins with < and ends with >. Tags come in three flavors:

    start-tag, such as <section>;
    end-tag, such as </section>;
    empty-element tag, such as <line-break />.

所以我希望将一个名称的所有上述标签重命名为另一个名称。如foobarbarbaz

运行 saxonb-xslt returns:

Saxon 9.1.0.8J from Saxonica

也许这个版本的 Saxon 没有功能,或者更可能的是 xslt 有缺陷。

从较大的文件截断 xml

<csv>
  <foo>
    <entry>Reported_Date</entry>
    <entry>HA</entry>
    <entry>Sex</entry>
    <entry>Age_Group</entry>
    <entry>Classification_Reported</entry>
  </foo>
  <bar>
    <entry>2020-01-26</entry>
    <entry>Vancouver Coastal</entry>
    <entry>M</entry>
    <entry>40-49</entry>
    <entry>Lab-diagnosed</entry>
  </bar>
  <record>
    <baz>2020-02-02</baz>
    <entry>Vancouver Coastal</entry>
    <entry>baz</entry>
    <entry>50-59</entry>
    <entry>Lab-diagnosed</entry>
  </record>
  <record>
    <entry>2020-02-05</entry>
    <entry>Vancouver Coastal</entry>
    <entry>F</entry>
    <entry>20-29</entry>
    <entry>Lab-diagnosed</entry>
  </record>
</csv>

xslt 文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes"/>

  <xsl:mode on-no-match="shallow-copy"/>
  <xsl:template match="foo">
    <baz><xsl:apply-templates/></baz>
  </xsl:template>
</xsl:stylesheet>

错误:

Error at xsl:mode on line 9 column 41 of bc.rename.xslt:
  XTSE0010: Element xsl:mode must not appear directly within xsl:stylesheet
Error at xsl:mode on line 9 column 41 of bc.rename.xslt:
  XTSE0010: Unknown XSLT element: mode
Failed to compile stylesheet. 2 errors detected.

xml 文档和 xslt 文档均通过 xmllint 且没有错误。

xsl:mode 需要 XSLT 3.0。据我所知,Saxon 9.1 仅支持 XSLT 2.0。

试试看:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="foo">
    <baz>
        <xsl:apply-templates/>
    </baz>
</xsl:template>

</xsl:stylesheet>