首页 > 解决方案 > xslt 转换错误:XTSE0010:元素 xsl:mode 不得直接出现在 xsl:stylesheet 中

问题描述

我正在寻找将节点从一个值更改为另一个值。不是节点的值,而是节点的名称。不是标签内的内容。

维基百科会说“标签”为:

标签 标签是一个以 < 开头并以 > 结尾的标记结构。标签有三种风格:

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

因此,我希望将一个名称的所有上述标签重命名为另一个名称。至于或等foo_ _barbarbaz

运行saxonb-xslt返回:

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且没有错误。

标签: xmlxsltxpathxml-parsingsaxon

解决方案


xsl:mode需要 XSLT 3.0。AFAIK,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>

推荐阅读