Search code examples
parsingurlxsltquery-stringc1-cms

How can I parse a YouTube Url using XSLT?


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>

Solution

  • 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