How to create hyperlinks in HTML using XSLT?
Create an XML document.
<?xml version="1.0" encoding="ISO8859-1"?>
<?xml-stylesheet type="text/xsl" href="video.xsl"?>
<VIDEOS>
<VIDEO>
<TITLE>Suzie</TITLE>
<DURATION type="second">30</DURATION>
<QUALITY>Quality level 3</QUALITY>
<BITRATE type="kbps">130</BITRATE>
<LINK VALUE="link1.htm">Link 1</LINK>
</VIDEO>
<VIDEO>
<TITLE>REX</TITLE>
<DURATION type="second">130</DURATION>
<QUALITY>Quality level 5</QUALITY>
<BITRATE type="kbps">500</BITRATE>
<LINK VALUE="link2.htm">Link 2</LINK>
</VIDEO>
<VIDEO>
<TITLE>Another Day in Paradise</TITLE>
<DURATION type="second">280</DURATION>
<QUALITY>Quality level 5</QUALITY>
<BITRATE type="kbps">1029</BITRATE>
<LINK VALUE="link3.htm">Link 3</LINK>
</VIDEO>
</VIDEOS>
Create an XSL stylesheet.
<?xml version="1.0" encoding="ISO8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr>
<th>Title</th>
<th>Duration (sec)</th>
<th>Quality</th>
<th>Bitrate (kbps)</th>
<th>Link</th>
</tr>
<xsl:for-each select="VIDEOS/VIDEO">
<tr>
<td align="left"><xsl:value-of select="TITLE"/></td>
<td align="right"><xsl:value-of select="DURATION"/></td>
<td align="right"><xsl:value-of select="QUALITY"/></td>
<td align="right"><xsl:value-of select="BITRATE"/></td>
<td>
<a><xsl:attribute name="href">
<xsl:value-of select="LINK/@VALUE"/></xsl:attribute>
<xsl:value-of select="LINK"/>
</a>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
For simplicity, these XML elements and attributes are the same as HTML.
<link> is used for
<a>,
value="" for
href="" and the text between
<link value="link1.htm">Link 1</link> will produce
<a href="link1.htm">Link 1</a>.
Open an
<a> tag.
<xsl:attribute name="href">
This creates an attribute with the value specified in the "name" attribute.
<xsl:value-of select="LINK/@VALUE"/>
This places the value of the @VALUE attribute in the href attribute.
<xsl:value-of select="LINK"/>
This places anything contained in
<LINK></LINK> element. The example uses text, but this is not always the case.
Close an
</a> tag.
Related threads:
How to write a link into XML?
XML FAQ