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

Josh picture Josh · Apr 1, 2010 · Viewed 67.6k times · Source

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>

Answer

zneak picture zneak · Apr 1, 2010

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