xsl-list
[Top] [All Lists]

RE: Producing a Table of Contents

2004-03-26 04:40:01
Sure, using the document() function with nothing between the '' (single
quotes would normally have the name of the external xml file between
them) you can access elements within the current XSL document using the
correct namespace.  So, for example, using
"document('')/*/xsl:template//h1" as the match attribute and "toc" as
the mode attribute would locate all the h1 elements with the namespace
"xsl" and the local-name of "template" and put them into a Result Tree
Fragment that would then be processed by the matching template using
match="h1" and mode="toc"...

So this XSL...

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
  <xsl:strip-space elements="*"/>
  <xsl:output method="html"/>
  <xsl:template match="/">
    <xsl:text>Table of Contents:</xsl:text>
    <br/>
    <xsl:element name="ol">
      <xsl:apply-templates select="document('')/*/xsl:template//h1"
mode="toc"/>
    </xsl:element>
    <br/>
    <h1>this is heading 1</h1>
    <p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i>
      </b>
      <i> with italicized text</i>
    </p>
    <h1>this is heading 2</h1>
    <p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i>
      </b>
      <i> with italicized text</i>
    </p>
    <h1>this is heading 3</h1>
    <p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i>
      </b>
      <i> with italicized text</i>
    </p>
    <h1>this is heading 4</h1>
    <p>this is paragraph one <b>with bold text <i>with bold and
italicized text</i>
      </b>
      <i> with italicized text</i>
    </p>
  </xsl:template>
  
  <xsl:template match="h1 | p | b | i">
    <xsl:copy>
      <xsl:element name="name()">
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="h1" mode="toc">
    <xsl:element name="li">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

Would result in this HTML...

Table of Contents:<br>
<ol>
  <li>this is heading 1</li>
  <li>this is heading 2</li>
  <li>this is heading 3</li>
  <li>this is heading 4</li>
</ol>
<br>
<h1>this is heading 1</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 2</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 3</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>
<h1>this is heading 4</h1>
<p>this is paragraph one <b>with bold text <i>with bold and italicized
text</i></b><i> with italicized text</i>
</p>

Which, is the exact same output as before but using the current document
instead of the source XML as the source of its data.

Let me know if I can help answer any more of your questions :)

Best of luck!

<M:D/>

-----Original Message-----
From: Julian Voelcker [mailto:asp(_at_)tvw(_dot_)net] 
Sent: Friday, March 26, 2004 3:58 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Producing a Table of Contents

Hi,

On Fri, 26 Mar 2004 03:43:48 -0700, M. David Peterson wrote:
Using the mode attribute of apply-templates allows you to select
particular elements and transform them different for each type of
mode.
Using "//hi" will select all h1 elements from your source.  Using
mode="toc" allows you to match each of these elements to the template
with the match attribute set to "h1" and the mode set to "toc".

Many thanks of for that.

Can the technique be applied in an XSLT document to search within itself

rather than a source XML file?
-- 
Cheers,

Julian Voelcker
United Kingdom



--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
You are subscribed as: m(_dot_)david(_at_)mdptws(_dot_)com
To unsubscribe, go to:
http://lists.mulberrytech.com/unsub.php/xsl-list/m(_dot_)david(_at_)mdptws(_dot_)com
or e-mail:
<mailto:xsl-list-unsubscribe-m(_dot_)david=mdptws(_dot_)com(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--



<Prev in Thread] Current Thread [Next in Thread>