sort and numbering - xslt

P

pstachy

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

xmlator

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
 
P

pstachy

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):
 
M

Martin Honnen

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

Dimitre Novatchev

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
 
P

pstachy

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):
 
X

xmlator

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top