如何仅读取XML属性的第一叶
我有这样的powershell代码:
$Xml = @"
<?xml version="1.0" encoding="utf-8"?>
<DisplayDefinitionTable>
<columns>
<column_entry order_num="0" relation_to_base="Item.current_name">current_name</column_entry>
</columns>
<rows>
<row>
<object_tag tag="91859" uid="TdjJhBMdpQNFhC"/>
<object_tag tag="86504" uid="TtvJBp53pQNFhC"/>
<row_element column="0" component_tag="91859" property_name="current_name">EAUX</row_element>
</row>
<row>
<object_tag tag="92069" uid="DCuJhBMdpQNFhC"/>
<object_tag tag="86504" uid="TtvJBp53pQNFhC"/>
<row_element column="0" component_tag="92069" property_name="current_name">VISS</row_element>
</row>
</rows>
</DisplayDefinitionTable>
"@
Select-Xml -Content $Xml -XPath "//object_tag" | foreach {$_.node.uid}
和结果:
Select-Xml -Content $Xml -XPath "//object_tag" | foreach {$_.node.uid}
TdjJhBMdpQNFhC
TtvJBp53pQNFhC
DCuJhBMdpQNFhC
TtvJBp53pQNFhC
PS C:\Windows\system32>
主要目标是仅获取文件中每个叶子的第一个UID值,以便适当的答案应该看起来像这样:
TdjJhBMdpQNFhC
DCuJhBMdpQNFhC
PS C:\Windows\system32>
如何执行此操作? 谢谢
I have Powershell code like this:
$Xml = @"
<?xml version="1.0" encoding="utf-8"?>
<DisplayDefinitionTable>
<columns>
<column_entry order_num="0" relation_to_base="Item.current_name">current_name</column_entry>
</columns>
<rows>
<row>
<object_tag tag="91859" uid="TdjJhBMdpQNFhC"/>
<object_tag tag="86504" uid="TtvJBp53pQNFhC"/>
<row_element column="0" component_tag="91859" property_name="current_name">EAUX</row_element>
</row>
<row>
<object_tag tag="92069" uid="DCuJhBMdpQNFhC"/>
<object_tag tag="86504" uid="TtvJBp53pQNFhC"/>
<row_element column="0" component_tag="92069" property_name="current_name">VISS</row_element>
</row>
</rows>
</DisplayDefinitionTable>
"@
Select-Xml -Content $Xml -XPath "//object_tag" | foreach {$_.node.uid}
And results:
Select-Xml -Content $Xml -XPath "//object_tag" | foreach {$_.node.uid}
TdjJhBMdpQNFhC
TtvJBp53pQNFhC
DCuJhBMdpQNFhC
TtvJBp53pQNFhC
PS C:\Windows\system32>
The main goal is to take only first uid value of every leaf in the file so proper answer should looks like this:
TdjJhBMdpQNFhC
DCuJhBMdpQNFhC
PS C:\Windows\system32>
How to do this??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用(加速的)标准.NET XML解析器可能会更容易地定位特定 row/object_tag
uid
s s,而不是所有其他可能的uid < /code>在其他级别上:
powershell xml点符号语法:
It is probably easier use the (accelerated) standard .Net xml parser this will also better target the specific row/object_tag
uid
s and not all other possibleuid
at other levels:and the PowerShell Xml dot notation syntax:
尝试将您的命令指定为:
注意,将 [1] 添加到XPath中,以专门拉一个实例。
或者,您也可以像下面的命令一样将管道保存到foreach:
上面的两个变化都可以为您提供所需的结果。
另外,这个很棒的备忘单 xpath上的备忘录中还提供了有关索引等的更多信息。
Try specifying your command as:
Note, the addition of the [1] to the XPath to specifically pull just the first instance.
Or you also could have your command like the below to save piping to the foreach:
Both variations above give you the results you're looking for.
Also, this great cheat sheet on XPath has some further information on indexing, etc...