All the nodes having an attribute
我可以使用某种 xpath 语法来获取下面 xml 中存在 identifierref 的所有节点(包括子节点)吗?我一直在尝试类似 XmlNodeList nodeList = xmlDoc.SelectNodes(“//@identifierref”); 的东西,但这不会返回项目节点下方的子标题节点。理想情况下,我想获得一个节点列表,它可以访问存在 identifierref 的项目节点和它们下面的标题节点。下面是我正在使用的 xml。提前致谢。
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<organization xmlns:adlcp=“test1” xmlns=“test2”>
1.2 Tester <item identifier=“C2_LESSON1”> TestName1 <item identifier=“I_SCO1” identifierref=“SCO01”> Tester SCO 1 </item> </item> <item identifier=“C2_LESSON2”> TestName2 <item identifier=“I_SCO2” identifierref=“SCO01”> Tester SCO 2 </item> </item> <item identifier=“C2_LESSON3”> TestName3 <item identifier=“I_SCO3” identifierref=“SCO01”> Tester SCO 3 </item> </item> </organization> |
I want to obtain a node list which has
access to the item nodes that have
identifierref present and the title
nodes below them
1
|
//item[@identifier]
|
上面选择了文档中具有 identifier 属性的所有 item 元素。
如果您还想选择 title 元素子元素。
1
|
//item[@identifier]|//item[@identifier]/title
|
但节点集结果顺序取决于宿主语言(最适用于文档顺序)。也没有”分组”功能:您不能遍历每两个,因为可能缺少一些 title。
所以,首先使用第一个 XPath 表达式,然后使用任何 DOM 方法或相对 XPath 表达式(例如:
)遍历那些获得 title 的 item
1
|
title
|
- 另一个没有两个绝对路径联合的表达式: //*[(self::item|self::title/parent::item)[@identifier]] 但它并不短并且可能效率不高。
来源:https://www.codenong.com/4308566/