sort and numbering - xslt

pstachy

Usenet Archive
Joined
Nov 5, 2006
Messages
7
Reaction score
0
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....
 

xmlator

Usenet Archive
Joined
Nov 26, 2006
Messages
7
Reaction score
0
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
 

pstachy

Usenet Archive
Joined
Nov 5, 2006
Messages
7
Reaction score
0
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):
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
pstachy said:
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:eek:utput 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.
 

Dimitre Novatchev

Usenet Archive
Joined
Nov 7, 2000
Messages
737
Reaction score
0
pstachy said:
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 <li> elements within an <ol> element.

Cheers,
Dimitre Novatchev
 

pstachy

Usenet Archive
Joined
Nov 5, 2006
Messages
7
Reaction score
0
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 <li> idea. 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):
 

xmlator

Usenet Archive
Joined
Nov 26, 2006
Messages
7
Reaction score
0
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top