简体   繁体   中英

Parsing an XML with Powershell

I need to read through this xml file and count how many Representation elements there are in the first AdaptationSet, because everytime this xml is generated it can have varying amounts from 1 up to 10. I'm new to powershell and was previously using xdoc to read an xml file.

<?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>

You can use Select-Xml to select nodes by XPath expression:

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

您也可以这样做:

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

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