Embed XSL code inside a tag

I have some XML data that include a URI. I want to list the URIs on an ASP page, but also make them accessible with <a> tags. However, XSLT does not allow you to embed an XSL command in a tag, for example:

 <xsl:for-each select="//uri"> <tr> <td class="tdDetail"> More information </td> <td> <a href="<xsl:value-of select="." />" alt="More information" target=_blank><xsl:value-of select="." /></a> </td> </tr> </xsl:for-each> 

How do I include the url in the <uri> after the code <a href=" ?

+6
xml xslt
source share
3 answers

Use the <xsl:attribute> element to have a non-fixed attribute.

 <a alt="More information" target="_blank"> <xsl:attribute name="href"> <xsl:value-of select="." /> </xsl:attribute> <xsl:value-of select="." /> </a> 

Edit: As already mentioned, you can also use attribute value templates :

 <a href="{.}" alt="More information" target="_blank"> <xsl:value-of select="." /> </a> 
+11
source share

Using

 <a href="{.}" alt="More information" target="_blank"> <xsl:value-of select="." /> </a> 

Try using AVT (Value Attribute Templates) whenever possible (for all attributes except the select attribute). This makes the code shorter and much more readable.

+5
source share

In addition to using <xsl: attribute> (mentioned in KennyTM's answer), you can also use the abbreviation "{}" when working with attributes:

 <a href="{.}"><xsl:value-of select="." /></a> 
+4
source share

All Articles