[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
How_To_XML_Create_XML_Hyperlinks_XSLT

How to create hyperlinks in HTML using XSLT?

An XML document can be thought of as a database containing records. Anything that appears on an HTML page can be defined in your own terms and stored as XML thus separating data from design. Links to files are just one example of this.

The example shows how to create a simple collection of files that are displayed as links in an HTML page.

Create the XML document and link it to the XSL stylesheet.

<?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 the 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, the XML element and attributes mimic the way of HTML. <link> is used for <a>, value="" for href="" and the text between <link value="link1.htm">Link 1</link> will appear as <a href="link1.htm">Link 1</a>.

Open the <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 the anything contained in <LINK></LINK> element. Now we have only text, but this is not always the case.

Close the </a> tag.

Related threads:
How to write a link into XML?

XML FAQ

last edited (April 19, 2004) by lillu, Number of views: 3172, Current Rev: 8 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.