tags:

views:

273

answers:

2

How to make the following code insert the youtubeId parameter:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
                xmlns:YouTube="urn:YouTube"
    xmlns:umbraco.library="urn:umbraco.library"
    exclude-result-prefixes="msxml umbraco.library YouTube">


<xsl:output method="xml" omit-xml-declaration="yes"/>

 <xsl:param name="videoId"/>
<xsl:template match="/">
 <a href="{$videoId}">{$videoId}</a>

<object width="425" height="355">
<param name="movie" value="http://www.youtube.com/v/{$videoId}&amp;amp;hl=en"&gt;&lt;/param&gt;
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/{$videoId}&amp;amp;hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed>
</object>$videoId {$videoId} {$videoId}
 <xsl:value-of select="/macro/videoId" />
</xsl:template>

</xsl:stylesheet>

As you can see I have experimented quite a bit. <xsl:value-of select="/macro/videoId" /> actually outputs the videoId but all other occurences do not. This must be an easy question to answer but I just cannot get it to work.

I am creating a macro in Umbraco CMS. The parameter is correctly passed into the XSLT (because actually outputs its value). I just want to insert the value into the src-attribute. I dont care at all how I do that as this is a onetime job. Any workaround appreciated.

+6  A: 
 <a href="{$videoId}">{$videoId}</a>

You must use <xsl:value-of select="$videoId"/> here:

<a href="{$videoId}"><xsl:value-of select="$videoId"/></a>

Whenever You are using an AVT ({$videoId}) inside an attribute value this must work with the exception of any select attribute.

In the last case you can use:

<xsl:value-of select="/macro/*[name()=$videoId]" />

When all of these are reflected in your transformation, it works in all cases:

<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxml="urn:schemas-microsoft-com:xslt"
                xmlns:YouTube="urn:YouTube"
    xmlns:umbraco.library="urn:umbraco.library"
    exclude-result-prefixes="msxml umbraco.library YouTube">


<xsl:output method="xml" omit-xml-declaration="yes"/>

 <xsl:param name="videoId" select="'XXX'"/>
<xsl:template match="/">
 <a href="{$videoId}"><xsl:value-of select="$videoId"/></a>

<object width="425" height="355">
<param name="movie" value="http://www.youtube.com/v/{$videoId}&amp;amp;hl=en"&gt;&lt;/param&gt;
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/{$videoId}&amp;amp;hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed>
</object><xsl:value-of select="concat($videoId, ' ', $videoId, ' ', $videoId)"/>
 <xsl:value-of select="/macro/*[name()=$videoId]" />
</xsl:template>

</xsl:stylesheet>
Dimitre Novatchev
Good answer (as always), and AVT is the term for *attribute value template*, see http://www.w3.org/TR/xslt#attribute-value-templates (just for clarification ;-)
0xA3
Your solution made me realize that the parameter videoId gets *not* set by umbraco. I modified it like this and it works: <xsl:param name="videoId" select="/macro/videoId"/>
usr
+1  A: 

You may want to just get a package that helps with this as well.

http://our.umbraco.org is awesome and packages are being added all the time.

A quick search reveals a few options:

http://our.umbraco.org/projects/tag/youtube#projectList

BeaverProj