Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2003, 07:16 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default walking an axis in sorted (not document) order

Here's a kind-of simple case for what I'm trying to do...

Input file:
<foo>
<bar>banana</bar>
<bar>eggplant</bar>
<bar>cherry</bar>
<bar>apple</bar>
<bar>donut</bar>
</foo>

Desired Output:
previous siblings for node apple:

previous siblings for node banana:
        apple

previous siblings for node cherry:
        apple banana

previous siblings for node donut:
        apple banana cherry

previous siblings for node eggplant:
        apple banana cherry donut


Here's my first stab:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.1">

    <xsl:output method="text"/>

    <xsl:template match="foo">
        <xsl:apply-templates select="bar">
            <xsl:sort/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="bar">
previous siblings for node <xsl:value-of select="."/>:
<xsl:for-each select="preceding-sibling::bar">
<xsl:value-of select="."/><xsl:text> </xsl:text>
</xsl:for-each>

<xsl:text>#10;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

..but that produces the output:

previous siblings for node apple:
banana eggplant cherry

previous siblings for node banana:


previous siblings for node cherry:
banana eggplant

previous siblings for node donut:
banana eggplant cherry apple

previous siblings for node eggplant:
banana

So, basically, I want to be able to get a node-set of previous-siblings in the <xsl:sort> order, not document order. Is this possible?

Thanks,
Rich
 
Old August 8th, 2003, 12:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 147
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to armmarti
Default

No, you can't affect the preceding-sibling(and any other) axis using xsl:sort instruction(an XSLT processor maintains internal tree of the source XML document, and when you use an axis, it looks in the tree independent of any sorting you specify). xsl:sort just guarantees that the current nodeset will be processed in the order you specified; you must rearrange the nodes to achieve the goal: copy all the "bar" elements into a variable, and then use your technique:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">
    <xsl:output method="text"/>
    <xsl:template match="foo">
        <xsl:variable name="bars">
            <xsl:for-each select="bar">
                <xsl:sort select="."/>
                <xsl:copy-of select="."/>
            </xsl:for-each>
        </xsl:variable>

        <xsl:for-each select="exsl:node-set($bars)/*">
            previous siblings for node <xsl:value-of select="."/>:
            <xsl:for-each select="current()/preceding-sibling::bar">
                <xsl:value-of select="."/><xsl:text> </xsl:text>
            </xsl:for-each>
            <xsl:text>#10;</xsl:text>

        </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>
The XSLT processor you're using must support EXSLT's node-set extension function, or otherwise it must provide its own extension function to be able to convert tree fragment to nodeset.

Regards,
Armen





Similar Threads
Thread Thread Starter Forum Replies Last Post
Walking through walls qjay BOOK: Professional Microsoft Robotics Studio ISBN: 978-0-470-14107-6 2 July 11th, 2008 03:03 PM
Document Order for preceding-sibling mphare XSLT 5 February 22nd, 2007 03:10 PM
sorted table crmpicco Javascript How-To 0 March 17th, 2005 10:29 AM
Walking the Tree rmcmillian XSLT 4 November 22nd, 2004 07:20 PM
document node order vs sort node order. ladyslipper98201 XSLT 2 June 5th, 2003 11:06 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.