xml - 获取当前节点的所有祖先

标签 xml xslt xpath xslt-1.0

我想获取当前节点的所有祖先:

XML:

<root>
   <item title="a">
       <item title="b">               
           <item title="c"></item> <!--CURRENT-->
           <item title="d"></item>                 
        </item> 
       <item title="x">             
           <item title="y"></item> 
           <item title="z"></item>  
       </item>            
   </item> 
</root>

结果:

<item title="a">...</item>
<item title="b">...</item>

编辑: 轴祖先的答案很好。 我的问题在别处,在 XSLT 中

XSLT:

<xsl:variable name="curr" select="//item[@title = 'c']"></xsl:variable>
<xsl:variable name="test" select="$curr/ancestor::item"></xsl:variable>

<xsl:for-each select="$test/item">
<xsl:value-of select="@title"></xsl:value-of>
</xsl:for-each>

返回:

bcdx

Edit2: 为 dimitre 和所有有类似问题的人

我的问题的所有答案都很好。

Just XSLT (up) 返回给我一个奇怪的结果,@Mads Hansen 纠正了我。

最终工作示例:

XML:

<?xml version="1.0" encoding="utf-8"?>
<root>
   <item title="a">
       <item title="b">               
           <item title="c"></item> 
           <item title="d"></item>                 
        </item> 
       <item title="x">             
           <item title="y"></item> 
           <item title="z"></item>  
       </item>            
   </item> 
</root>

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:variable name="curr" select="//item[@title = 'c']"></xsl:variable>
        <xsl:variable name="test" select="$curr/ancestor::item"></xsl:variable>

        <xsl:for-each select="$test">
            <xsl:value-of select="@title"></xsl:value-of>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

返回:

ab

最佳答案

祝贺 Adam 很快给出了第一个答案。

只是添加一点细节:

您列出的预期结果与您的话不符。根元素也是祖先节点,文档也是祖先节点。

ancestor::node()

... 将按以下顺序返回一个序列:

  1. item[@title='b']
  2. item[@title='a']
  3. root 元素(又名文档元素)
  4. root node /

要获得您列出的特定结果,您需要:

ancestor::item/.

/的效果。是将排序改回转发文档顺序。 ancestor::的原生顺序是反向文档顺序。


更新:评论提要中提出的观点的说明。

此样式表(带有 OP 的输入)...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="//item[@title='c']">
    <xsl:value-of select="ancestor::item[1]/@title" />
    <xsl:value-of select="ancestor::item[2]/@title" />
  </xsl:for-each>  
</xsl:template>         
</xsl:stylesheet>

... 将输出 'ba' 说明 ancestor::确实是一个反向轴。然而这个样式表......

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="//item[@title='c']">
    <xsl:value-of select="(ancestor::item/@title)[1]" />
    <xsl:value-of select="(ancestor::item/@title)[2]" />
  </xsl:for-each>  
</xsl:template>

</xsl:stylesheet>

... 有相反的结果 'ab' 。这是有启发性的,因为它表明在 XSLT 1.0 中(在 XSLT 2.0 中不是这样),括号消除了反向性质,它变成了文档有序节点集。

OP 询问了类似...的转换

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:for-each select="//item[@title='c']">
    <xsl:for-each select="ancestor::item">
      <xsl:value-of select="@title" />
    </xsl:for-each>
  </xsl:for-each>  
</xsl:template>

</xsl:stylesheet>

这个返回“ab”(在 XSLT 2.0 中它将返回“ba”)。为什么?因为在 XSLT 1.0 中,xsl:for-each 指令忽略轴的反向性并按文档顺序处理(除非 xsl:sort 指令另有说明)。

关于xml - 获取当前节点的所有祖先,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12369734/

相关文章:

python - 无法找到带有类名的元素,无法找到带有 xpath 的元素 - Selenium,Python, `ERR_SSL_VERSION_OR_CIPHER_MISMATCH`

html - 表中的内容

xml - 为什么xsl :value-of not working here (XML namespaces)?

xml - XSLT:文档分析:如何在文档中输出唯一路径?

xml - 使用XSLT基于字符串模式自动创建xml模板

java - XSL 使用参数 for-each

c#-4.0 - XmlDocument选择节点: find an element by an attribute value only

java - 写入 XML 时强制转义字符

java - TextView 未放置在屏幕的一半下方

java - 在 Java 中将元素(org.w3c.dom)转换为字符串