使用xslt 2从节点生成xpath

时间:2016-07-27 00:46:54

标签: xml xslt xslt-2.0

要为xml文件中的每个节点生成xpath并将此路径作为属性添加到每个节点,我找到了一些帮助here。 xslt文件应如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" encoding="UTF-8"/>
  <xsl:strip-space elements="*"/>

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

  <xsl:template match="*">
    <xsl:copy>
      <xsl:attribute name="xpath">
        <xsl:for-each select="ancestor-or-self::*">
          <xsl:value-of select="concat('/',local-name())"/>
          <!--Predicate is only output when needed.-->
          <xsl:if
            test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
            <xsl:value-of
              select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')"
            />
          </xsl:if>
        </xsl:for-each>
      </xsl:attribute>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="text()"/>

</xsl:stylesheet>

现在我对使用xslt 2.0更紧凑的方式感兴趣。例如,在以下xslt文件中,我有两个函数createXPath和getXpath。第一个返回带有节点名称的路径,第二个返回相应的数字。是否有可能以聪明的方式将它们结合起来?

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:func="http://www.functx.com">
  <xsl:output method="xml" encoding="utf-8"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:attribute name="xpath">
            <xsl:value-of select="func:getXpath(.)"/>
        </xsl:attribute>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

   <xsl:function name="func:createXPath" >
    <xsl:param name="pNode" as="node()"/>
    <xsl:value-of select="$pNode/ancestor-or-self::*/local-name()" separator="/"/>
  </xsl:function>

  <xsl:function name="func:getXpath">
  <xsl:param name="pNode" as="node()"/>
   <xsl:value-of select="$pNode/ancestor-or-self::*/(count(preceding-sibling::*) + 1)" separator="/" />
</xsl:function> 

</xsl:stylesheet>

2 个答案:

答案 0 :(得分:1)

组合这两个函数相当简单 - 例如,你可以这样做:

<xsl:function name="func:path" >
    <xsl:param name="target" as="element()"/>
    <xsl:value-of select="for $step in $target/ancestor-or-self::* return concat(name($step), '[', count($step/preceding-sibling::*[name() = name($step)]) + 1, ']')" separator="/"/>
</xsl:function>

然而,这种方法效率很低,因为它必须反复遍历树。请考虑一下:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="*">
    <xsl:param name="path"/>
    <xsl:variable name="my-path">
        <xsl:value-of select="$path"/>
        <xsl:text>/</xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text>[</xsl:text>
        <xsl:value-of select="count(preceding-sibling::*[name() = name(current())]) + 1"/>
        <xsl:text>]</xsl:text>
    </xsl:variable>
    <xsl:copy>
        <xsl:attribute name="xpath">
            <xsl:value-of select="$my-path" />    
        </xsl:attribute>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates>
            <xsl:with-param name="path" select="$my-path"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

利用了XSLT的递归处理模型。

答案 1 :(得分:0)

有几点。

(a)没有“节点的XPath”之类的东西。有些人,当他们使用这样的术语时,意味着像a/b/c这样的路径,其他人将意味着a[1]/b[5]/c[6],而其他人将意味着通过在每个测试级别使用谓词来完全命名空间限定的路径命名空间URI()。

(b)XPath 3.0提供了一个函数path(),它返回这种类型的XPath表达式;它使用EQName语法Q{uri}local来确保元素名称不受上下文限制。

(c)我获得你正在产生的那种路径的方法是

<xsl:function name="f:path" as="xs:string">
  <xsl:param name="e" as="element(*)"/>
  <xsl:value-of select="ancestor-or-self::*/concat(
    '/',
    name($e),
    concat('[',f:index($e),']')[. ne '[1]']
  )" separator=""/>
</xsl:function>

<xsl:function name="f:index" as="xs:integer">
  <xsl:param name="e" as="element(*)"/>
  <xsl:sequence select="count(preceding-sibling::*[name()=name($e)])+1"/>
</xsl:function>

然后

<xsl:copy>
  <xsl:attribute name="path" select="f:path(.)"/>
  ....
</xsl:copy>