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 November 17th, 2009, 06:03 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
Default Sort with XSL

Hi,

I have a strange question which I hope someone will be help me with...

if I had some XML like below..

Code:
<children>
 <child name="bob" />
 <child name="dave" />
 <child name="sid" seq="3" /> 
 <child name="fred" seq="1" />
 <child name="rupert" seq="2"/>
 <child name="mildred" />
</children>
and I wanted to sort ONLY the children that have a seq atrribute all the other children MUST stay in the position they are in.. (as below)

Code:
<children>
 <child name="bob" />
 <child name="dave" />
 <child name="fred" seq="1" /> 
 <child name="rupert" seq="2"/>
 <child name="sid" seq="3" />
 <child name="mildred" />
</children>
is this possible??

I know you can sort with XSL using the xsl:sort element, but will it work in this case??

Cheers

Darren
 
Old November 17th, 2009, 06:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't think the problem is well specified. In your example, the elements with a seq attribute are in a single contiguous group. What output would you want if that were not the case?

Also you don't say whether you are sorting by seq or by name - in your example the answer is the same either way.

If you want to sort groups of contiguous elements having a seq attribute, you could use

Code:
<xsl:for-each-group select="child" group-adjacent="boolean(@seq)">
  <xsl:choose>
     <xsl:when test="current-grouping-key()">
        <xsl:perform-sort select="current-group()">
            <xsl:sort select="@seq"/>
        </xsl:perform-sort>
     </xsl:when>
     <xsl:otherwise>
         <xsl:sequence select="current-group()"/>
     </
  </
</
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old November 17th, 2009, 06:15 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
Default

yeah sorry about me not been very clear about what I mean, it's a little hard to explain exactly what I do mean..

basic I only want to sort nodes that have a seq attribute and I want to sort them by seq, if they don't have a seq attrib it is essential that they stay where they are..

I hope this clears things up a little..
 
Old November 17th, 2009, 06:22 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It also isn't clear from your example whether the @seq attribute will always be contiguous and consecutive , i.e. will there always be elements which each number in the sequence, or might there be element with @seq = 1, 2, 4 and 5, with no 3.

Code:
   <xsl:template match="child[@seq]">
      <xsl:variable name="seq">
         <xsl:number count="//child[@seq]"/>
      </xsl:variable>
      <child name="{../child[@seq=$seq]/@name}" seq="{$seq}"/>
   </xsl:template>
   <xsl:template match="child[not(@seq)]">
      <xsl:copy-of select="."/>
   </xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old November 17th, 2009, 06:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I hope this clears things up a little..

No, it doesn't clear things up at all.

What output would you want for this input?

Code:
<children>
 <child name="bob" seq="3"/>
 <child name="dave" />
 <child name="sid" /> 
 <child name="fred" seq="1" />
 <child name="rupert" seq="2"/>
 <child name="mildred" />
</children>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old November 17th, 2009, 06:34 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I kinda wish I'd not started this now, I knew it wasn't gonna be easy for me to explain..

@mhkay from your example I would want..

Code:
<children>
<child name="dave" />
<child name="sid" /> 
<child name="fred" seq="1" />
<child name="rupert" seq="2"/>
<child name="bob" seq="3"/>
<child name="mildred" />
</children>
@samjudson - no it won't always be contiguous and consecutive, it could go ..

Code:
<children>
 <child name="bob" seq="5"/>
 <child name="dave" />
 <child name="sid" /> 
 <child name="fred" seq="3" />
 <child name="rupert" seq="2"/>
 <child name="mildred" />
</children>
 
Old November 17th, 2009, 06:40 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

From Michael's example - how do you determine where the moved children now come - they appear in your output grouped together, but with no indication of why they appear after sid and before mildred.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
crazeydazey (November 17th, 2009)
 
Old November 17th, 2009, 06:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't think we can code this one until we have a clear unambiguous specification.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
crazeydazey (November 17th, 2009)
 
Old November 17th, 2009, 06:49 AM
Authorized User
 
Join Date: Jun 2006
Posts: 22
Thanks: 2
Thanked 0 Times in 0 Posts
Default

not to worry guys, there is a hell of a lot more to the full story, which I couldn't begin to explain..

I will have to try and think of some other way around this (ie making sure the XML comes out in the order I want in the first place, I think this would be easier for me.. XSLT is NOT my strong point..)

thanks for your help and time though.. much appreciated...
 
Old November 17th, 2009, 06:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>XSLT is NOT my strong point

We're here to help with the XSLT. But we can't help you specify the problem.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Xsl to Sort the following simple xml: Takashi321 XSLT 3 August 27th, 2007 11:54 AM
xsl:sort Question kwilliams XSLT 6 July 18th, 2005 08:39 AM
Unable to sort using xsl sort command sly_jimmy_boy XSLT 3 June 17th, 2005 05:15 AM
XSLT-- xsl:sort help... debo XSLT 2 December 3rd, 2004 04:25 PM
xsl:sort and IE 5.0 Issue babloo81 XSLT 1 March 17th, 2004 06:12 AM





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