在XSL中获取父节点属性

ing*_*.am 18 xslt parent nodes

在我的XML中,我有以下内容:

<a>
  <b>
    <c something="false">
      <d>
        <e>
          <f>someResult</f>
        </e>
      </d>
    </c>
  </b>
</a>
Run Code Online (Sandbox Code Playgroud)

现在在循环中的XSL中,我可以执行以下操作:

<xsl:value-of select="f"></xsl:value-of>
Run Code Online (Sandbox Code Playgroud)

但是如何在c中获取属性?

我尝试过以下操作

<xsl:value-of select="////@something"></xsl:value-of>
Run Code Online (Sandbox Code Playgroud)

除了尝试父母,似乎没有任何工作.你能得到像这样的父节点吗?

另外,我不能这样做:

<xsl:value-of select="/a/b/c/@something"></xsl:value-of>
Run Code Online (Sandbox Code Playgroud)

因为可以有多个c.

kaj*_*kaj 39

要向上移动树,每个级别使用"..",即在这种情况下可能

select="../../../@something"
Run Code Online (Sandbox Code Playgroud)

您还可以按名称选择祖先节点(约)

select="ancestor::c[1]/@something"  
Run Code Online (Sandbox Code Playgroud)

有关更多示例,请参见http://www.stackoverflow.com/questions/3672992


Dim*_*hev 11

用途:

ancestor::c[1]/@something
Run Code Online (Sandbox Code Playgroud)

这将选择名为something第一个(从当前节点向上)祖先命名的属性c.