[英]How to: seaching XML child nodes
Given a piece of Xml Like the one below. 给出了一部分Xml,如下所示。 How would I write an XPATH Query to get the value of the 'leaf2' child where the 'key' value has a particular values (say 2)
我将如何编写XPATH查询以获取'key2'子项具有特定值的'leaf2'子项的值(例如2)
I'm working in C# .NET. 我正在使用C#.NET。 At the moment I'm just looking at getting the Xpath for key using SelectNodes , finding the right value then navigating back up to leaf2.
目前,我只是想使用SelectNodes获取键的Xpath,找到正确的值,然后导航回leaf2。
<root>
<child>
<anotherChild>
<key>1</key>
</anotherChild>
<leaf1>X</leaf1>
<leaf2>Y</leaf2>
<leaf3></leaf3>
</child>
<child>
<anotherChild>
<key>2</key>
</anotherChild>
<leaf1>A</leaf1>
<leaf2>B</leaf2>
<leaf3></leaf3>
</child>
</root>
You want: 你要:
/root/child[anotherChild/key = '2']/leaf2
This is saying, "get elements named leaf2
, whose parent is child
and whose grandparent is root
, where child
is being filtered by its child named anotherChild
with a child named key
whose value is 2
." 这就是说,“获取名为
leaf2
元素,其元素的父级为child
,其祖父母为root
,其中child
将被其名为anotherChild
的子key
过滤,其子key
的键值为2
”
Or, perhaps a bit more flexibly because it doesn't assume the grandfather is root 或者,也许更灵活一点,因为它不认为祖父是根
//child/anotherChild/key[text()="2"]/../../leaf2
"find the key with text 2, parent anotherChild and grandparentchild, go to grandparent(ie child, and find leaf2" “找到带有文本2的密钥,将parentOtherChild和祖父母的子女作为父母,转到祖父母(即子女,并找到leaf2”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.