【问题标题】:Annotating an xml instance from a list of xpath statements with xslt使用 xslt 从 xpath 语句列表中注释 xml 实例
【发布时间】:2013-10-04 22:31:49
【问题描述】:

给定一个 xpath 语句列表,我想编写一个样式表,它将贯穿一个 xml 文档并输出相同的文档,但在每个 xpath 语句中标识的节点之前插入一个注释。让我们举个例子。从一个包含 xpath 语句的 xml 实例开始:

<paths>
  <xpath location="/root/a" annotate="1"/>
  <xpath location="/root/a/b" annotate="2"/>
</paths>

给定输入:

<root>
  <a>
    <b>B</b>
  </a>
  <c>C</c>
</root>

它应该产生:

<root>
  <!-- 1 -->
  <a>
    <!-- 2 -->
    <b>B</b>
  </a>
  <c>C</c>
</root>

我最初的想法是拥有一个带有file-list 参数的身份样式表,在其上调用document 函数以获取xpath 节点列表。然后它会根据该列表检查输入的每个节点,然后在找到一个时插入注释节点,但我希望随着 xpaths 列表变大(或者可能不是,告诉我。我正在使用撒克逊 9)。

所以我的问题是:有没有一种有效的方法来做这样的事情?

【问题讨论】:

    标签: xslt xpath xslt-2.0


    【解决方案1】:

    假设 Saxon 9 PE 或 EE,也应该可以使用 XSLT 3.0 和 xsl:evaluate,如下所示:

    <?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"
        xmlns:math="http://www.w3.org/2005/xpath-functions/math"
        xmlns:map="http://www.w3.org/2005/xpath-functions/map"
        xmlns:mf="http://example.com/mf"
        exclude-result-prefixes="xs math map mf"
        version="3.0">
    
        <xsl:output indent="yes"/>
    
        <xsl:param name="paths-url" as="xs:string" select="'paths1.xml'"/>
        <xsl:param name="paths-doc" as="document-node()" select="doc($paths-url)"/>
    
        <xsl:variable name="main-root" select="/"/>
    
        <xsl:variable 
            name="mapped-nodes">
            <map>
                <xsl:for-each select="$paths-doc/paths/xpath">
                    <xsl:variable name="node" as="node()?" select="mf:evaluate(@location, $main-root)"/>
                    <xsl:if test="$node">
                        <entry key="{generate-id($node)}">
                            <xsl:value-of select="@annotate"/>
                        </entry>
                    </xsl:if>
                </xsl:for-each>
            </map>
        </xsl:variable>
    
        <xsl:key name="node-by-id" match="map/entry" use="@key"/>
    
        <xsl:function name="mf:evaluate" as="node()?">
            <xsl:param name="path" as="xs:string"/>
            <xsl:param name="context" as="node()"/>
            <xsl:evaluate xpath="$path" context-item="$context"></xsl:evaluate>
        </xsl:function>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* , node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="node()[key('node-by-id', generate-id(), $mapped-nodes)]">
            <xsl:comment select="key('node-by-id', generate-id(), $mapped-nodes)"/>
            <xsl:text>&#10;</xsl:text>
            <xsl:copy>
                <xsl:apply-templates select="@* , node()"/>
            </xsl:copy>
        </xsl:template>
    
    
    </xsl:stylesheet>
    

    这是最初发布的代码的编辑版本,它使用 XSLT 3.0 地图功能而不是临时文档来存储通过动态 XPath 评估找到的节点的生成 id 与注释之间的关联:

    <?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"
        xmlns:math="http://www.w3.org/2005/xpath-functions/math"
        xmlns:map="http://www.w3.org/2005/xpath-functions/map"
        xmlns:mf="http://example.com/mf"
        exclude-result-prefixes="xs math map mf"
        version="3.0">
    
        <xsl:param name="paths-url" as="xs:string" select="'paths1.xml'"/>
        <xsl:param name="paths-doc" as="document-node()" select="doc($paths-url)"/>
    
        <xsl:output indent="yes"/>
    
        <xsl:variable 
            name="mapped-nodes"
            as="map(xs:string, xs:string)"
            select="map:new(for $path in $paths-doc/paths/xpath, $node in mf:evaluate($path/@location, /) return map:entry(generate-id($node), string($path/@annotate)))"/>
    
        <xsl:function name="mf:evaluate" as="node()?">
            <xsl:param name="path" as="xs:string"/>
            <xsl:param name="context" as="node()"/>
            <xsl:evaluate xpath="$path" context-item="$context"></xsl:evaluate>
        </xsl:function>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* , node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="node()[map:contains($mapped-nodes, generate-id())]">
            <xsl:comment select="$mapped-nodes(generate-id())"/>
            <xsl:text>&#10;</xsl:text>
            <xsl:copy>
                <xsl:apply-templates select="@* , node()"/>
            </xsl:copy>
        </xsl:template>
    
    
    </xsl:stylesheet>
    

    作为第一个样式表,它需要 Saxon 9.5 PE 或 EE 才能运行。

    【讨论】:

    • 我喜欢通过利用 XSLT 3.0 的 XPath 评估工具,您的答案似乎能够提供比 template/@match 更强大的匹配。 +1
    【解决方案2】:

    概述:

    编写一个元 XSLT 转换,将 paths 文件作为输入并生成一个新的 XSLT 转换作为输出。这个新的 XSLT 将从您的 root 输入 XML 转换为带注释的副本输出 XML。

    注意事项:

    1. 适用于 XSLT 1.0、2.0 或 3.0。
    2. 应该非常高效,尤其是如果生成的 转换必须在大量输入上运行或必须运行 重复,因为它有效地编译为本机 XSLT 而不是 而不是使用基于 XSLT 的解释器重新实现匹配。
    3. 比必须重建的方法更强大 元素祖先在代码中手动。因为它将路径映射到 template/@match 属性,@matching 的完整复杂性 可以有效地使用。我已经包含了一个属性值测试作为 一个例子。
    4. 请务必考虑@DanielHaley 提供的优雅 XSLT 2.0 和 3.0 解决方案 和@MartinHonnen,尤其是中间元 XSLT 文件 不会为你工作。通过利用 XSLT 3.0 的 XPath 评估 设施,@MartinHonnen 的回答似乎能够提供 比 template/@match 这里的匹配更加稳健。

    此输入 XML 指定 XPath 和注释:

    <paths>
      <xpath location="/root/a" annotate="1"/>
      <xpath location="/root/a/b" annotate="2"/>
      <xpath location="/root/c[@x='123']" annotate="3"/>
    </paths>
    

    当输入到这个元 XSLT 转换时:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="/paths">
        <xsl:element name="xsl:stylesheet">
          <xsl:attribute name="version">1.0</xsl:attribute>
          <xsl:element name="xsl:output">
            <xsl:attribute name="method">xml</xsl:attribute>
            <xsl:attribute name="indent">yes</xsl:attribute>
          </xsl:element>
          <xsl:call-template name="gen_identity_template"/>
          <xsl:apply-templates select="xpath"/>
        </xsl:element>
      </xsl:template>
    
      <xsl:template name="gen_identity_template">
        <xsl:element name="xsl:template">
          <xsl:attribute name="match">node()|@*</xsl:attribute>
          <xsl:element name="xsl:copy">
            <xsl:element name="xsl:apply-templates">
              <xsl:attribute name="select">node()|@*</xsl:attribute>
            </xsl:element>
          </xsl:element>
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="xpath">
        <xsl:element name="xsl:template">
          <xsl:attribute name="match">
            <xsl:value-of select="@location"/>
          </xsl:attribute>
          <xsl:element name="xsl:comment">
            <xsl:value-of select="@annotate"/>
          </xsl:element>
          <xsl:element name="xsl:text">
            <xsl:text disable-output-escaping="yes">&amp;#xa;</xsl:text>
          </xsl:element>
          <xsl:element name="xsl:copy">
            <xsl:element name="xsl:apply-templates">
              <xsl:attribute name="select">node()|@*</xsl:attribute>
            </xsl:element>
          </xsl:element>
        </xsl:element>
      </xsl:template>
    </xsl:stylesheet>
    

    将产生这个 XSLT 转换:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
       <xsl:output method="xml" indent="yes"/>
       <xsl:template match="node()|@*">
          <xsl:copy>
             <xsl:apply-templates select="node()|@*"/>
          </xsl:copy>
       </xsl:template>
       <xsl:template match="/root/a">
          <xsl:comment>1</xsl:comment>
          <xsl:text>&#xa;</xsl:text>
          <xsl:copy>
             <xsl:apply-templates select="node()|@*"/>
          </xsl:copy>
       </xsl:template>
       <xsl:template match="/root/a/b">
          <xsl:comment>2</xsl:comment>
          <xsl:text>&#xa;</xsl:text>
          <xsl:copy>
             <xsl:apply-templates select="node()|@*"/>
          </xsl:copy>
       </xsl:template>
       <xsl:template match="/root/c[@x='123']">
          <xsl:comment>3</xsl:comment>
          <xsl:text>&#xa;</xsl:text>
          <xsl:copy>
             <xsl:apply-templates select="node()|@*"/>
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    

    当提供此输入 XML 文件时:

    <root>
      <a>
        <b>B</b>
      </a>
      <c x="123">C</c>
    </root>
    

    将生成所需的输出 XML 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
      <!--1-->
       <a>
        <!--2-->
          <b>B</b>
      </a>
      <!--3-->
       <c x="123">C</c>
    </root>
    

    【讨论】:

    • 很好,@MartinHonnen。当我编辑答案以显示更强大的匹配示例时,我忽略了更新该输入 XML。现在修好了。谢谢。
    • 我认为这可能是我将采用的方法,因为此时我对 3.0 一点也不熟悉。我已经尝试使用 saxon:evaluate 函数,我怀疑它与 Martin 的解决方案大致相同。关于这个解决方案,我还没有找到一种方法来注释作为属性节点的 xpath。例如"/root/c/@x".
    【解决方案3】:

    我不确定 kjhughes 关于创建第二个转换的建议是否比您最初的想法更有效。如果您的paths XML 变大,我确实看到第二次转换的可能性会变得很大。

    这就是我的做法......

    XML 输入

    <root>
        <a>
            <b>B</b>
        </a>
        <c>C</c>
    </root>
    

    “路径”XML (paths.xml)

    <paths>
        <xpath location="/root/a" annotate="1"/>
        <xpath location="/root/a/b" annotate="2"/>
    </paths>
    

    XSLT 2.0

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:param name="paths" select="document('paths.xml')"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="*" priority="1">
            <xsl:variable name="path">
                <xsl:for-each select="ancestor-or-self::*">
                    <xsl:value-of select="concat('/',local-name())"/>
                </xsl:for-each>
            </xsl:variable>
            <xsl:if test="$paths/*/xpath[@location=$path]">
                <xsl:comment select="$paths/*/xpath[@location=$path]/@annotate"/>
            </xsl:if>
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    XML 输出

    <root>
        <!--1-->
        <a>
            <!--2-->
            <b>B</b>
        </a>
        <c>C</c>
    </root>
    

    【讨论】:

    • 这也是一个非常不同但也很好的解决方案。 +1
    • 谢谢@Daniel,这或多或少是我想出的。我猜我的后续问题与test 谓词在xsl:if 元素中的性能有关。除了O(n) n 是路径的数量之外,它是否可能是其他任何东西?我对此不太了解。
    • 关于这个解决方案我想到的另一件事。我相信如果您的 paths.xml 有奇怪的 xpath,它会失败。例如,如果我将 "/root/a/b" 替换为 /root/*[local-name()='a']/b。两者应该返回相同的节点集,但由于path 变量被构造为字符串/root/a/b,它与第二个不匹配。这是evaluate 函数的唯一出路。
    猜你喜欢
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多