xml 通过 XPath 提取属性节点的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4835891/
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
Extract value of attribute node via XPath
提问by Rehman
How can I extract the value of an attribute node via XPath?
如何通过 XPath 提取属性节点的值?
A sample XML file is:
示例 XML 文件是:
<parents name='Parents'>
<Parent id='1' name='Parent_1'>
<Children name='Children'>
<child name='Child_2' id='2'>child2_Parent_1</child>
<child name='Child_4' id='4'>child4_Parent_1</child>
<child name='Child_1' id='3'>child1_Parent_1</child>
<child name='Child_3' id='1'>child3_Parent_1</child>
</Children>
</Parent>
<Parent id='2' name='Parent_2'>
<Children name='Children'>
<child name='Child_1' id='8'>child1_parent2</child>
<child name='Child_2' id='7'>child2_parent2</child>
<child name='Child_4' id='6'>child4_parent2</child>
<child name='Child_3' id='5'>child3_parent2</child>
</Children>
</Parent>
</parents>
So far I have this XPath string:
到目前为止,我有这个 XPath 字符串:
//Parent[@id='1']/Children/child[@name]
It returns only childelements, but I would like to have the value of the nameattribute.
它只返回child元素,但我想要name属性的值。
For my sample XML file, here's what I'd like the output to be:
对于我的示例 XML 文件,我希望输出如下:
Child_2
Child_4
Child_1
Child_3
回答by lweller
//Parent[@id='1']/Children/child/@name
Your original child[@name]means an element childwhich has an attribute name. You want child/@name.
您的原始child[@name]意思是一个child具有属性的元素name。你要child/@name。
回答by acdcjunior
To get just the value (without attribute names), use string():
要仅获取值(没有属性名称),请使用string():
string(//Parent[@id='1']/Children/child/@name)
string(//Parent[@id='1']/Children/child/@name)
The fn:string()fucntion will return the value of its argument as xs:string. In case its argument is an attribute, it will therefore return the attribute's value as xs:string.
该FN:字符串()温控功能将返回其作为参数的值xs:string。如果它的参数是一个属性,它将因此返回属性的值作为xs:string。
回答by Natalia Maciejowska
You should use //Parent[@id='1']/Children/child/data(@name)
你应该使用 //Parent[@id='1']/Children/child/data(@name)
The attributes can not be serialized so you can't return them in an xml looking result. What you need to do is obtain the data from the attribute using data() function.
属性无法序列化,因此您无法在 xml 查找结果中返回它们。您需要做的是使用 data() 函数从属性中获取数据。
回答by Vinod Srivastav
As answered above:
如上所述:
//Parent[@id='1']/Children/child/@name
will only output the nameattribute of the 4 childnodes belonging to the Parentspecified by its predicate [@id=1]. You'll then need to change the predicate to [@id=2]to get the set of childnodes for the next Parent.
将只输出属于其谓词指定name的 4 个child节点的属性。然后,您需要将谓词更改为以获取下一个.Parent[@id=1][@id=2]childParent
However, if you ignore the Parentnode altogether and use:
但是,如果您Parent完全忽略该节点并使用:
//child/@name
you can select nameattribute of all childnodes in one go.
您可以一次性选择name所有child节点的属性。
name="Child_2"
name="Child_4"
name="Child_1"
name="Child_3"
name="Child_1"
name="Child_2"
name="Child_4"
name="Child_3"
回答by Akshay Dubey
//Parent/Children[@ Attribute='value']/@Attribute
This is the case which can be used where element is having 2 attribute and we can get the one attribute with the help of another one.
这是可以在元素具有 2 个属性的情况下使用的情况,我们可以在另一个属性的帮助下获得一个属性。
回答by NickC
@ryenus, You need to loop through the result. This is how I'd do it in vbscript;
@ryenus,您需要遍历结果。这就是我在 vbscript 中的做法;
Set xmlDoc = CreateObject("Msxml2.DOMDocument")
xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.load("kids.xml")
'Remove the id=1 attribute on Parent to return all child names for all Parent nodes
For Each c In xmlDoc.selectNodes ("//Parent[@id='1']/Children/child/@name")
Wscript.Echo c.text
Next
回答by Ed Bangga
for all xml with namespace use local-name()
对于所有具有命名空间的 xml 使用 local-name()
//*[local-name()='Parent'][@id='1']/*[local-name()='Children']/*[local-name()='child']/@name

