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 June 3rd, 2006, 05:45 AM
Authorized User
 
Join Date: May 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default sorting nested elements again

I am trying to find a way to sort the contents of an XML document not just by element ("section" in my example), but to also sort the elements in each of those elements ("para" in my example). The result should not seperate the "para" elements from their parent "section" elements when displayed.

My XML document looks like this:

<notes>
    <section sensing="2" intuitive="3">
        <title>Section One

    </title>
        <extra show="on">extra one
        </extra>

        <para sensing="1" intuitive="3">One para

    </para>
        <para sensing="3" intuitive="1">Two para

    </para>
    </section>
    <section sensing="1" intuitive="2">
        <title>Section two

    </title>
        <para sensing="1" intuitive="3">Three para

    </para>
        <para sensing="3" intuitive="1">Four para

    </para>
        <extra show="on">
        extra two
        </extra>

    </section>
    <section sensing="3" intuitive="1">
        <title>Section three

    </title>
        <para sensing="1" intuitive="3">Five para

    </para>
        <para sensing="3" intuitive="1">Six para

    </para>
        <extra show="on">
        extra three
        </extra>

    </section>
</notes>

The stylesheet that sorts this only by the "section" elements looks like this:


<xsl:template match="/">

<html>
    <head>
        <title></title>
    </head>
    <body>

 <xsl:apply-templates/>

    </body>
</html>


</xsl:template>

    <xsl:template match="notes">
    <xsl:apply-templates select="section">
    <xsl:sort select="@sensing" />
        </xsl:apply-templates>
</xsl:template>

<xsl:include href="notesx.xslt"/>

</xsl:stylesheet>

The "notesx" stylesheet looks like this:

<xsl:template match="section">
<h3><xsl:apply-templates select="title"/></h3>
<xsl:for-each select="para">
<p><xsl:value-of select="text()"/></p>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>



Why does the following stylesheet not work?

<xsl:template match="/">

<html>
    <head>
        <title></title>
    </head>
    <body>

<xsl:apply-templates/>


    </body>
</html>


</xsl:template>



  <xsl:template match="notes">

       <xsl:for-each select="section">
      <xsl:sort select="@intuitive"/>

             <xsl:for-each select="para">
                    <xsl:sort select="@intuitive" />

                </xsl:for-each>

        </xsl:for-each>
 </xsl:template>


<xsl:include href="notesx.xslt"/>

</xsl:stylesheet>




 
Old June 5th, 2006, 03:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You code doesn;t actually generate any output within the nested for-each loops. Perhaps you took it out for clarity; but in that case you could have taken a lot of other things out for clarity as well, and you are asking us to debug code which is not the code you actually ran. If you are still having difficulty getting this to work please submit a complete example that's simplified as much as you can, showing the input, the stylesheet, the expected output and the actual output.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting Elements scopley XSLT 4 November 7th, 2007 11:14 AM
Problem nested elements Geierwally XSLT 7 May 9th, 2007 07:27 AM
transformation from attributes to nested elements e-bell XSLT 2 January 21st, 2007 07:21 PM
path for nested elements rjonk XSLT 7 November 20th, 2006 05:43 AM
sorting nested elements stekker XSLT 5 June 5th, 2006 01:19 AM





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