如何仅读取XML属性的第一叶

发布于 2025-02-11 05:43:12 字数 1362 浏览 2 评论 0原文

我有这样的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃扇骨 2025-02-18 05:43:13

使用(加速的)标准.NET XML解析器可能会更容易地定位特定 row/object_tag uid s s,而不是所有其他可能的uid < /code>在其他级别上:

$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>
'@

powershell xml点符号语法:

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

It is probably easier use the (accelerated) standard .Net xml parser this will also better target the specific row/object_tag uids and not all other possible uid at other levels:

$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:

$xml.DisplayDefinitionTable.Rows.Row.ForEach{ $_.object_tag[0].uid }
TdjJhBMdpQNFhC
DCuJhBMdpQNFhC
情话已封尘 2025-02-18 05:43:12

尝试将您的命令指定为:

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

注意,将 [1] 添加到XPath中,以专门拉一个实例。

或者,您也可以像下面的命令一样将管道保存到foreach:

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

上面的两个变化都可以为您提供所需的结果。

另外,这个很棒的备忘单 xpath上的备忘录中还提供了有关索引等的更多信息。

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.

Or you also could have your command like the below to save piping to the 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文