xml 从 Xpath 2.0 中的当前节点获取父节点的父节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11834020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting parent's parent from current node in Xpath 2.0
提问by meder omuraliev
I always seem to have trouble with xpath axis expressions...
我似乎总是在使用 xpath 轴表达式时遇到问题...
In some expressions I have used ../to refer to the parent node, but is that not valid for testexpressions? Or is my syntax just wrong?
在某些表达式中,我曾经../引用父节点,但这对test表达式无效吗?还是我的语法错误?
<xsl:when test="../../[@status='current']">
My goal is to apply an attribute inside the xsl:whenIF the parent's parent has a status attribute with a value of 'current'.
我的目标是xsl:when在父级的父级具有值为“当前”的状态属性的IF内应用一个属性。
EDIT: self::parent/parent[@status='current']is a valid xpath expression and may be what I want, can anyone confirm? I might not be going far enough.
编辑:self::parent/parent[@status='current']是一个有效的 xpath 表达式,可能是我想要的,任何人都可以确认吗?我可能走得不够远。
采纳答案by choroba
The problem is in /[. You can change it to
问题出在/[. 你可以把它改成
../../self::*[@status='current']
回答by Michael Kay
A simpler solution than those from choroba and Hansen is
比 choroba 和 Hansen 更简单的解决方案是
../..[@status='current']
回答by Mads Hansen
You could also use the following:
您还可以使用以下内容:
parent::*/parent::*[@status='current']
回答by Dimitre Novatchev
With Xpath 2.0:
使用 Xpath 2.0:
../../@status eq 'current'
With XPath 1.0 andXPath 2.0:
使用 XPath 1.0和XPath 2.0:
../../@status = 'current'

