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

October 20th, 2011, 06:46 AM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
xslt 1.0 count, devide and sort (order?)
Hi.
As a newbie i'm trying to solve a problem where i have to count the number of an entity and devide it by three in order to get the result sorted like this:
From:
1
2
3
4
5
6
7
8
9
To:
1
4
7
2
5
7
3
6
9
How would i be able to do that
Nicolai
|
|

October 20th, 2011, 07:00 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
I am assuming you mean sorting by the remainder when dividing by 3, i.e. Modulus (or the "mod" operator in XPath).
If this was your input XML:
Code:
<root>
<child>1</child>
<child>2</child>
<child>3</child>
<child>4</child>
<child>5</child>
<child>6</child>
<child>7</child>
<child>8</child>
<child>9</child>
</root>
Then the following XSLT could be used:
Code:
<xsl:template match="root">
<root>
<xsl:for-each select="child">
<xsl:sort select="(number(.)-1) mod 3"/>
<xsl:sort select="number(.)"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</root>
</xsl:template>
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
|
|

October 20th, 2011, 07:11 AM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by samjudson
I am assuming you mean sorting by the remainder when dividing by 3, i.e. Modulus (or the "mod" operator in XPath).
If this was your input XML:
Code:
<root>
<child>1</child>
<child>2</child>
<child>3</child>
<child>4</child>
<child>5</child>
<child>6</child>
<child>7</child>
<child>8</child>
<child>9</child>
</root>
Then the following XSLT could be used:
Code:
<xsl:template match="root">
<root>
<xsl:for-each select="child">
<xsl:sort select="(number(.)-1) mod 3"/>
<xsl:sort select="number(.)"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</root>
</xsl:template>
|
Hi Works great :-)
But if all my numbers have a letter as a prefix like 'L' what then.
Do you have to convert the L+number into somtheing else?
Nicolai
|
|

October 20th, 2011, 07:17 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Please don't ask us to solve a problem for you, and then push back by asking for a solution to a different problem. Explain clearly what input (or what class of inputs) your stylesheet has to handle.
For example, in your sample the input numbers are sorted, and they are dense. Is that a general feature of your inputs, or an accident of your example? We need to know.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

October 20th, 2011, 07:33 AM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Hi.
I'm sorry if have violated a rule of conduct on this forum, which off couse was not my intension.
My data are sorted, but not dense (if that means that all the values are as described in the silutions example) but scatttered in the xml that is sought sorted. And is an general feature of the xml.
As i said, I'm a newbie/beginner a have rudimentary knowledge compared to you solution contributors.
So to elaborate on my problem, as i clumsily tried to explane in the initial post:
The number i try to get sorted in the way explained in the first post are on this format:
L001
L002
etc.
I was wrongly disregarding the prefix letter in my initial question and regret that in hindsight.
Hope that was answers to your questions :-)
Best regard
Nicolai
|
|

October 20th, 2011, 07:51 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
To sort by the number part you would probably use the substring() function to get the number from the text, something like changing this "number(.)" to this "number(substring(.,2))".
|
|

October 20th, 2011, 08:07 AM
|
|
Registered User
|
|
Join Date: Oct 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
I tried with this but can't the a result.
Can you see the problem?
<xsl:template match="lagringsenhet">
<lagringsenhet>
<xsl:for-each select="asta:lagringsenhet/asta:identifikasjon">
<xsl:sort select="number(substring(.,2)-1) mod 3"/>
<xsl:sort select="number(substring(.,2)"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</lagringsenhet>
</xsl:template>
EDIT:
removed the wrong number of paratheses
Last edited by vestlink; October 20th, 2011 at 08:15 AM..
|
|

October 20th, 2011, 08:54 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well given the fact we have no idea what your input XML looks like, and no idea what you are trying to output, nor how the above differs from what you are expecting how on earth could we help?
Also, even though you've edited the above I still think you've got the number of parenthesis wrong.
|
|

October 20th, 2011, 09:56 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>I'm sorry if have violated a rule of conduct on this forum
It's not a rule of conduct, it's just a question of trying to provide the information needed to answer the question, so that you can get the best possible answer in the shortest possible time.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|
 |