【问题标题】:Parsing an XML with Powershell使用 Powershell 解析 XML
【发布时间】:2015-07-18 20:22:49
【问题描述】:

我需要通读这个 xml 文件并计算第一个 AdaptationSet 中有多少 Representation 元素,因为每次生成这个 xml 时,它的数量都可以从 1 到 10 不等。我是 powershell 的新手,以前是使用 xdoc 读取 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<MPD xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="111091661:1853125475:Ntsc" profiles="urn:mpeg:dash:profile:isoff-live:2011 urn:com:dashif:dash264" type="dynamic" availabilityStartTime="2015-07-09T18:47:42.8780481" publishTime="2015-07-09T18:47:41.7236461" minimumUpdatePeriod="PT3600S" minBufferTime="PT15S" timeShiftBufferDepth="PT60S" suggestedPresentationDelay="PT30S" maxSegmentDuration="PT1.000S" xsi:schemaLocation="urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd" xmlns="urn:mpeg:dash:schema:mpd:2011">
  <Period id="1" start="PT0S">
    <AdaptationSet frameRate="30000/1001" mimeType="video/mp4" codecs="avc1.4D401E" startWithSAP="1" segmentAlignment="true">
      <SegmentTemplate timescale="30000" duration="30030" startNumber="0" media="$Bandwidth$/$Number$.m4v" initialization="$Bandwidth$/0.m4s" />
      <Representation width="314" height="210" id="v0" bandwidth="300000" />
      <Representation width="614" height="414" id="v1" bandwidth="1150000" />
      <Representation width="720" height="486" id="v2" bandwidth="2000000" />
    </AdaptationSet>
    <AdaptationSet mimeType="audio/mp4" codecs="mp4a.40.2" startWithSAP="1" segmentAlignment="true">
      <SegmentTemplate timescale="48000" duration="48048" startNumber="0" media="audio/$Number$.m4a" initialization="audio/0.m4s" />
      <Representation id="a0" bandwidth="448000" />
    </AdaptationSet>
  </Period>
</MPD>

【问题讨论】:

    标签: xml powershell


    【解决方案1】:

    您可以使用Select-Xml通过XPath表达式选择节点:

    (
        Select-Xml -Namespace @{ns='urn:mpeg:dash:schema:mpd:2011'} `
                   -XPath //ns:AdaptationSet[1]/ns:Representation `
                   -Path Test.xml |
        Measure-Object
    ).Count
    

    【讨论】:

      【解决方案2】:

      你也可以这样做:

      $Count = ([xml](Get-Content .\Test.xml)).MPD.Period.FirstChild.Representation.Count
      

      【讨论】:

        猜你喜欢
        • 2013-08-04
        • 2020-12-30
        • 2019-09-27
        • 2021-01-02
        • 2020-12-30
        • 2010-12-15
        • 2020-10-22
        相关资源
        最近更新 更多