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 May 28th, 2006, 09:17 AM
Authorized User
 
Join Date: May 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default sorting nested elements

I want to sort nested elements without losing their placement inside their parent elements when they get displayed in the browser.

If I have an XML document something like this:

<?xml version="1.0" encoding="UTF-8"?>
<notes>
    <section order="3">

        <para order = "3">
        </para>
        <para order = "2">
        </para>
        <para order = "1">
        </para>

    </section>

    <section order="1">

        <para order = "3">
        </para>
        <para order = "2">
        </para>
        <para order = "1">
        </para>

    </section>

    <section order="2">

        <para order = "3">
        </para>
        <para order = "2">
        </para>
        <para order = "1">
        </para>

    </section>
</notes>


I can get the "section" elements to appear in a different order by using XSLT something like this:

<xsl:template match=" /">

     <xsl:apply-templates select="//section">
    <xsl:sort select="@order" />
    </xsl:apply-templates>

</xsl:apply-templates>


</xsl:template>

But how can I get the nested "para" elemets to appear in the order suggested by their "order" attributes as well? At the moment, I can only get them to appear sorted outside of their sections. I would like the paragraphs to appear in their relevant sections.

Thanks




 
Old May 28th, 2006, 09:35 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Presumably you are outputting the para elements using a template such as

<xsl:template match="section">
  <xsl:apply-templates select="para"/>
</xsl:template>

You just need to change that to:

<xsl:template match="section">
  <xsl:apply-templates select="para">
    <xsl:sort select="@order" data-type="number"/>
  </xsl:apply-templates>
</xsl:template>

It's helpful with this kind of question if you show your non-working code. That helps me to work out where you've misunderstood the spec. As it is, I'm working in the dark.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 29th, 2006, 01:48 AM
Authorized User
 
Join Date: May 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi - sorry, yes - I am not being clear.

I did try the template as you describe it. But if I have both these templates:

<xsl:template match = "/">
<xsl:apply tempates/>

</xsl:template>

<xsl:template match="//section">

     <xsl:apply-templates select="//section">
    <xsl:sort select="@order" />
    </xsl:apply-templates>

</xsl:apply-templates>

<xsl:template match="section">
  <xsl:apply-templates select="para">
    <xsl:sort select="@order" data-type="number"/>
  </xsl:apply-templates>

</xsl:template>

Then the information in the browser comes out something like this:

Section1
Section2
Section3

Para1
Para2
Para3
Para1
Para2
Para3

etc

Instead of

Section1
Para1
Para2
Para3

Section2
Para1
Para2
Para3.

Which is what I want. This must be to do with the placement of my "apply templates" tags but I have no idea how to fix this.



 
Old May 29th, 2006, 06:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Having one template with match="//Section" and another with match="Section" doesn't make sense. Both of them match any section, and the one with match="//Section" will always win because it has higher default priority. Further I would expect that

<xsl:template match="//section">

     <xsl:apply-templates select="//section">
    <xsl:sort select="@order" />
    </xsl:apply-templates>

</xsl:apply-templates>

will cause an infinite loop, because every time you process a section, it causes every section in the document to be processed, including itself. Your sections in your sample document aren't nested so I've no idea what you are trying to do here: even if they were nested, select="section" would make sense but select="//section" wouldn't.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 29th, 2006, 08:15 AM
Authorized User
 
Join Date: May 2006
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK - thats what comes from typing my templates from my head instead of copying from my working stylesheet - it makes no sense. I dont have access to my XSLT files right now, but will copy the working-so-far version as soon as I do.

My problem is with nested elements in the XML document: if I have

<notes>
    <section order="3">

        <para order = "3">
        </para>
        <para order = "2">
        </para>
        <para order = "1">
        </para>

    </section>

    <section order="1">

        <para order = "3">
        </para>
        <para order = "2">
        </para>
        <para order = "1">
        </para>

    </section>

</notes>

How can I have an XSLT stylesheet that orders not only the "section" elements by their attributes, but orders the "para" elements as well and displays the content of the "para" elements each in the correct "section" element.

 
Old June 5th, 2006, 01:19 AM
Registered User
 
Join Date: May 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I guess something like that (?):

<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove">
        <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/>

        <xsl:template match="*">
            <notes>
                <xsl:apply-templates select="section"></xsl:apply-templates>
                <xsl:sort select="@order" data-type="number" />
            </notes>
        </xsl:template>

        <xsl:template match="section">
            <section>
                <xsl:apply-templates select="para"></xsl:apply-templates>
                <xsl:sort select="@order" data-type="number" />
            </section>
        </xsl:template>

        <xsl:template match="para">
            <para></para>
        </xsl:template>

</xsl:stylesheet>






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 again stekker XSLT 1 June 5th, 2006 03:38 AM





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