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

August 16th, 2009, 03:32 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
GETTING COUNT AND POSITION using XSLT
Hi Guys,
Here is my requirement.
sample XML
<root>
<item>
...
<data name="x">...</data>
<data name="title">uday</data>
<data name="y">...</data>
....
</item>
<item>
...
<data name="x">...</data>
<data name="title">uday</data>
<data name="y">...</data>
....
</item>
<item>
...
<data name="x">...</data>
<data name="title">bhaskar</data>
<data name="y">...</data>
....
</item>
<item>
...
<data name="x">...</data>
<data name="title">bhaskar</data>
<data name="y">...</data>
....
</item>
<item>
...
<data name="x">...</data>
<data name="title">bhaskar</data>
<data name="y">...</data>
....
</item>
Desired o/p XML
<rss>
<item>
..
<position>1</position>
<total>2</total>
....
</item>
<item>
..
<position>2</position>
<total>2</total>
....
</item>
<item>
..
<position>1</position>
<total>3</total>
....
</item>
<item>
..
<position>2</position>
<total>3</total>
....
</item>
<item>
..
<position>3</position>
<total>3</total>
....
</item>
</rss>
for each item in the input xml, we get item in the output.
total represents the no. of items with the same title value
position represents the position of the item with a paticular title.
Iam using xslt 1.0
Thanks you in advance
Uday
|
|

August 16th, 2009, 06:08 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
First here is the XSLT 2.0 solution:
Code:
<xsl:for-each-group select="item" group-adjacent="data[@name='title']">
<xsl:for-each select="current-group()">
<item>
<position><xsl:value-of select="position()"/></position>
<count><xsl:value-of select="count(current-group())"/></count>
</item>
</xsl:for-each>
</xsl:for-each-group>
I hope this will persuade you that moving to XSLT 2.0 is a good idea if you possibly can.
I'm not going to attempt a 1.0 solution, it's too late in the evening, and frankly, coding this in 1.0 is not much fun. You basically need to have a recursive template that outputs one item and then calls itself to process the next, passing the current title and position as parameters. That part is fairly easy, the difficult bit is the count, where you need to "look ahead" in the recursion to see how many following items there are with the same key.
I'm assuming in both solutions that items with the same key are always adjacent in the input sequence.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 17th, 2009, 02:14 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you very much. I tried your solution.Its working while seeing the output in the XML SPY since iam keeping the version as 2.0
i.e <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
since we are using xalan i should be doing the same thing in xslt 1.0.
|
|

August 17th, 2009, 02:27 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>since we are using xalan i should be doing the same thing in xslt 1.0.
Shouldn't that be:
"Since this requires XSLT 2.0 we should not be using Xalan"?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 17th, 2009, 03:53 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I don't know the exact reason,but soo far we developed all the xslt's in 1.0
When i tried to run the 2.0 version xslt, i got the below problem.
SystemId Unknown; Line #63; Column #44; Could not find function: current-group
SystemId Unknown; Line #63; Column #44; function token not found.
(Location of error unknown)java.lang.NullPointerException
So i came to conclusion that my java api which is responsible for transforming to XML is expecting 1.0 version.
Thanks
Uday
|
|

August 17th, 2009, 04:25 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I mean to say the the java API build to transform the xml is based on XALAN. And it doesn't recognize the xslt 2.0 functions. Sorry if iam not giving the proper explanation.
Thanks
Uday
|
|

August 17th, 2009, 05:13 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Yes, it looks as if you are running an XSLT 1.0 processor, probably Xalan. You need to run with a 2.0 processor, which in effect means Saxon.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 17th, 2009, 09:54 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Can you please help me out in writing the same code in 1.0 version? because i can not upgrade to 2.0 rightnow in my production environment.
|
|

August 18th, 2009, 03:42 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
No, sorry. I find writing XSLT 1.0 code really boring.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 18th, 2009, 07:57 AM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Its ok..no problem. Thank you for prompt replies.
Thanks a lot
Uday
|
|
 |