Re: [xsl] Next node outside of axis pattern

Subject: Re: [xsl] Next node outside of axis pattern
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 17 Jul 2009 18:34:06 -0400
At 2009-07-17 15:24 -0700, Spencer Tickner wrote:
If I had an XML
like the following (snippet tried to cover off most scenarios):

<?xml version="1.0"?>
<root>
        <a>This is some text</a>
        <s>
                <section>
                        <toc>Heading 1</toc>
                        <p>This is<endofpage num="1"/> a paragraph</p>
                        <p>This is a paragraph</p>
                </section>
                <section>
                        <toc>Heading 2</toc>
                        <p>This is a paragraph</p>
                </section>
                <section>
                        <p>This is a paragraph</p>
                        <p>This<endofpage num="2"/> is a paragraph</p>
                        <li>
                                <p>This is a paragraph</p>
                                <p>This is a paragraph</p>
                        </li>
                </section>
                <section>
                        <toc>Heading 3</toc>
                        <p>This is a paragraph</p>
                </section>
        </s>
        <a>This<endofpage num="3"/> is some more text</a>
        <p>This is a paragraph</p>
</root>

the <endofpage> element can appear anywhere within the document in an
element that contains text. The issues is building a table of contents
of the <toc> elements. For <toc>Heading 1</toc> no problem using
descendant axis. However with Heading 2 and Heading 3 I can't figure
out how to get the page number. I'm looking for something like:

<xsl:template match="toc">
<p><xsl:value-of select="."/> ......... <!-- next endofpage/@num goes
here --></p>
</xsl:template>


Any thoughts?

So by "next" do you need the "closest" <endofpage> element to the start of each given section with a <toc>?


Since you are already in a section's child, that would entail going up to the parent of <toc> and down as well as those that follow, so the first of the union of those addressed elements:

(..//endofpage | ../following::endofpage)[1]

... though I would strongly suggest you revisit your XML to see if the <endofpage> can be more logically positioned. I understand of course that many times we have to deal with what we are given, but it would seem to me there are too many opportunities for a misplaced <endofpage> element.

I hope the example below helps.

. . . . . . . . . . Ken

T:\ftemp>type tickner.xml
<root>
        <a>This is some text</a>
        <s>
                <section>
                        <toc>Heading 1</toc>
                        <p>This is<endofpage num="1"/> a paragraph</p>
                        <p>This is a paragraph</p>
                </section>
                <section>
                        <toc>Heading 2</toc>
                        <p>This is a paragraph</p>
                </section>
                <section>
                        <p>This is a paragraph</p>
                        <p>This<endofpage num="2"/> is a paragraph</p>
                        <li>
                                <p>This is a paragraph</p>
                                <p>This is a paragraph</p>
                        </li>
                </section>
                <section>
                        <toc>Heading 3</toc>
                        <p>This is a paragraph</p>
                </section>
        </s>
        <a>This<endofpage num="3"/> is some more text</a>
        <p>This is a paragraph</p>
</root>


T:\ftemp>xslt tickner.xml tickner.xsl <?xml version="1.0" encoding="utf-8"?> <table-of-contents> <p>Heading 1...............1</p> <p>Heading 2...............2</p> <p>Heading 3...............3</p> </table-of-contents> T:\ftemp>type tickner.xsl <?xml version="1.0" encoding="US-ASCII"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="/">
  <table-of-contents>
    <xsl:apply-templates select="//toc"/>
  </table-of-contents>
</xsl:template>

<xsl:template match="toc">
  <p>
    <xsl:value-of select="."/>
    <xsl:text>...............</xsl:text>
    <xsl:value-of select="(..//endofpage | ../following::endofpage)[1]/@num"/>
  </p>
</xsl:template>

</xsl:stylesheet>


T:\ftemp>



-- XSLT/XSL-FO/XQuery hands-on training - Oakland, CA, USA 2009-08-03 Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video Video lesson: http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18 Video overview: http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18 G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread