XML and XQL

Again since I cant post a reply to “XML help, please” there I post it here

  1. simply walk the list of nodes knowing the structure. this can be problematic at times though
  2. XQL can be used in a few ways. For instance :
    assuming sampleXML looks like
<?xml version="1.0" encoding="UTF-8"?>
<assets>
<asset id="r2" name="00018M" uid="DF36C3050CEDE81F705DC99FCF0E1487">
     <media-rep kind="original-media" sig="DF36C3050CEDE81F705DC99FCF0E1487" src="file:///Volumes/Storage2/00018M.MXF">
     </media-rep>
     <metadata>
          <md key="com.apple.rawToLogConversion" value="0"/>
          <md key="com.apple.kMDItemProfileName" value="SD (6-1-6)"/>
     </metadata>
</asset>
<asset id="r3" name="00019M" uid="1">
     <media-rep kind="original-media" sig="D" src="file:///Volumes/Storage_45/00019M.MXF">
     </media-rep>
     <metadata>
          <md key="com.apple.rawToLogConversion" value="0"/>
          <md key="com.apple.kMDItemProfileName" value="SD (6-1-6)"/>
     </metadata>
</asset>
</assets>

then code as follows will work

Dim xmlDoc As New XmlDocument(sampleXML)

Dim docNode As XmlNode = xmlDoc.DocumentElement

// find all assets that are at the top level
Dim assets As XmlNodeList = docNode.XQL("//asset")

For i As Integer = 0 To assets.Length - 1
  
  Dim currentAsset  As XmlNode = assets.Item(i)
  
  // now we want the media-rep nodes
  // maybe there's one or more ?
  // note we do not use // as we just want the ones that are in THIS node
  Dim mediaRep As XmlNodeList = currentAsset.XQL("media-rep")
  
  For j As Integer = 0 To mediaRep.Length - 1
    
    Dim currentmediaRep As XMLNode = mediaRep.item(j)
    
    // print the src attribute
    System.debuglog currentmediaRep.GetAttribute("src")
    
  Next
  
Next

A second way to do this is to use a more comprehensive XQL Query

// find all media-reps that are contained by an asset
// and get their src attribute

// // is the descendent operator
// see http://www.ibiblio.org/xql/xql-proposal.html

Dim srcs As XmlNodeList = docNode.XQL("//asset/media-rep/@src")

For i As Integer = 0 To srcs.Length - 1
  
  Dim currentSrc As XmlNode = srcs.Item(i)
  
  System.debuglog currentSrc.Value
  
Next

// is the “descendent operator” and will find nodes disregarding intervening nodes
when at the beginning of a query like this it means “starting from the root element”
So this query finds all “asset” nodes that have “media-rep” nodes that have a “src” attribute in one quick shot