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 February 23rd, 2009, 05:07 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default sorting issue

I want to sort with xsl the following xml

Code:
<file>
    
        
        <type>2</type>
    
        
        <type>1</type>
    
        <type>2</type>
    
        <type>2</type>
    
        <type>1</type>
    
</file>
into this

Code:
<file>
    
        
        <type>1</type>
    
        
        <type>2</type>
    
        <type>1</type>
    
        <type>2</type>
    
        <type>2</type>
    
</file>
Any ideas
 
Old February 23rd, 2009, 05:28 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

That's a bit like saying the input is ABCDE and you want the output to be WHPRZ. Without giving some idea of the algorithmic relationship of the output to the input, it's a non-starter as a requirements statement.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 23rd, 2009, 05:36 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

I just want the output to be ordered 1 2 1 2 1 2 etc for any combination of input 1 and 2's
 
Old February 23rd, 2009, 06:03 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Oh dear, this is going to be hard work. You clearly understand your problem, but you aren't communicating it very well.

(a) What happens if there is something other than a 1 or a 2 in the input?

(b) One possible description of the transformation you have done is as follows: the input follows the grammar (2+ 1+)* - that is, a sequence of groups, each of which is one-or-more 2s followed by one-or-more 1s - and you want to change it so that each group is reordered with the 1s preceding the 2s. Is that an accurate description of your requirement? (It's a wild guess, I could think of many other relationships between your sample input and output)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 23rd, 2009, 06:15 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

Hello Mr Kay

a) there is only every going to be 1 and 2 as inputs

B) the 1 and 2's can come in any order any amount of times ie 21212, 22112, 12212, 11221 etc

and I want reorder it like the following - 1 followed by 2 followed by 1 followed by 2 etc

I appreciate your effort.

Hope this helps
 
Old February 23rd, 2009, 07:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Hope this helps

No, it doesn't.

If the input is 22222222222222212, what should the output be, and why?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 23rd, 2009, 07:17 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

>
If the input is 22222222222222212, what should the output be, and why?


The output would be 1222222222... etc


No matter how many ones or twos, the output order is always 1212

so for 11112 output of 12111
so for 212111 output of 121211

Thanks
 
Old February 23rd, 2009, 07:26 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

OK, I'll have one more go. Perhaps your rule is this:

Let the number of 1s in the input be X. Let the number of 2s in the input be Y. If X<=Y, output "12" repeated X times, followed by "2" repeated (Y-X) times. If X>Y, output "12" repeated Y times, followed by "1" repeated (X-Y) times.

If I've guessed wrong again, then I give up.
__________________
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:
JohnBampton (February 23rd, 2009)
 
Old February 23rd, 2009, 07:33 AM
Friend of Wrox
 
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default

Yes that is exactly it.

You finally understand it.

Now how do I do a sort in xsl?

Any ideas?
 
Old February 23rd, 2009, 08:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try this:

<xsl:for-each select="type">
<xsl:sort select="count(preceding-sibling::type[. = current()])" data-type="number"/>
<xsl:sort select="."/>
<xsl:copy-of select="."/>
</xsl:for-each>
__________________
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:
JohnBampton (February 23rd, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting issue moin.khan Struts 0 June 21st, 2007 09:37 AM
Please Help Me sorting this nj.nikhil BOOK: Beginning ASP 3.0 1 November 10th, 2006 03:45 PM
SORTING pallone XSLT 3 October 29th, 2006 08:45 AM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM
Sorting? pbernardo XSLT 2 October 27th, 2003 11:34 AM





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