Search code examples
htmlxmlxsltvalue-of

Value-of in <a> html markup


I have this XSL file :

<xsl:template match="attribute">
 <a  href="-">
    mylink
 </a>
 <xsl:value-of select="url" />

I want to put the value of my the url attribute in the href.

Do you know how to do this ? Because <a href="<xsl:value-of select="url" />"> doesn't work.


Solution

  • Either

    <a href="{url}">mylink</a>
    

    Or

    <a>
       <xsl:attribute name="href">
         <xsl:value-of select="url"/>
       </xsl:attribute>
       mylink
    </a>
    

    The former is an attribute value template. Read about them in more detail here.

    Caveat: This works only if your input XML looks similar to the following (i.e. if url is really an element and attribute its parent element).

    <attribute>
      <url>www.google.com</url>
    </attribute>
    

    You might have to adapt the above for your XML input (since you do not show it).