RE: One pass or two?

Subject: RE: One pass or two?
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 25 Aug 1999 13:09:25 -0400
At 99/08/25 16:32 +0100, DPawson@xxxxxxxxxxx wrote:
Sorry Ken, I'm not explaining very well:

Sorry, Dave, I must not be answering very well.  :{)}

source document has an element <q> which
I want to add to a table of contents.
Being lazy, I haven't used the implied id Attribute
in the DTD.
Hence source line is:

        <q>blah</q>

output wanted, in HTML

The TOC which refers to the element via a hot link.

        <H4>Table of contents</H4>
        <p><a href=""> ...

Then in the body I need to have the id
as the target of the link from the table of contents.

<p> <a name="N49"> blah </a></p>


The missing id attribute I can put in using
generate-id() in the stylesheet, but I guess
that the id isn't available for use on the input side?

... but it is available from the input at all times during the run.

For what you need, you need only go over the source tree twice, once for the table of contents and once for the body (or, of course, any other times you may wish to visit the source tree).

Each time around you use generate-id(.) in a different construct of the result tree, but because it is one run of the XSLT engine the value returned for a given node is guaranteed to be the same.  As soon as you finish the run that guarantee is gone ... the next time around you must treat the value created by generate-id(.) as brand new, even if it coincidentally happens to be what it was before.

Note that the value generated for the result tree comes from the node in the source tree ... you aren't asking anything about the result tree.

I hope the example below helps.

........... Ken

T:\dave>type test.xml
<?xml version="1.0"?>
<test>
  <q>blah 1</q>
  <q>blah 2</q>
</test>

T:\dave>type test.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">

<xsl:output indent="yes"/>

<xsl:template match="/">                    <!--root rule-->
  <H4>Toc</H4>
  <xsl:for-each select="//q">
    <p><a href="" select="."/></a></p>
  </xsl:for-each>
  <body>
    <xsl:apply-templates/>
  </body>
</xsl:template>

<xsl:template match="q">
  <p><a name="{generate-id(.)}"><xsl:value-of select="."/></a></p>
</xsl:template>

</xsl:stylesheet>

T:\dave>call xsl test.xml test.xsl test.htm
T:\dave>type test.htm
<H4>Toc</H4>
<p>
<a href="" 1</a>
</p>
<p>
<a href="" 2</a>
</p>
<body>
  <p>
<a name="N3">blah 1</a>
</p>
  <p>
<a name="N6">blah 2</a>
</p>
</body>

T:\dave>

--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Practical Transformation Using XSLT and XPath      ISBN 1-894049-01-2
Next instructor-led training:                     MT'99 1999-12-05/06
Current Thread
  • One pass or two?
    • DPawson - Wed, 25 Aug 1999 12:13:53 +0100
      • <Possible follow-ups>
      • DPawson - Wed, 25 Aug 1999 16:32:29 +0100
        • G. Ken Holman - Wed, 25 Aug 1999 13:09:25 -0400 <=
      • DPawson - Thu, 26 Aug 1999 07:19:57 +0100