Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting xsl:value-of into an href attribute and the text field of a link in an XSLT

Tags:

href

xml

xslt

How can I set an a href that is both a link to and has the text for a link through an XSLT transformation? Here's what I have so far, which gives me the error "xsl:value-of cannot be a child of the xsl:text element":

<xsl:element name="a">    <xsl:attribute name="href">       <xsl:value-of select="actionUrl"/>    </xsl:attribute>    <xsl:text><xsl:value-of select="actionUrl"/></xsl:text>  </xsl:element> 
like image 701
Josh Avatar asked Apr 01 '10 19:04

Josh


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!

How do I hyperlink in XSLT?

XSLT doesn't do hyperlinks. Rethink your question. When thinking about how to achieve something like this in XSLT, split the task into two: (a) decide what HTML you want to generate, and (b) decide what XSLT code you need in order to generate it.

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.


2 Answers

<xsl:text> defines a text section in an XSL document. Only real, plain text can go here, and not XML nodes. You only need <xsl:value-of select="actionUrl"/>, which will print text anyways.

<xsl:element name="a">     <xsl:attribute name="href">         <xsl:value-of select="actionUrl"/>     </xsl:attribute>     <xsl:value-of select="actionUrl"/> </xsl:element> 
like image 133
zneak Avatar answered Sep 19 '22 00:09

zneak


You can also do:

<a href="{actionUrl}"><xsl:value-of select="actionUrl"/></a> 
like image 24
lexicore Avatar answered Sep 22 '22 00:09

lexicore