Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT: insert parameter value inside of an html attribute

Tags:

xml

xslt

umbraco

How can I insert the youtubeId parameter in the following code :

<?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;hl=en"></param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/{$videoId}&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>

<xsl:value-of select="/macro/videoId" /> actually outputs the videoId but all other occurences do not.

I am creating a macro in Umbraco CMS. The parameter is correctly passed into the XSLT (because actually outputs its value). How can I insert this value into the src-attribute?

like image 858
usr Avatar asked May 06 '10 13:05

usr


People also ask

How do I assign a value to a variable in XSLT?

XSLT <xsl:variable>The <xsl:variable> element is used to declare a local or global variable. Note: The variable is global if it's declared as a top-level element, and local if it's declared within a template. Note: Once you have set a variable's value, you cannot change or modify that value!

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

Can we reassign a value to variable in XSLT?

Variables in XSLT are not really variables, as their values cannot be changed. They resemble constants from conventional programming languages. The only way in which a variable can be changed is by declaring it inside a for-each loop, in which case its value will be updated for every iteration.

What is TD in XSLT?

Definition and Usage The <td> tag defines a standard data cell in an HTML table. An HTML table has two kinds of cells: Header cells - contains header information (created with the <th> element) Data cells - contains data (created with the <td> element)


2 Answers

 <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;hl=en"></param>
<param name="wmode" value="transparent"></param>
<embed src="http://www.youtube.com/v/{$videoId}&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>
like image 120
Dimitre Novatchev Avatar answered Nov 02 '22 22:11

Dimitre Novatchev


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

like image 35
BeaverProj Avatar answered Nov 02 '22 22:11

BeaverProj