使用带有Where子句的powershell选择XML节点

时间:2014-07-31 13:10:04

标签: arrays xml powershell video

我有以下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>

我正在尝试将URI值设为formatCode="1001"。但是由于它是一个数组,我无法这样做。在获取formatCode =1001数组$uri的所有值都打印在控制台上。我已经尝试过管道Where子句,例如| Where {$xml.video.videoFiles.videoFile.formatCode -eq 1001 },但这也没有帮助。

我使用以下代码:

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
}

1 个答案:

答案 0 :(得分:2)

XPath是一个功能强大的工具,用于选择XML的特定部分。我知道XPath但不知道PowerShell,我基于this post的答案(你的XML与我链接的XML有相似之处,两者都有默认名称空间),可能需要对powershell脚本进行一些修改才能使其适合你的需求:

..... 
$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)

或者,如果您确定formatCode是唯一的,则可以使用SelectSingleNode代替SelectNodes