如何使用xslt选择节点的祖父母 [英] How to select the grandparent of a node using xslt
问题描述
我处于只能测试子节点的情况,但是我必须将标签应用于该子节点的祖父母.
I'm in a situation where I can only test for the child node, but I have to apply tags to the grandparent of this child node.
我尝试使用:
<xsl:call-template name="grandparent" select="parent::parent::node()"/>
和:
<xsl:call-template name="grandparent" select="ancestor::node [@nameofgrandparentnode]"/>
但都行不通.
祖父母节点的级别不是固定的,所以我认为我也不能使用[@ level =#].任何有关如何选择它的想法将不胜感激.
The level of the grandparent node is not fixed, so I figure I can't use [@level=#] either. Any ideas on how to select it would be greatly appreciated.
-此部分已发布为新问题:
使用下面的建议选择节点成功了.谢谢!但是,我还需要通过祖父母或孙节点的属性进行测试.
Selecting the node using the suggestions from below worked. Thanks! However I also need to test by the attribute of the grandparent or grandchild node.
我尝试过:
<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"的孙子节点.
However template "two" always gets called, and "" is always inserted. Even for grandchild nodes whose attribute value is not equal to 'attrValue'.
我在这里想念东西吗?
推荐答案
我尝试使用:
I've tried using:
<xsl:call-template name="grandparent" select="parent::parent::node()"/>
和:
<xsl:call-template name="grandparent" select="ancestor::node[@nameofgrandparentnode]"/>
但都行不通.
由于<xsl:call-template>
指令没有select
属性,因此肯定不会成功!
Certainly it will not "work", because the <xsl:call-template>
instruction doesn't have a select
attribute!
您可以使用<xsl:call-template>
指令的<xsl:with-param>
子代来传递参数,如下所示:
You may pass parameters using <xsl:with-param>
children of the <xsl:call-template>
instruction, like this:
<xsl:call-template name="compute">
<xsl:param name="pAncestor" select="someExpression"/>
</xsl:call-template>
对于<xsl:with-param>
使用的select
属性:
对于真正的祖父母:
../..
对于名称为someName
的最接近祖先元素:
for the closest ancestor element with name someName
:
ancestor::someName[1]
最接近的祖先元素,其名称包含在变量$ancName
中:
for the closest ancestor element with name contained in the variable $ancName
:
ancestor::*[name()=$ancName][1]
这篇关于如何使用xslt选择节点的祖父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!