2011-04-04 63 views
5

我有一个格式类似于这样的XML文件:中选择特定的节点在一个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> 

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

我都试过,但它只是让我的存在方式的一部分:

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

我感谢您的帮助!

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

在命令行:

回答

11

这个工作对我来说:

$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)并提供用于显示目的的别名(散列表中的LabelL)。

+0

这是有效的!谢谢! – jgrant 2011-04-04 16:39:22

2

这是可以做到的。

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

我希望它能帮助

JP

+0

谢谢你的尝试。我已经在拉ID和标题。我还需要来自较低级别节点的id和严重性。 – jgrant 2011-04-04 16:40:35

+0

在这里,你并没有做出太多的努力。我确实尝试; o) – JPBlanc 2011-04-04 16:44:45

相关问题