This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XSLT
The XSLT Example: Generating a new stylesheet
The XSLT Example: Using different axes
The XSLT Example: Creating a summary of author sales for a publisher
The XSLT Example: Creating a summary of all Apollo flights
The XSLT Example:
The XSLT Element: xsl:with-param
The XSLT Element: xsl:when
The XSLT Element: xsl:variable
The XSLT Element: xsl:value-of
The XSLT Element: xsl:transform
The XSLT Element: xsl:text
The XSLT Element: xsl:template
The XSLT Element: xsl:stylesheet
The XSLT Element: xsl:strip-space
The XSLT Element: xsl:sort
The XSLT Element: xsl:processing-instruction
The XSLT Element: xsl:preserve-space
The XSLT Element: xsl:param
The XSLT Element: xsl:output
The XSLT Element: xsl:otherwise
<< XQuery
.NET and XML >>

By :Mark Wilson
I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :03/24/2008
Times viewed :4411

 
[XSLT Reference] [XPath Reference] [Download Reference Download this Reference]

Example: Creating an HTML document with 'previous' and 'next' links

Creating HTML links in a document that is generated from XML: a recurring task. At best, we would like to have a maintainable way to create links in the result document. Especially internal links (where both the link and the links target are generated from the same source) can be tedious to maintain manually. If you set things up as swonw in this sample, both the links and the anchors (the <a name=""> tag) are created using the same code. This guarantees that internal links are always correct.

The last 5 templates in this stylesheet could be used as an import stylesheet in other transformations. The logic has been split up in multiple templates: by doing this, we can import the templates and override one of them to create a slightly different linking behavior (i.e. change only the way to generate a unique identifier to another algorithm).

In this source document, the id attributes are all unique strings, which makes them suitable for use as anchor. The only problem is that they contain spaces and possibly also pound signs (#). These characters are not allowed in the anchor name, so we use the translate function to change all spaces and #'s to underscores (_). Because this template is used for both creating the links and creating the anchors, we can't go wrong!
XSLT elements used:xsl:stylesheet xsl:template xsl:apply-templates xsl:for-each xsl:value-of xsl:call-template xsl:copy-of xsl:param xsl:attribute xsl:text xsl:output
XSLT and XPath functions used:translate
XML source:<?xml version="1.0"?> <book id="book oos" title="The origin of species"> <part id="oos part 1" title="Opening chapters"> <chapter id="oos ch 1" title="An historical sketch"> <par id="par 1"> text text text </par> <par id="par 2"> text text text text text </par> <par id="par 3"> text text text text text text text text text </par> </chapter> <chapter id="oos ch 2" title="Introduction"> <par id="par 4"> text text text text text </par> <par id="par 4"> <b>text text</b> text text text </par> <par id="par 5"> text text text text text </par> <par id="par 6"> text text text text text </par> </chapter> </part> <part id="oos part 2" title="Later chapters"> <chapter id="oos ch 3" title="Variation under domestication"> <par id="par 7"> text text text text text </par> <par id="par 8"> text text text text text </par> </chapter> <chapter id="oos ch 4" title="Variation under nature"> <par id="par 9"> text text text text text </par> </chapter> <chapter id="oos ch 5" title="Struggle for existence"> <par id="par 10"> text text text text text </par> <par id="par 11"> text text text text text </par> <par id="par 12"> text text text text text </par> </chapter> <chapter id="oos ch 6" title="Natural selection"> <par id="par 13"> text text text text text </par> </chapter> <chapter id="oos ch 7" title="Laws of variation"> <par id="par 14"> text text text text text </par> </chapter> </part> </book>
XSLT code:<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <xsl:template match="/book"> <html><body> <xsl:call-template name="anchor-tag-for"/> <h1><xsl:value-of select="@title"/></h1> <xsl:apply-templates select="part"/> </body></html> </xsl:template> <xsl:template match="part"> <xsl:call-template name="anchor-tag-for"/> <h2><xsl:value-of select="@title"/></h2> <xsl:apply-templates select="chapter"/> </xsl:template> <xsl:template match="chapter"> <xsl:for-each select="preceding-sibling::chapter[1]"> Prev: <xsl:call-template name="link-to"/><br/> </xsl:for-each> <xsl:for-each select="following-sibling::chapter[1]"> Next: <xsl:call-template name="link-to"/><br/> </xsl:for-each> <xsl:call-template name="anchor-tag-for"/> <h3><xsl:value-of select="@title"/></h3> <xsl:apply-templates select="par"/> </xsl:template> <xsl:template match="par"> <p> <xsl:call-template name="anchor-tag-for"/> <xsl:copy-of select="node()"/> </p> </xsl:template> <!-- It is important to have a way to reference to a specific location in the source document with a string value. This can be used to create links to a specific location in the content and much more. In this case, every node we want to use as a link has a unique attribute called id. --> <xsl:template name="node-identifier"> <xsl:param name="id-node" select="self::*"/> <xsl:value-of select="translate($id-node/@id, ' #', '__')"/> </xsl:template> <!-- Create an HTML link to any section in the document. A link should always be the same, both from the index or from another chapter. We locate the logic of link-creation in this template. It relies on other templates for determining the file location and the need for an anchor string (trailing #blabla) --> <xsl:template name="link-to"> <xsl:param name="linktext" select="@title"/> <a> <xsl:attribute name="href"> <!-- first the filename --> <xsl:call-template name="file-of"/> <xsl:text>#</xsl:text> <xsl:call-template name="anchor-of"/> </xsl:attribute> <xsl:value-of select="$linktext"/> </a> </xsl:template> <!-- For creating links, one must know the filename. Calculating the filename is isolated in this template. In later versions, when we distribute the content over many files, we will overrule this template with much more interesting versions. --> <xsl:template name="file-of"> <xsl:text>doc.htm</xsl:text> </xsl:template> <!-- Here we decide if we need a trailing hash-string to identify the exact spot in a page where to find a certain piece of information. For now we have only one file, so we'll always need this extra info. --> <xsl:template name="anchor-of"> <xsl:call-template name="node-identifier"/> </xsl:template> <xsl:template name="anchor-tag-for"> <xsl:param name="anchor-node" select="self::*"/> <xsl:for-each select="$anchor-node"> <a> <xsl:attribute name="name"> <xsl:call-template name="anchor-of"/> </xsl:attribute> </a> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Resulting XML output:
saxon
xt
xalan
msxml3
msxml.NET
<?xml version="1.0" encoding="utf-8"?><html><body><a name="book_oos"/><h1>The origin of species</h1><a name="oos_part_1"/><h2>Opening chapters</h2> Next: <a href="doc.htm#oos_ch_2">Introduction</a><br/><a name="oos_ch_1"/><h3>An historical sketch</h3><p><a name="par_1"/> text text text </p><p><a name="par_2"/> text text text text text </p><p><a name="par_3"/> text text text text text text text text text </p> Prev: <a href="doc.htm#oos_ch_1">An historical sketch</a><br/><a name="oos_ch_2"/><h3>Introduction</h3><p><a name="par_4"/> text text text text text </p><p><a name="par_4"/> <b>text text</b> text text text </p><p><a name="par_5"/> text text text text text </p><p><a name="par_6"/> text text text text text </p><a name="oos_part_2"/><h2>Later chapters</h2> Next: <a href="doc.htm#oos_ch_4">Variation under nature</a><br/><a name="oos_ch_3"/><h3>Variation under domestication</h3><p><a name="par_7"/> text text text text text </p><p><a name="par_8"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_3">Variation under domestication</a><br/> Next: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br/><a name="oos_ch_4"/><h3>Variation under nature</h3><p><a name="par_9"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_4">Variation under nature</a><br/> Next: <a href="doc.htm#oos_ch_6">Natural selection</a><br/><a name="oos_ch_5"/><h3>Struggle for existence</h3><p><a name="par_10"/> text text text text text </p><p><a name="par_11"/> text text text text text </p><p><a name="par_12"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br/> Next: <a href="doc.htm#oos_ch_7">Laws of variation</a><br/><a name="oos_ch_6"/><h3>Natural selection</h3><p><a name="par_13"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_6">Natural selection</a><br/><a name="oos_ch_7"/><h3>Laws of variation</h3><p><a name="par_14"/> text text text text text </p></body></html><?xml version="1.0" encoding="utf-8"?> <html><body><a name="book_oos"/><h1>The origin of species</h1><a name="oos_part_1"/><h2>Opening chapters</h2> Next: <a href="doc.htm#oos_ch_2">Introduction</a><br/><a name="oos_ch_1"/><h3>An historical sketch</h3><p><a name="par_1"/> text text text </p><p><a name="par_2"/> text text text text text </p><p><a name="par_3"/> text text text text text text text text text </p> Prev: <a href="doc.htm#oos_ch_1">An historical sketch</a><br/><a name="oos_ch_2"/><h3>Introduction</h3><p><a name="par_4"/> text text text text text </p><p><a name="par_4"/> <b>text text</b> text text text </p><p><a name="par_5"/> text text text text text </p><p><a name="par_6"/> text text text text text </p><a name="oos_part_2"/><h2>Later chapters</h2> Next: <a href="doc.htm#oos_ch_4">Variation under nature</a><br/><a name="oos_ch_3"/><h3>Variation under domestication</h3><p><a name="par_7"/> text text text text text </p><p><a name="par_8"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_3">Variation under domestication</a><br/> Next: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br/><a name="oos_ch_4"/><h3>Variation under nature</h3><p><a name="par_9"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_4">Variation under nature</a><br/> Next: <a href="doc.htm#oos_ch_6">Natural selection</a><br/><a name="oos_ch_5"/><h3>Struggle for existence</h3><p><a name="par_10"/> text text text text text </p><p><a name="par_11"/> text text text text text </p><p><a name="par_12"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br/> Next: <a href="doc.htm#oos_ch_7">Laws of variation</a><br/><a name="oos_ch_6"/><h3>Natural selection</h3><p><a name="par_13"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_6">Natural selection</a><br/><a name="oos_ch_7"/><h3>Laws of variation</h3><p><a name="par_14"/> text text text text text </p></body></html><?xml version="1.0" encoding="UTF-8"?> <html><body><a name="book_oos"/><h1>The origin of species</h1><a name="oos_part_1"/><h2>Opening chapters</h2> Next: <a href="doc.htm#oos_ch_2">Introduction</a><br/><a name="oos_ch_1"/><h3>An historical sketch</h3><p><a name="par_1"/> text text text </p><p><a name="par_2"/> text text text text text </p><p><a name="par_3"/> text text text text text text text text text </p> Prev: <a href="doc.htm#oos_ch_1">An historical sketch</a><br/><a name="oos_ch_2"/><h3>Introduction</h3><p><a name="par_4"/> text text text text text </p><p><a name="par_4"/> <b>text text</b> text text text </p><p><a name="par_5"/> text text text text text </p><p><a name="par_6"/> text text text text text </p><a name="oos_part_2"/><h2>Later chapters</h2> Next: <a href="doc.htm#oos_ch_4">Variation under nature</a><br/><a name="oos_ch_3"/><h3>Variation under domestication</h3><p><a name="par_7"/> text text text text text </p><p><a name="par_8"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_3">Variation under domestication</a><br/> Next: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br/><a name="oos_ch_4"/><h3>Variation under nature</h3><p><a name="par_9"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_4">Variation under nature</a><br/> Next: <a href="doc.htm#oos_ch_6">Natural selection</a><br/><a name="oos_ch_5"/><h3>Struggle for existence</h3><p><a name="par_10"/> text text text text text </p><p><a name="par_11"/> text text text text text </p><p><a name="par_12"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br/> Next: <a href="doc.htm#oos_ch_7">Laws of variation</a><br/><a name="oos_ch_6"/><h3>Natural selection</h3><p><a name="par_13"/> text text text text text </p> Prev: <a href="doc.htm#oos_ch_6">Natural selection</a><br/><a name="oos_ch_7"/><h3>Laws of variation</h3><p><a name="par_14"/> text text text text text </p></body></html><?xml version="1.0" encoding="UTF-16"?><html><body><a name="book_oos" /><h1>The origin of species</h1><a name="oos_part_1" /><h2>Opening chapters</h2> Next: <a href="doc.htm#oos_ch_2">Introduction</a><br /><a name="oos_ch_1" /><h3>An historical sketch</h3><p><a name="par_1" /> text text text </p><p><a name="par_2" /> text text text text text </p><p><a name="par_3" /> text text text text text text text text text </p> Prev: <a href="doc.htm#oos_ch_1">An historical sketch</a><br /><a name="oos_ch_2" /><h3>Introduction</h3><p><a name="par_4" /> text text text text text </p><p><a name="par_4" /> <b>text text</b> text text text </p><p><a name="par_5" /> text text text text text </p><p><a name="par_6" /> text text text text text </p><a name="oos_part_2" /><h2>Later chapters</h2> Next: <a href="doc.htm#oos_ch_4">Variation under nature</a><br /><a name="oos_ch_3" /><h3>Variation under domestication</h3><p><a name="par_7" /> text text text text text </p><p><a name="par_8" /> text text text text text </p> Prev: <a href="doc.htm#oos_ch_3">Variation under domestication</a><br /> Next: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br /><a name="oos_ch_4" /><h3>Variation under nature</h3><p><a name="par_9" /> text text text text text </p> Prev: <a href="doc.htm#oos_ch_4">Variation under nature</a><br /> Next: <a href="doc.htm#oos_ch_6">Natural selection</a><br /><a name="oos_ch_5" /><h3>Struggle for existence</h3><p><a name="par_10" /> text text text text text </p><p><a name="par_11" /> text text text text text </p><p><a name="par_12" /> text text text text text </p> Prev: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br /> Next: <a href="doc.htm#oos_ch_7">Laws of variation</a><br /><a name="oos_ch_6" /><h3>Natural selection</h3><p><a name="par_13" /> text text text text text </p> Prev: <a href="doc.htm#oos_ch_6">Natural selection</a><br /><a name="oos_ch_7" /><h3>Laws of variation</h3><p><a name="par_14" /> text text text text text </p></body></html><?xml version="1.0" encoding="utf-8"?><html><body><a name="book_oos"></a><h1>The origin of species</h1><a name="oos_part_1"></a><h2>Opening chapters</h2> Next: <a href="doc.htm#oos_ch_2">Introduction</a><br /><a name="oos_ch_1"></a><h3>An historical sketch</h3><p><a name="par_1"></a> text text text </p><p><a name="par_2"></a> text text text text text </p><p><a name="par_3"></a> text text text text text text text text text </p> Prev: <a href="doc.htm#oos_ch_1">An historical sketch</a><br /><a name="oos_ch_2"></a><h3>Introduction</h3><p><a name="par_4"></a> text text text text text </p><p><a name="par_4"></a><b>text text</b> text text text </p><p><a name="par_5"></a> text text text text text </p><p><a name="par_6"></a> text text text text text </p><a name="oos_part_2"></a><h2>Later chapters</h2> Next: <a href="doc.htm#oos_ch_4">Variation under nature</a><br /><a name="oos_ch_3"></a><h3>Variation under domestication</h3><p><a name="par_7"></a> text text text text text </p><p><a name="par_8"></a> text text text text text </p> Prev: <a href="doc.htm#oos_ch_3">Variation under domestication</a><br /> Next: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br /><a name="oos_ch_4"></a><h3>Variation under nature</h3><p><a name="par_9"></a> text text text text text </p> Prev: <a href="doc.htm#oos_ch_4">Variation under nature</a><br /> Next: <a href="doc.htm#oos_ch_6">Natural selection</a><br /><a name="oos_ch_5"></a><h3>Struggle for existence</h3><p><a name="par_10"></a> text text text text text </p><p><a name="par_11"></a> text text text text text </p><p><a name="par_12"></a> text text text text text </p> Prev: <a href="doc.htm#oos_ch_5">Struggle for existence</a><br /> Next: <a href="doc.htm#oos_ch_7">Laws of variation</a><br /><a name="oos_ch_6"></a><h3>Natural selection</h3><p><a name="par_13"></a> text text text text text </p> Prev: <a href="doc.htm#oos_ch_6">Natural selection</a><br /><a name="oos_ch_7"></a><h3>Laws of variation</h3><p><a name="par_14"></a> text text text text text </p></body></html>

Note: You can now download the full content of the XPath reference, the XSLT reference and the DOM reference in one PDF document. This document contains the implementation tables, internal links to navigate through the references, a full linked table of contents. You can use free text search through the whole reference or print as a nicely formatted document. Download here...

© 2000 Teun Duynstee. Shown on TopXML.com. Information used from XSLT and XPath recommendations © W3C and MSDN documentation © Microsoft.
Do you have comments, additions or suggestions, mail me.

Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

View all Jobs (Add yours)
View all CV (Add yours)






    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP