使用 XPath 检索特定属性的属性值 [英] Use XPath to retrieve attribute value for specific attribute
问题描述
在 PowerShell 中,我尝试使用 XPath 检索属性的值.在下面的示例中,我想获取hi"类别的name"值,即new test".
示例代码显示:
<字符串>测试</字符串><String>test2</String><Report name="new test" category="hi"><String>你好</String><String>你好</String></报告></报告>
这就是我所拥有的:
Select-Xml -XPath "//Report[@name='test']/Report" |选择姓名 |输出文件result.txt"
我的结果是:
<前>名称----我真的不需要指定类别属性,因为 <Report>
标签总是紧跟在第一个 <Report>
标签之后,所以为什么我认为使用 XPath 会更容易.
需要先展开节点.Select-XML 提供对象.如果你这样做:
Select-Xml -Xml $XML -XPath "//Report[@name='test']/Report"
你会得到
<块引用>节点:报告
路径:输入流
模式://报告[@name='test']/报告
展开节点"后,您将获得更多属性
Select-Xml -Xml $XML -xPath "//Report[@name='test']/Report" |Select-Object –ExpandProperty 节点" |选择 *
<块引用>
名称:新测试
类别:你好
字符串:{你好,再次嗨}
本地名称:报告
...等等...等等
完整概念如下
[xml]$XML = @"<报告名称="测试"类别="thisone"><字符串>测试</字符串><String>test2</String><Report name="new test" category="hi"><String>你好</String><String>你好</String></报告></报告>"@Select-Xml -Xml $XML -xPath "//Report[@name='test']/Report" |Select-Object –ExpandProperty 节点" |选择名称
In PowerShell I'm trying to retrieve the value of an attribute using XPath. In the example below, I'd like to get the value of "name" for the category of "hi," which would be "new test."
Example code shows:
<Report name="test" category="thisone">
<String>test</String>
<String>test2</String>
<Report name="new test" category="hi">
<String>hello</String>
<String>hi again</String>
</Report>
</Report>
This is what I have:
Select-Xml -XPath "//Report[@name='test']/Report" | Select name |
Out-File "result.txt"
My results are:
name ----
I don't really need to specify the category attribute because that <Report>
tag will always be right after the first <Report>
tag, so that's why I thought it would be easier to use XPath.
You need to expand the node first. Select-XML is providing the object. If you do :
Select-Xml -Xml $XML -XPath "//Report[@name='test']/Report"
you will get
Node : Report
Path : InputStream
Pattern : //Report[@name='test']/Report
Once you expand "Node" you will get a bunch more properties
Select-Xml -Xml $XML -xPath "//Report[@name='test']/Report" | Select-Object –ExpandProperty "node" | select *
name : new test
category : hi
String : {hello, hi again}
LocalName : Report
...etc...etc
Full Concept Below
[xml]$XML = @"
<Report name="test" category="thisone">
<String>test</String>
<String>test2</String>
<Report name="new test" category="hi">
<String>hello</String>
<String>hi again</String>
</Report>
</Report>
"@
Select-Xml -Xml $XML -xPath "//Report[@name='test']/Report" | Select-Object –ExpandProperty "node" | select name
这篇关于使用 XPath 检索特定属性的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!