Altova Mailing List Archives


RE: One pass or two?

From: "G. Ken Holman" <gkholman@-------------------->
To:
Date: 8/25/1999 9:09:00 AM
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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.