简体   繁体   中英

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

i have following example 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:

en;1;not used / unknown

en;2;not used / unknown

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

As @Matthias R. Jessen suggested in the comments, your XPath is a bit wonky. 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 .

The rest is hopefully self-explanatory...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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