如何使用 xslt 选择节点的祖父母

2024-05-09

我所处的情况是,我只能测试子节点,但我必须将标签应用于该子节点的祖节点。

我尝试过使用:

<xsl:call-template name="grandparent" select="parent::parent::node()"/>

and:

<xsl:call-template name="grandparent" select="ancestor::node [@nameofgrandparentnode]"/>

但两者都不起作用。

祖父母节点的级别不固定,所以我想我也不能使用[@level=#]。任何有关如何选择它的想法将不胜感激。

编辑:--这部分已作为新问题发布:

xslt 根据其孙节点的属性值选择祖父节点 https://stackoverflow.com/questions/3687466/xslt-select-grandparent-node-depending-on-an-attribute-value-of-its-grandchild-no

使用下面的建议选择节点是有效的。谢谢!但是我还需要通过祖父母或孙节点的属性进行测试。

我试过了:

<xsl:template name"one" match="grandparentnode">
 <Tag1>
  <xsl:apply-templates select="parentNode" />
 </Tag1>
</xsl:template>

<xsl:template name="two" match="grandparentnode[*/*/@grandchildattr='attrValue']">
 <Tag2>
     <xsl:apply-templates select="parentNode" />
 </Tag2>
</xsl:template>

然而,模板“two”总是被调用,并且“”总是被插入。即使对于属性值不等于“attrValue”的孙节点也是如此。

我在这里错过了什么吗?


我尝试过使用:

<xsl:call-template name="grandparent" select="parent::parent::node()"/> 

and:

<xsl:call-template name="grandparent" select="ancestor::node[@nameofgrandparentnode]"/>

但两者都不起作用。

当然它不会“起作用”,因为<xsl:call-template>指令没有select属性!

您可以使用传递参数<xsl:with-param>的孩子们<xsl:call-template>指令,像这样:

<xsl:call-template name="compute">
  <xsl:param name="pAncestor" select="someExpression"/>
</xsl:call-template>

For the select的属性<xsl:with-param> Use:

对于真正的祖父母:

../..

对于具有名称的最近祖先元素someName:

ancestor::someName[1]

对于名称包含在变量中的最接近的祖先元素$ancName:

ancestor::*[name()=$ancName][1]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 xslt 选择节点的祖父母 的相关文章