So I’ve been working on some XSLT to modify YouTube’s RSS XML, and of course as soon as I got it working, they’ve changed their formatting. Before, each video’s unique ID was stored between <videoid> tags, which you could then use to create a URL. But now the only way to get a video’s URL is from a tag like this
<media:player url='https://www.youtube.com/watch?v=XXXXXXXXXXX&feature=youtube_gdata_player'/>`
which is contained within <media:group> tags.
The way I’ve been trying to get at it is
<xsl:value-of select="media:group/media:player@url" />
but doing that gives me a compilation error that says
xsl:value-of : could not compile select expression 'media:group/media:player@url'
Does anyone see anything wrong with that?
Also, as a side note, I want to do something similar with
<xsl:value-of select="media:group/media:thumbnail@url" />
however there are several <media:thumbnail> tags for each entry; would this just grab the first one, or would this potentially cause errors?
you are missing a
/in your XPath. Try:As far as DOM API is concerned, attributes belong to an element. The XPath goes about it in a slightly different way. Child nodes live on the
child::axes (which is the default so you rarely see it used explicitly) and attributes live on theatribute::axes (you can get there with the abbreviated@). When constructing an XPath expression you are basically building a sequence of location steps separated by/. An attribute is “one location step” away from the element owning it.To the second part of your question. The selector will create a sequence (think node list) of all nodes that match the expression and will do what the
xsl:instruction prescribes to do on that sequence. In your case (xsl:value-of), all@urlof all matchingmedia:thumbnailwill be put together in a node-set that will be further converted to a string:So you will get the value of the “first” one. That said, I would argue that running
xsl:value-ofon a sequence of more than one node is not really intuitive (even though the spec clearly says what it will do) so you would do a favor to someone reading the code after you if you be more specific with your selectors. Something like:media:group/media:thumbnail[1]/@url