从多个级别选择 XML 文件中的特定节点

发布于 2024-10-30 05:58:45 字数 1137 浏览 2 评论 0原文

我有一个格式类似于以下内容的 xml 文件:

<benchmark>
<group>
    <id>1</id>
    <rule>
        <id>H1234</id>
        <severity>High</severity>
    </rule>
    <title>How to win</title>
</group>
<group>
    <id>2</id>
    <rule>
        <id>5317</id>
        <severity>low</severity>
    </rule>
    <title>How to not</title>
</group>
<group>
    <id>3</id>
    <rule>
        <id>H15678</id>
        <severity>medium</severity>
    </rule>
    <title>How to be</title>
</group>
<group>
    <id>4</id>
    <rule>
        <id>H454</id>
        <severity>High</severity>
    </rule>
    <title>How to lose</title>
</group></benchmark>

我希望能够从 xml 文档中的每个组中选择组/id、组/规则/id、组/规则/严重性和组/标题值。

我已经尝试过,但这只让我完成了部分任务:

I have tried $xml.benchmark.group | %{$_} | select title, id

感谢您的帮助!

I have an xml file in a format similar to this:

<benchmark>
<group>
    <id>1</id>
    <rule>
        <id>H1234</id>
        <severity>High</severity>
    </rule>
    <title>How to win</title>
</group>
<group>
    <id>2</id>
    <rule>
        <id>5317</id>
        <severity>low</severity>
    </rule>
    <title>How to not</title>
</group>
<group>
    <id>3</id>
    <rule>
        <id>H15678</id>
        <severity>medium</severity>
    </rule>
    <title>How to be</title>
</group>
<group>
    <id>4</id>
    <rule>
        <id>H454</id>
        <severity>High</severity>
    </rule>
    <title>How to lose</title>
</group></benchmark>

I would like to be able to select the group/id, group/rule/id, group/rule/severity and group/title values from each group in the xml docoument.

I have tried this but it only gets me part of the way there:

I have tried $xml.benchmark.group | %{$_} | select title, id

I appreciate your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

淑女气质 2024-11-06 05:58:45

这对我有用:

$xml.benchmark.group |
select @{ L = 'GroupID';      E = { $_.id } },
       @{ L = 'GroupTitle';   E = { $_.title } },
       @{ L = 'RuleID';       E = { $_.rule.id } },
       @{ L = 'RuleSeverity'; E = { $_.rule.severity } }

产生以下结果:

GroupID GroupTitle  RuleID RuleSeverity
------- ----------  ------ ------------
1       How to win  H1234  High
2       How to not  5317   low
3       How to be   H15678 medium
4       How to lose H454   High

上面的语法类似于 SQL 的 SELECT Foo AS Bar,选择一个值(ExpressionE in哈希表)并提供用于显示目的的别名(哈希表中的 LabelL)。

This works for me:

$xml.benchmark.group |
select @{ L = 'GroupID';      E = { $_.id } },
       @{ L = 'GroupTitle';   E = { $_.title } },
       @{ L = 'RuleID';       E = { $_.rule.id } },
       @{ L = 'RuleSeverity'; E = { $_.rule.severity } }

yielding the following:

GroupID GroupTitle  RuleID RuleSeverity
------- ----------  ------ ------------
1       How to win  H1234  High
2       How to not  5317   low
3       How to be   H15678 medium
4       How to lose H454   High

The syntax above is similar to SQL's SELECT Foo AS Bar, selecting a value (Expression or E in the hashtable) and providing an alias for display purposes (Label or L in the hashtable).

傾旎 2024-11-06 05:58:45

这可以通过以下方式完成:

Clear-Host
$xmlData = [xml](Get-Content c:\temp\fic.xml)
foreach ($group in $xmlData.benchmark.group)
{
  $group.id
  $group.title
  $group.rule.id
}

在命令行上。

([xml](Get-Content C:\temp\fic.xmlfic.xml)).benchmark.group | % {$_.id + " " + $_.rule.id}

希望对

JP有帮助

This can be done with :

Clear-Host
$xmlData = [xml](Get-Content c:\temp\fic.xml)
foreach ($group in $xmlData.benchmark.group)
{
  $group.id
  $group.title
  $group.rule.id
}

On the command line.

([xml](Get-Content C:\temp\fic.xmlfic.xml)).benchmark.group | % {$_.id + " " + $_.rule.id}

I hope it helps

JP

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