如何通过XPath选择祖先的子节点?

时间:2021-07-07 23:31:29

I have an XML structure similar to:

我有一个类似于:

<a>
   <b>
      <c id="2.3">
      </c>
   </b>
   <d>
      <e>
         <f>
         </f>
      </e>
   </d>
</a>

I am inside a template for "f" where i want to put a when with a test on the first number of id of c, for instance:

我在一个“f”的模板中,在这里我想要在第一个c的id号上做一个测试,例如:

<xsl:template match="f">
   <xsl:choose>
      <xsl:when test="substring-before(../../../c/@id, '.') = '2'">
         <xsl:text>Successful</xsl:text>
      </xsl:when>
   </xsl:choose>
</xsl:template>

The above code doesn't works!

上面的代码不起作用!

Can anyone suggest me a way to make it work?

谁能给我推荐一种工作方式吗?

Thnx in advance!!

提前感谢! !

2 个解决方案

#1


4  

Because there is a b between the ancestor and the c.

因为在祖先和c之间有一个b。

This should work:

这应该工作:

substring-before(../../../b/c/@id, '.') = '2'

(assuming the second <b> was supposed to be a </b>)

(假设第二个应该是a )

#2


4  

Relative paths are nice, but sometimes it's useful to be more explicit.

相对路径是很好的,但有时更显式更有用。

<xsl:template match="f">
   <xsl:choose>
      <xsl:when test="substring-before(ancestor::a[1]/b/c/@id, '.') = '2'">
         <xsl:text>Successful</xsl:text>
      </xsl:when>
   </xsl:choose>
</xsl:template>

#1


4  

Because there is a b between the ancestor and the c.

因为在祖先和c之间有一个b。

This should work:

这应该工作:

substring-before(../../../b/c/@id, '.') = '2'

(assuming the second <b> was supposed to be a </b>)

(假设第二个应该是a )

#2


4  

Relative paths are nice, but sometimes it's useful to be more explicit.

相对路径是很好的,但有时更显式更有用。

<xsl:template match="f">
   <xsl:choose>
      <xsl:when test="substring-before(ancestor::a[1]/b/c/@id, '.') = '2'">
         <xsl:text>Successful</xsl:text>
      </xsl:when>
   </xsl:choose>
</xsl:template>