 |
| 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
|
|
|
|

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

November 17th, 2009, 06:09 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

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

November 17th, 2009, 06:22 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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>
|
|

November 17th, 2009, 06:23 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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
|
|

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

November 17th, 2009, 06:40 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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.
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

November 17th, 2009, 06:40 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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:
|
|
|

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

November 17th, 2009, 06:58 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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
|
|
 |