Re: [xsl] Better way to change context?

Subject: Re: [xsl] Better way to change context?
From: JBryant@xxxxxxxxx
Date: Thu, 14 Oct 2004 11:25:25 -0500
Unfortunately, xsl:number doesn't give me what I want.

I actually do construct the file name for each chapter with xsl:number. I 
have a bunch of chapters with the wildly original name "chaptern.html", 
where n is chapter number.

So, here's the XSL snippet that sets the chapter number:

  <xsl:template match="chapter">
    <xsl:variable name="chapNum">
      <xsl:number/>
    </xsl:variable>
    <!-- do some other stuff -->
  </xsl:template>

And here's an xsl:number attempt at getting the chapter number with 
xsl:number (as you can see, I just added the number to a working 
cross-reference for testing purposes):

    <xsl:variable name="thisChapter">
      <xsl:for-each select="key('refentries', .)">chapter<xsl:value-of 
select="substring-before(substring-after(saxon:path(), 'chapter['), 
']')"/>.html#</xsl:for-each>
    </xsl:variable>
    <xsl:variable name="thisTest">
      <xsl:for-each select="key('refentries', .)"><xsl:number 
level="multiple" format="1."/></xsl:for-each>
    </xsl:variable>
    <!-- ... -->
        <xsl:value-of select="$thisTest"/><a href="{concat($thisChapter, 
generate-id($reftarget))}"><xsl:value-of select="."/></a>

The resulting output in the HTML is:

1.<a href="chapter8.html#d1e1390a1993">Finding a Snapshot</a>

And every cross-reference has the same number (1) with no additional 
depth. I assume that using a key prevents xsl:number from finding the full 
path.

Much as I prefer to not use extensions (as I may wish to use a different 
processor someday), I elected to use saxon:path, as 10 characters (and 
another namespace declaration at the top of the file) seemed preferable to 
a recursive template that would walk back up the tree to the chapter 
element. If I ever do change processors (which I am not anticipating, as 
Saxon does everything I need to do and does it well), I'll have to fix the 
problem the hard way.

Thanks for the ideas.

Jay Bryant
Bryant Communication Services




David Carlisle <davidc@xxxxxxxxx> 
10/14/2004 10:41 AM
Please respond to
xsl-list@xxxxxxxxxxxxxxxxxxxxxx


To
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
cc

Subject
Re: [xsl] Better way to change context?








> However, something bugs me. I used xsl:for-each to force the context to 
> the node pointed to by the cross-reference. It seems a little silly to 
use 
> a for-each statement to do that.

Not really, saxon:path() could have been designed to take an argument
specifying a node, but it isn't it just does the current node, so you
have to make what you want be the current node and for-each is used for
that purpose as often as it's used for iterating over a set of more than
one. (similar uses come up all the time with key() or xsl:number)

On the other hand I almost certainly wouldn't use an extension element
at all here. Without seeing your input and only a fraction of your
output I'm not sure what I would use, but probably xsl:number would give
you a 3.2.4 type number for chapter 3 section 2 subsetion 4 that you
could use to generate a unique filename suffix for each file.

David


________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread