简体   繁体   English

如何只读取xml属性的第一叶

[英]How to read only first leaf of xml attribute

I have Powershell code like this:我有这样的 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}

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:主要目标是只获取文件中每个叶子的第一个 uid 值,因此正确的答案应该如下所示:

TdjJhBMdpQNFhC
DCuJhBMdpQNFhC


PS C:\Windows\system32> 

How to do this??这个怎么做?? Thanks谢谢

Try specifying your command as:尝试将您的命令指定为:

Select-Xml -Content $Xml -XPath "//object_tag[1]" | foreach {$_.node.uid}

Note, the addition of the [1] to the XPath to specifically pull just the first instance.请注意,将[1]添加到 XPath 以专门提取第一个实例。

Or you also could have your command like the below to save piping to the foreach:或者,您也可以使用如下命令将管道保存到 foreach:

(Select-Xml -Content $Xml -XPath "//object_tag[1]").node.uid

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...此外,这个关于 XPath 的备忘单有一些关于索引等的更多信息......

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 possible uid at other levels:使用(加速的)标准 .Net xml 解析器可能更容易,这也将更好地针对特定的row/object_tag uid ,而不是其他级别的所有其他可能的uid

$Xml = [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>
'@

and the PowerShell Xml dot notation syntax:和 PowerShell Xml 点表示法语法:

$xml.DisplayDefinitionTable.Rows.Row.ForEach{ $_.object_tag[0].uid }
TdjJhBMdpQNFhC
DCuJhBMdpQNFhC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM