【问题标题】:Selecting XML node using powershell with Where clause使用带有 Where 子句的 powershell 选择 XML 节点
【发布时间】:2015-02-11 07:24:16
【问题描述】:

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

我正在尝试获取formatCode="1001" 的URI 值。但是,由于它是一个数组,因此我无法这样做。在到达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
}

【问题讨论】:

    标签: arrays xml powershell video


    【解决方案1】:

    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

    【讨论】:

    • 我试图通过使用“.”来获取 URI 值。符号并认为它更整洁,但 Xpath 似乎很有帮助。感谢您的帮助 Har07。
    • 是的,没问题。如果通过遵循此答案解决了问题,您可能希望将此答案标记为已接受。更多信息:Stackoverflow Help center > Asking
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多