Re: [xsl] XHTML table of contents with XSLT

Subject: Re: [xsl] XHTML table of contents with XSLT
From: "Daniel K. Schneider" <Daniel.Schneider@xxxxxxxxxxxxxx>
Date: Sat, 24 Feb 2007 18:23:23 +0100
Thanx lot !! That was it (a bit embarassed again for not finding this)

- Daniel

PS: For others who want the final good solution:

http://tecfa.unige.ch/guides/xml/examples/xsl-toc/xhtml-toc-ns.xsl
http://tecfa.unige.ch/guides/xml/examples/xsl-toc/ (more good and bad examples)

Martin Honnen wrote:
Daniel K. Schneider wrote:

(2) David Carlisle told me to replace the ugly "*[local-name()='xxx']" by "h:xxx"
but then I run into namespace problems (this why I put them there in the first place).

I think your stylesheet is supposed to generate XHTML output with result elements in the XHTML namespace so the stylesheet should have the XHTML namespace as the default namespace (at least for the easiest way to make sure all result elements are in that namespace). Then for XPath expressions and match patterns to be able to select/match elements in the XHTML namespace you need a declaration binding a prefix to the namespace too. To avoid that the prefix is later appearing in the output and making the markup invalid in terms of (rather limited) DTD validation use exclude-result-prefixes e.g.


  <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    version="1.0"
    xmlns="http://www.w3.org/1999/xhtml";
    xmlns:h="http://www.w3.org/1999/xhtml";
    exclude-result-prefixes="h">

Then you can use e.g.

<xsl:template match="/h:html">
  <html>
    <head>
      <title><xsl:value-of select="h:title"/></title>
    </head>

    <body bgcolor="#FFFFFF">
      <xsl:apply-templates select="h:body"/>
    </body>
  </html>
</xsl:template>

<xsl:template match="h:body">
  <strong><a name="toc">Contents</a></strong>
  <xsl:apply-templates select="h:h1 | h:h2" mode="toc"/>
  <xsl:apply-templates />

</xsl:template>

<xsl:template match="h:h1" mode="toc">
  <br/>
  <a href="#h1_{generate-id(.)}"><xsl:value-of select="."/></a><br/>
</xsl:template>

and so on.

Current Thread