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

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

February 23rd, 2009, 05:28 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

February 23rd, 2009, 05:36 AM
|
|
Friend of Wrox
|
|
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
|
|
I just want the output to be ordered 1 2 1 2 1 2 etc for any combination of input 1 and 2's
|
|

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

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

February 23rd, 2009, 07:11 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>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
|
|

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

February 23rd, 2009, 07:26 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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:
|
|
|

February 23rd, 2009, 07:33 AM
|
|
Friend of Wrox
|
|
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
|
|
Yes that is exactly it.
You finally understand it.
Now how do I do a sort in xsl?
Any ideas?
|
|

February 23rd, 2009, 08:02 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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:
|
|
|
 |