简体   繁体   中英

Selecting XML node using powershell with Where clause

I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>

<video publishState="Published" xmlns="urn:schemas-microsoft-com:msnvideo:catalog">
<uuid>ed255a56e807</uuid>
<title>ABCD</title>
<description>ABCD</description>
<startDate>2014-06-12T06:30:00Z</startDate>
<activeEndDate>2017-06-01</activeEndDate>
<searchableEndDate>2017-06-01</searchableEndDate>
<archiveEndDate>2019-05-22</archiveEndDate>

<videoFiles>

<videoFile sourceFileHash="ebc2f23aba9f0ff8d0146a052089a34f" bitrate="400" width="320" height="180" fileSize="9654368" msnFileId="ED0ADD1E-7C36-472E-8186-5C43DDCCD58C" formatCode="101">

<uri>http://e2/ds/977d13e5-285f-4fe3-b49c-a6b604e89c22.mp4</uri>

</videoFile>

<videoFile sourceFileHash="ebc2f23aba9f0ff8d0146a052089a34f" bitrate="600" width="640" height="360" fileSize="14911095" msnFileId="59AB7AE5-15F3-4EA8-ABD9-92E1A1CE0424" formatCode="102">

<uri>http://e2/ds/0aaad23d-bcb1-48e4-85c7-788b58ab8ae6/ds/3f4b77a3-3157-48a4-88b5-df854025cfe5.mp4</uri>

</videoFile>

<videoFile msnFileId="EDAA3798-B16B-435F-A9CA-6B1D07729D44" formatCode="1001" fileHash="ebc2f23aba9f0ff8d0146a052089a34f">

<uri>ftp://handler_20140611_highlight_b_222747.mp4</uri>

</videoFile>

<videoFile sourceFileHash="ebc2f23aba9f0ff8d0146a052089a34f" bitrate="3000" width="1280" height="720" fileSize="70668740" msnFileId="9505AEF2-7742-4BB1-9427-F07185D6FC9B" formatCode="104">

<uri>http://e2/ds/a2e1563f-c9a3-41f7-97fe-e024742c6bbf.mp4</uri>

</videoFiles>

</video>

I am trying to get URI value where formatCode="1001" . However since its an array, I am unable to do so. On getting to formatCode =1001 all values of array $uri are printed on console. I have tried piping Where clause eg | Where {$xml.video.videoFiles.videoFile.formatCode -eq 1001 } | Where {$xml.video.videoFiles.videoFile.formatCode -eq 1001 } but that too didn't help.

I am using the following code:

Get-ChildItem D:\video*.xml | ForEach-Object 
{ 
    $xml = [xml] (Get-Content $_.fullname)
    # $xml.video.videoFiles.videoFile | Where-Object { $_.formatCode -eq 1001 } | Format-Table   msnFileId, uri      
    $out = New-Object PSObject
    $out | Add-Member NoteProperty UUID -Value $xml.video.uuid.innertext
    $out | Add-Member NoteProperty EndDate -Value $xml.video.activeEndDate
    Foreach ($uri in $xml.video.videoFiles.videoFile.uri)
    {
        if ($xml.video.videoFiles.videoFile.formatCode -eq 1001)
        {
            $temp1 = [string]$uri
        }
    }
    $out | Add-Member NoteProperty URI -Value $temp1
    Write-Output $out
}

XPath is a powerfull tool to select specific part of an XML. I know XPath but not powershell, I'm basing my answer from this post (your XML has similarity to the one I linked, both has default namespace), some modifications to the powershell script maybe required to make it fit your needs :

..... 
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("ns", $xml.DocumentElement.NamespaceURI)
$xml.SelectNodes("/ns:video/ns:videoFiles/ns:videoFile[@formatCode='1001']/ns:uri", $ns)

Or if you're sure formatCode is unique, you can use SelectSingleNode instead of SelectNodes .

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