General discussion

Locked

Creating table of contents with XSLT

By mstefanus ·
Hi Guys,

A question, how can I create a table of contents from an XML file with rule-based XSLT stylesheet? I've been trying to do that but with my current stylesheet every text on the XML file will be transformed to html file, not just the highlights that I want to show. Know the answer? My Thanks in advance.

Martin

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="Article">
<html>
<head>
<title>Table of contents</title>
</head>
<body>
<h3>Table of contents</h3>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>


<xsl:template match="//*/Sect1/Title">
<p><div><a href="{generate-id(.)}"><xsl:apply-templates/></a></div></p>
</xsl:template>

<xsl:template match="//*/Sect2">
<p><div style="text-indent: 1cm"><a href="{generate-id(.)}"><xsl:apply-templates/></a></div></p>
</xsl:template>
<xsl:template match="//*/Bibliography/Title">
<p><div><a href="{generate-id(.)}"><xsl:apply-templates/></a></div></p>
</xsl:template>

<xsl:template match="//*/Bibliography/Title">
<p><div><a href="{generate-id(.)}"><xsl:apply-templates/></a></div></p>
</xsl:template>

</xsl:stylesheet>

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Creating table of contents with XSLT

by Geert Pante In reply to Creating table of content ...

Hi,

You know that in XSLT there are two default templates with low priority:

<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>

You use them both implicit in your example, since in the main template, you put an <xsl:apply-templates/>, causing every child to be elected for template matching, and they all match one of the default templates.

The second default template causes every node text or attribute to be displayed, which is a problem in your case.

One possible solution is to explicitly select all nodes you want to match in the main template: <xsl:apply-templates select="//*/Sect1/Title|
//*/Sect2|//*/Bibliography/Title"/>

An other solution is to override the second default template, and select the content of you <a href> explicitly.

<!-- empty template -->
<xsl:template match="text()|@*" />

<xsl:template match="//*/Bibliography/Title">
<p><div>
<a href="{generate-id(.)}"><xsl:value-of select="text()"/></ a>
</div></p>
</xsl:template>

Collapse -

Creating table of contents with XSLT

by mstefanus In reply to Creating table of content ...

The selection of template helped... but there is an important attribute when you want to make a table of contents, which is mode="toc"

Collapse -

Creating table of contents with XSLT

by mstefanus In reply to Creating table of content ...

This question was closed by the author

Back to Web Development Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums