简体   繁体   English

PowerShell:通过xpath解析xml节点,创建output字符串

[英]PowerShell: Parse through xml Nodes via xpath and create output string

i have following example XML我有以下示例 XML

<AlarmGroup xmlns="http://.." Name="example">
  <TextGroup>
   <TextLayer LanguageId="en">
      <Text ID="1" Value="not used / unknown"/>
      <Text ID="2" Value="not used / unknown"/>
      <Text ID="3" Value="not used / unknown"/>
      <Text ID="4" Value="not used / unknown"/>
    </TextLayer>
    <TextLayer LanguageId="de">  
      <Text ID="1" Value="not used / unknown"/>
      ...   
    </TextLayer> 

' I try to go through each Texlayer and build a string like the following: ' 我尝试通过每个 Texlayer go 并构建如下字符串:

en;1;not used / unknown zh;1;未使用/未知

en;2;not used / unknown zh;2;未使用/未知

I have tried many ways, for example:我尝试了很多方法,例如:

 $AlarmgroupLanguageText = Select-Xml -Xml $Content -XPath "//example:TextLayer" -namespace $ns | select -ExpandProperty node

    $AlarmgroupLanguageText.TextLayer |foreach{
      $AlarmgroupLanguageText |foreach{
        $output += $_.LanguageID + ";" + $_.Text.ID + ";" + $_.Text.Value + "`r`n"

    }   

It would be great if someone could help me request this layman's Question.如果有人可以帮助我提出这个外行的问题,那就太好了。

TIA TIA

As @Matthias R. Jessen suggested in the comments, your XPath is a bit wonky.正如@Matthias R。Jessen 在评论中建议,您的 XPath 有点不稳定。 However, once you've got that working there's a few other things wrong with your code as well, so stepping back a bit, here's a slightly different way to do it:但是,一旦您开始工作,您的代码也会出现一些其他问题,所以退后一步,这里有一种稍微不同的方法:

$xml = [xml] @"
<AlarmGroup xmlns="http://.." Name="example">
  <TextGroup>
   <TextLayer LanguageId="en">
      <Text ID="1" Value="not used / unknown"/>
      <Text ID="2" Value="not used / unknown"/>
      <Text ID="3" Value="not used / unknown"/>
      <Text ID="4" Value="not used / unknown"/>
    </TextLayer>
    <TextLayer LanguageId="de">  
      <Text ID="1" Value="not used / unknown"/>
    </TextLayer>
  </TextGroup>
</AlarmGroup>
"@;

# 1. find a root <AlarmGroup> where Name="example"
$alarmGroup = @( $xml.AlarmGroup | where-object { $_.Name -eq "example" } )[0];

# 2. get all the child <Text> nodes nested under the <AlarmGroup>
$textNodes = @( $alarmGroup.TextGroup.TextLayer.Text );

# 3. format each <Text> node into a string
$textLines = $textNodes | foreach-object {
    $_.ParentNode.LanguageId + ";" + $_.ID + ";" + $_.Value
};

# 4. concatenate all the strings together
$output = $textLines -join "`r`n";

# 5. show the result
$output
# en;1;not used / unknown
# en;2;not used / unknown
# en;3;not used / unknown
# en;4;not used / unknown
# de;1;not used / unknown

The tricksy bit is $alarmGroup.TextGroup.TextLayer.Text - this uses Member Enumeration to expand the TextGroup and TextLayer nodes to get all the nested Text nodes - in this case, it's basically equivalent to the XPath //TextGroup/TextLayer/Text .棘手的一点是$alarmGroup.TextGroup.TextLayer.Text - 它使用成员枚举来扩展TextGroupTextLayer节点以获取所有嵌套的Text节点 - 在这种情况下,它基本上等同于 XPath //TextGroup/TextLayer/Text

The rest is hopefully self-explanatory... rest 希望是不言自明的......

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

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