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

jgr*_*ant 5 xml powershell powershell-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>
Run Code Online (Sandbox Code Playgroud)

我希望能够从xml docoument中的每个组中选择组/ id,组/规则/ id,组/规则/严重性和组/标题值.

我试过这个,但它只是让我在那里的一部分:

I have tried $xml.benchmark.group | %{$_} | select title, id
Run Code Online (Sandbox Code Playgroud)

我感谢您的帮助!

Joe*_*oey 11

这对我有用:

$xml.benchmark.group |
select @{ L = 'GroupID';      E = { $_.id } },
       @{ L = 'GroupTitle';   E = { $_.title } },
       @{ L = 'RuleID';       E = { $_.rule.id } },
       @{ L = 'RuleSeverity'; E = { $_.rule.severity } }
Run Code Online (Sandbox Code Playgroud)

得出以下结论:

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
Run Code Online (Sandbox Code Playgroud)

上面的语法类似于SQL SELECT Foo AS Bar,选择一个值(ExpressionE在散列表中)并为显示目的(LabelL在散列表中)提供别名.