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>

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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)
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

3 Comments

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 &amp; but it may not have the ampersand.
Okay... totally +1 for xmlplayground... GREAT site. Also, I modified the solution there to ` <xsl:variable name='yt_url' select='concat("youtube.com/…)' />` to catch proper URLs as well.
Thanks - I launched it a few weeks back. Spread da word :) Glad you got the result you needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.