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 October 20th, 2011, 06:46 AM
Registered User
 
Join Date: Oct 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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
 
Old October 20th, 2011, 07:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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>
__________________
/- 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:
vestlink (October 20th, 2011)
 
Old October 20th, 2011, 07:11 AM
Registered User
 
Join Date: Oct 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
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
 
Old October 20th, 2011, 07:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old October 20th, 2011, 07:33 AM
Registered User
 
Join Date: Oct 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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
 
Old October 20th, 2011, 07:51 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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))".
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old October 20th, 2011, 08:07 AM
Registered User
 
Join Date: Oct 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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..
 
Old October 20th, 2011, 08:54 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old October 20th, 2011, 09:56 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic sort order or sort datatype kapy_kal XSLT 2 September 18th, 2007 02:10 PM
How to sort order in local language. surjit singh .NET Framework 2.0 2 July 21st, 2007 01:54 AM
sort recordset in different order pablohoney Classic ASP Databases 0 December 29th, 2005 08:32 PM
Sort order Question Tere Crystal Reports 1 February 14th, 2005 03:18 PM
Access 2K Sort Order rgerald Access VBA 0 August 12th, 2004 10:37 AM





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