472,951 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

sort and numbering - xslt

Hi all,
I'm having problem with XSLT transformation concerned with sort and
numberings at a time.
I wish to have my registries sorted alfabetically and would like them
to have numberings at a time as well.

My XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<root>
<child><name>Paul</name></child>
<child><name>Adam</name></child>
<child><name>Will</name></child>
<root>
My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//child">
<xsl:sort order="ascending" select="name" data-type="text"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>

<xsl:template match="child">
<xsl:number format="1. "/>
<xsl:value-of select="name" /><br />
</xsl:template>

</xsl:stylesheet>

And would like to have the following after transformation:

1. Adam
2. Paul
3. Will

Unfortunatelly result is not the one I expected:

2. Adam
1. Paul
3. Will

Please help, thanks in advance....

Nov 26 '06 #1
7 1909
xsl:number wants to use document order and
is not affected by the sort.

One possible solution is to first create a separate
node set containing the nodes of interest in the
correct order and then applying xsl:number to
that set, rather than the original set of nodes.

Unfortunately, that has the potential for getting you
into the irritating historical XSLT problem of "is that
thing a node set or a result tree fragment?". In any
case, here's a solution using exslt that works with
xsltproc and probably some other processors:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl"
version="1.0"
>
<xsl:template match="/">
<xsl:variable name="SortedChildren">
<xsl:for-each select="//child">
<xsl:sort order="ascending" select="name" data-type="text"/>
<xsl:copy-of select="." />
</xsl:for-each>
</xsl:variable>
<html>
<body>
<xsl:for-each select="exsl:node-set($SortedChildren)/child">
<xsl:apply-templates select="." />
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template match="child">
<xsl:number format="1. "/>
<xsl:value-of select="name" /><br />
</xsl:template>

</xsl:stylesheet>

Hope this helps,
Ron Burk
www.xmlator.com

Nov 26 '06 #2
Use the position() function.
Cheers,
Dimitre Novatchev
"pstachy" <ps*****@gmail.comwrote in message
news:11*********************@14g2000cws.googlegrou ps.com...
Hi all,
I'm having problem with XSLT transformation concerned with sort and
numberings at a time.
I wish to have my registries sorted alfabetically and would like them
to have numberings at a time as well.

My XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<root>
<child><name>Paul</name></child>
<child><name>Adam</name></child>
<child><name>Will</name></child>
<root>
My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//child">
<xsl:sort order="ascending" select="name" data-type="text"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>

<xsl:template match="child">
<xsl:number format="1. "/>
<xsl:value-of select="name" /><br />
</xsl:template>

</xsl:stylesheet>

And would like to have the following after transformation:

1. Adam
2. Paul
3. Will

Unfortunatelly result is not the one I expected:

2. Adam
1. Paul
3. Will

Please help, thanks in advance....

Nov 27 '06 #3
Could you please amplify your 'position()' idea. I looked for this
function and found it returns a number equal to the context position
from the expression evaluation context.
Sorry I am total beginner and I do not know how to use this in practise
in this particular case.
(Unfortunatelly the solution with exsl does not work with Firefox
processor and I'm forced to use it.)
Dimitre Novatchev napisal(a):
Use the position() function.
Cheers,
Dimitre Novatchev
"pstachy" <ps*****@gmail.comwrote in message
news:11*********************@14g2000cws.googlegrou ps.com...
Hi all,
I'm having problem with XSLT transformation concerned with sort and
numberings at a time.
I wish to have my registries sorted alfabetically and would like them
to have numberings at a time as well.

My XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<root>
<child><name>Paul</name></child>
<child><name>Adam</name></child>
<child><name>Will</name></child>
<root>
My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//child">
<xsl:sort order="ascending" select="name" data-type="text"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>

<xsl:template match="child">
<xsl:number format="1. "/>
<xsl:value-of select="name" /><br />
</xsl:template>

</xsl:stylesheet>

And would like to have the following after transformation:

1. Adam
2. Paul
3. Will

Unfortunatelly result is not the one I expected:

2. Adam
1. Paul
3. Will

Please help, thanks in advance....
Nov 27 '06 #4
pstachy wrote:
I'm having problem with XSLT transformation concerned with sort and
numberings at a time.
I wish to have my registries sorted alfabetically and would like them
to have numberings at a time as well.
<root>
<child><name>Paul</name></child>
<child><name>Adam</name></child>
<child><name>Will</name></child>
<root>
As you are transforming to HTML I think you should simply transform that
list of child elements to a HTML ordered list e.g.

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="html"/>

<xsl:template match="/">
<html>
<head>
<title>Example</title>
</head>
<body>
<ol>
<xsl:apply-templates select="root/child">
<xsl:sort select="name" data-type="text" order="ascending"/>
</xsl:apply-templates>
</ol>
</body>
</html>
</xsl:template>

<xsl:template match="child">
<li><xsl:value-of select="name"/></li>
</xsl:template>

</xsl:stylesheet>

That way you generate a well structured HTML document with semantical
markup (an ordered list) and the browser will render the numbers for you.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 27 '06 #5

"pstachy" <ps*****@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
Could you please amplify your 'position()' idea. I looked for this
function and found it returns a number equal to the context position
from the expression evaluation context.
Sorry I am total beginner and I do not know how to use this in practise
in this particular case.
(Unfortunatelly the solution with exsl does not work with Firefox
processor and I'm forced to use it.)
Then you need to read a good XSLT/XPath book (I usually recommend Michal Kay
and Jeni Tennison).

And Martin Honnen is right that in case the intended output is html, then it
is a good idea to use just <lielements within an <olelement.
Cheers,
Dimitre Novatchev
>

Dimitre Novatchev napisal(a):
>Use the position() function.
Cheers,
Dimitre Novatchev
"pstachy" <ps*****@gmail.comwrote in message
news:11*********************@14g2000cws.googlegro ups.com...
Hi all,
I'm having problem with XSLT transformation concerned with sort and
numberings at a time.
I wish to have my registries sorted alfabetically and would like them
to have numberings at a time as well.

My XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<root>
<child><name>Paul</name></child>
<child><name>Adam</name></child>
<child><name>Will</name></child>
<root>
My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//child">
<xsl:sort order="ascending" select="name" data-type="text"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>

<xsl:template match="child">
<xsl:number format="1. "/>
<xsl:value-of select="name" /><br />
</xsl:template>

</xsl:stylesheet>

And would like to have the following after transformation:

1. Adam
2. Paul
3. Will

Unfortunatelly result is not the one I expected:

2. Adam
1. Paul
3. Will

Please help, thanks in advance....

Nov 27 '06 #6
Yes, I wish I had time for reading some book but unfortunatelly I
don't... :) I have to finish this till tommorrow. Indeed the <li>
solution is great, but in my particular case does not work. I didn't
write that before because I thought it was not important.... well, it
is with the <liidea. Namely I am using table in may xslt, like this:

<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:apply-templates select="//child">
<xsl:sort order="ascending"
select="name"

data-type="text"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>

<xsl:template match="child">
<tr>
<td>
<xsl:number format="1. "/>
</td>

<td>
<xsl:value-of select="name" />
</td>
</tr>
</xsl:template>

So as you see I can't use <li>.... :(

If anyone has any idea how to solve this I would very appreciate if he
shared it with me:)

Anyway, thanks for all previous help...

Best Regards
Piotr

Dimitre Novatchev napisal(a):
"pstachy" <ps*****@gmail.comwrote in message
news:11**********************@h54g2000cwb.googlegr oups.com...
Could you please amplify your 'position()' idea. I looked for this
function and found it returns a number equal to the context position
from the expression evaluation context.
Sorry I am total beginner and I do not know how to use this in practise
in this particular case.
(Unfortunatelly the solution with exsl does not work with Firefox
processor and I'm forced to use it.)

Then you need to read a good XSLT/XPath book (I usually recommend Michal Kay
and Jeni Tennison).

And Martin Honnen is right that in case the intended output is html, then it
is a good idea to use just <lielements within an <olelement.
Cheers,
Dimitre Novatchev


Dimitre Novatchev napisal(a):
Use the position() function.
Cheers,
Dimitre Novatchev
"pstachy" <ps*****@gmail.comwrote in message
news:11*********************@14g2000cws.googlegrou ps.com...
Hi all,
I'm having problem with XSLT transformation concerned with sort and
numberings at a time.
I wish to have my registries sorted alfabetically and would like them
to have numberings at a time as well.

My XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xsl.xsl"?>
<root>
<child><name>Paul</name></child>
<child><name>Adam</name></child>
<child><name>Will</name></child>
<root>
My XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="//child">
<xsl:sort order="ascending" select="name" data-type="text"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>

<xsl:template match="child">
<xsl:number format="1. "/>
<xsl:value-of select="name" /><br />
</xsl:template>

</xsl:stylesheet>

And would like to have the following after transformation:

1. Adam
2. Paul
3. Will

Unfortunatelly result is not the one I expected:

2. Adam
1. Paul
3. Will

Please help, thanks in advance....
Nov 27 '06 #7
If anyone has any idea how to solve this I would very appreciate if he
shared it with me:)
Hmmm, well, the stylesheet I supplied produces
the output you specified from the input you specified...
Was there a problem with that?

Ron Burk
www.xmlator.com

Nov 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andy Glew | last post by:
I have long looked for (and occasionally posted questions to groups such as this about) a tool that can take a group of HTML pages (nowadays XHTML, or XML) and produce a nicely formatted...
2
by: Mike Dickens | last post by:
hi, i have a query regarding numbering one set of elements as filtered by another set. eg if i have <?xml version="1.0" encoding="ISO-8859-1"?> <a> <b i="1" j="aaaa"/> <b i="2" j="bbbb"/> <b...
10
by: Mike Dickens | last post by:
hi, suppose i have: <a> <b i="Y" j="aaaa"/> <c i="N" j="bbbb"/> <d i="Y" j="cccc"/> <e i="N" j="dddd"/> <f i="N" j="eeee"/> <g i="Y" j="ffff"/>
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
1
by: aerotops | last post by:
Hi, I am trying to sort something using XSLT. I am going to give examples. Original.xml <Root> <Car> <Name>Ford</Name> <DealerRating>3</DealerRating>
3
by: aerotops | last post by:
Hi, I am trying to sort some elements. But my key is a node fragment. How can I sort this? Or how can I convert a node fragment to a node set? Thanks, Harsh.
10
by: Jim Andersen | last post by:
I have this xml-file. I want to sort it, and create a new xml-file. The result should be identical to the input except that it's sorted. <?xml version="1.0" encoding="UTF-8"?> <levelone> <child...
1
by: Michael Doubez | last post by:
Hello, I want to mix in the same document Parts that have automatically numbered sections and parts which section are not numbered. Is there any out of the box way to locally suppress the...
2
by: richardkreidl | last post by:
I heard of importing your xml doc into a dataset, then sort the dataset and re-populate your xml, but I'm not sure how to code this or is there another way of sorting a XML file? I not familiar...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.