0

I`m trying to select one node, by string value of another node.

My Xml part:

          <XIP xmlns="http://prty/XIP/v6.0">
             <Generation>
               <Properties>
                 <Property>
                   <PUID>prp/10008</PUID>
                   <PropertyName>Number Of Audio Streams</PropertyName>
                   <Value>1</Value>
                 </Property>
                <Property>
                  <PUID>prp/12</PUID>
                  <PropertyName>Sampling Frequency</PropertyName>
                  <Value>48000</Value>
                </Property>
                <Property>
                  <PUID>prp/10002</PUID>
                  <PropertyName>Duration</PropertyName>
                  <Value>2M9.686S</Value>
                </Property>
             <Properties>
          <Generation>
        </XIP>

So, the idea is get 2M9.686S, by string Duration

$xmlDocument.XIP.Generation.Properties.Property.Value |  Where-Object $xmlDocument.XIP.Generation.Properties.Property -Property PropertyName -Like ("Duration")

I'm trying for different ways, but don't return any value. What am i doing wrong?

Thanks for any help.

1
  • Xml has errors no </Properties> or </Generation>. Commented Jun 12, 2020 at 17:15

2 Answers 2

1

You can navigate like this:

$xmlDocument.XIP.Generation.Properties.Property | where {$_.PropertyName -eq "Duration"}

to access to the specific Property and the followint to access to the Value, ie:

($xmlDocument.XIP.Generation.Properties.Property |
    where {$_.PropertyName -eq "Duration"}).value

(For replicate the scenario, I used the $xmlDocument = [xml](Get-Content .\test.xml) to save and access to the XML content)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I think that actually exist another best approach, but if it works, perfect!
You don't need the backquote, a pipe can continue a line, among a lot of other things.
Thanks js2010, i try your solution and works fine to!
0

Here's the xpath version. The namespace adds a little complication. The double quotes are required, otherwise the pattern looks like an array.

$ns = @{x = 'http://prty/XIP/v6.0'}
select-xml "//*[x:PropertyName='Duration']" file.xml -Namespace $ns | 
  % {$_.node.value}

2M9.686S

Comments

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.