471,309 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,309 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 1811
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mike Dickens | last post: by
10 posts views Thread by Mike Dickens | last post: by
1 post views Thread by aerotops | last post: by
3 posts views Thread by aerotops | last post: by
10 posts views Thread by Jim Andersen | last post: by
1 post views Thread by Michael Doubez | last post: by
2 posts views Thread by richardkreidl | last post: by

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.