Search code examples
xmlxslthref

Setting xsl:value-of into an href attribute and the text field of a link in an 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>

Solution

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