xpath for youtube entry search result

2019-08-17 06:14发布

问题:

i need help with the xpath query to extract the title text and thumbnail url of each entry in the following rss results

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/'>
<entry>
<title>LiveFit Augusta - "Gym Time"</title>
<media:group>
<media:thumbnail url='http://i.ytimg.com/vi/Ig7CcLCR2n4/default.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ig7CcLCR2n4/mqdefault.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ig7CcLCR2n4/hqdefault.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ig7CcLCR2n4/1.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ig7CcLCR2n4/2.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/Ig7CcLCR2n4/3.jpg'/>
</media:group>
</entry>
<entry>
<title>LiveFit Augusta - "Everyday Joes &amp; Janes"</title>
<media:group>
<media:thumbnail url='http://i.ytimg.com/vi/REhn9jjjV7Q/default.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/REhn9jjjV7Q/mqdefault.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/REhn9jjjV7Q/hqdefault.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/REhn9jjjV7Q/1.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/REhn9jjjV7Q/2.jpg'/>
<media:thumbnail url='http://i.ytimg.com/vi/REhn9jjjV7Q/3.jpg'/>
</media:group>
</entry>
</feed>

the title

< title type='text'>Evolution of Dance< /title>

or

< media:title type='plain'>Evolution of Dance< /media:title>

thumbnail

<media:thumbnail 
      url='http://img.youtube.com/vi/dMH0bHeiRNg/1.jpg' height='97' width='130' 
      time='00:01:30'/>

EDIT

here is the code am using

Dim xmlDoc As MSXML2.DOMDocument30
Dim xmlNode As MSXML2.IXMLDOMNode
Dim xmlEntryNodes As IXMLDOMNodeList
Dim ns As String
Set xmlDoc = New DOMDocument30
ns = "xmlns:x='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/'"
    xmlDoc.setProperty "SelectionLanguage", "XPath"
    xmlDoc.setProperty "SelectionNamespaces", ns

If xmlDoc.loadXML(xmlText) = False Then
  Exit Function
End If

Set xmlEntryNodes = xmlDoc.documentElement.selectNodes("/feed/entry")

debug.print xmlEntryNodes.Length returns 0

回答1:

Use:

ns = "xmlns:x='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/'"

doc.SetProperty("SelectionNamespaces", ns)

And then use an XPath expression like this:

/*/x:entry/x:title[. = 'Evolution of Dance']

and this:

/*/x:entry/media:group/media:thumbnail[@url='http://img.youtube.com/vi/dMH0bHeiRNg/1.jpg']

Learn more about the SetProperty() function here:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms756048(v=vs.85).aspx

Alternatively, if you don't want to register namespaces, you may use less-readable expressions like these:

/*/*[name()='entry']/*[name()='title'][. = 'Evolution of Dance']

and this:

/*/*[name()='entry']
       /*[name()='media:group']
           /*[name()='media:thumbnail']
                 [@url='http://img.youtube.com/vi/dMH0bHeiRNg/1.jpg']


回答2:

Is this what you are looking for?

title

/feed/entry/title

media:title

/feed/entry/media:group/media:title

thumbnail

/feed/entry/media:group/media:thumbnail