6

I would like to parse a youtube url using XSLT and get only the Video ID from that URL. What's the best way to do this using XSLT?

So, if the url is: http://www.youtube.com/watch?v=qadqO3TOvbQ&feature=channel&list=UL

I only want qadqO3TOvbQ and put it into an embed code:

<iframe width="560" height="315" src="http://www.youtube.com/embed/qadqO3TOvbQ" frameborder="0" allowfullscreen=""></iframe>
Armstrongest
  • 15,181
  • 13
  • 67
  • 106

2 Answers2

4

I. This XPath 2.0 expression:

substring-after(tokenize($pUrl, '[?|&amp;]')[starts-with(., 'v=')], 'v=')

produces the wanted, correct result.

Alternatively, one can use the slightly shorter:

tokenize(tokenize($pUrl, '[?|&amp;]')[starts-with(., 'v=')], '=')[2]

Here is a complete XSLT 2.0 transformation:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:param name="pUrl" select=
"'http://www.youtube.com/watch?v=qadqO3TOvbQ&amp;feature=channel&amp;list=UL'"/>

 <xsl:template match="/">
   <xsl:sequence select=
    "tokenize(tokenize($pUrl, '[?|&amp;]')[starts-with(., 'v=')], '=')[2]"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document (not used), the wanted, correct result is produced:

qadqO3TOvbQ

II. This XPath 1.0 expression:

 concat
   (substring-before(substring-after(concat($pUrl,'&amp;'),'?v='),'&amp;'),
    substring-before(substring-after(concat($pUrl,'&amp;'),'&amp;v='),'&amp;')
   )

produces the wanted result.

Do note:

Both solutions extract the wanted string even if the query string parameter named v isn't the first one or even in the case when it is the last one.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • I love that you provided Xpath 1.0 and XSLT 2.0. I didn't read the whole answer the first time when I saw the xslt 2.0 and tokenize code (which I'm also going to try out) – Armstrongest Jul 12 '12 at 17:03
3

XSLT/XPath is not best suited to string handling (1.0 especially) but you can achieve what you need by mixing up the substring-after() and substring-before() functions:

<xsl:value-of select="substring-before(substring-after($yt_url, '?v='), '&amp;feature')" />

(assumes the YT URL is stored in an XSLT var, $yt_url, and that it has its & escaped to &amp;).

Demo at this this XML Playground

Mitya
  • 33,629
  • 9
  • 60
  • 107
  • Problem is I don't know if it will end in feature... the user will just copy the url and expect it to work. My job is to parse whatever comes. I was thinking between ?v= and & but it may not have the ampersand. – Armstrongest Jul 11 '12 at 16:42
  • Okay... totally +1 for xmlplayground... GREAT site. Also, I modified the solution there to ` ` to catch proper URLs as well. – Armstrongest Jul 11 '12 at 16:47
  • Thanks - I launched it a few weeks back. Spread da word :) Glad you got the result you needed. – Mitya Jul 11 '12 at 16:57