你会得到

节点:报告

路径:输入流

模式://报告[@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

上一篇 : :Oracle Regexp 用空格替换 、 和下一篇 : 美化存储在PHP字符串中的HTML

相关阅读

技术问答最新文章