0

I have the following XML syntax and I'm trying to obtain the value in Node ID "2", Row Key "Vol2-3" for now:

<nodes>
   <node id="1">
     <store>
        <row key="Vol1-1" value="1a"/>
        <row key="Vol1-2" value="1b"/>
        <row key="Vol1-3" value="1c"/>
        <row key="Vol1-4" value="1d"/>
</store>
</node>
   <node id="2">
     <store>
        <row key="Vol2-1" value="2a"/>
        <row key="Vol2-2" value="2b"/>
        <row key="Vol2-3" value="2c"/>
        <row key="Vol2-4" value="2d"/>
</store>
</node>
   <node id="3">
     <store>
        <row key="Vol3-1" value="3a"/>
        <row key="Vol3-2" value="3b"/>
        <row key="Vol3-3" value="3c"/>
        <row key="Vol3-4" value="3d"/>
</store>
</node>
</nodes>

Code:

$nodevar=2 
[xml]$XMLDoc=(Get-Content "\..\..somefile.txt")
$value_extract=$XMLDoc.nodes.SelectNodes("node[@id='$nodevar']").store.SelectNodes("row[@key='']")

The issue I have is that I'm trying to set my $value_extract variable to pivot from the correct node ID to get to the corresponding row key containing my value. I wrote it in such a way that I'll eventually be using the SelectNodes in a for loop to obtain the values I need for further processing. Is there a good way to write this since I'm pivoting off of the correct node ID as well as the correct row key?

2 Answers 2

1

I'm not sure I understand your pivot-problem, but this should work, if you just need to get the text value:

[xml]$xml = Get-Content "somexml.xml"
$nodevar = "2"
$keyvar = "Vol2-3"
$value_extract=$xml.SelectNodes("/nodes/node[@id='$nodevar']/store/row[@key='$keyvar3']/@value")
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Palle Due. I meant to say using $nodevar=2 to move from that specific node value to get to the underlying row key values. Dunno if that explains it better.
0

Or without using xpath syntax:

$node = $XMLDoc.nodes.node | ? id -eq '2'
$row = $node.store.row | ? key -eq 'Vol2-3'
$row.value

1 Comment

Thanks Swonkie, I'll have to look into this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.