 |
| 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 18th, 2009, 08:31 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Here is an XSLT 1.0 solution, based on your original problem description:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="root">
<rss>
<xsl:apply-templates select="item"/>
</rss>
</xsl:template>
<xsl:template match="item">
<xsl:copy>
<position>
<xsl:value-of select="count(preceding-sibling::item[data[@name = 'title'] = current()/data[@name = 'title']]) + 1"/>
</position>
<total>
<xsl:value-of select="count(../item[data[@name = 'title'] = current()/data[@name = 'title']])"/>
</total>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The following should have the same result but should be more efficient:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="k1" match="item" use="data[@name = 'title']"/>
<xsl:template match="root">
<rss>
<xsl:apply-templates select="item"/>
</rss>
</xsl:template>
<xsl:template match="item">
<xsl:copy>
<position>
<xsl:value-of select="count(preceding-sibling::item[data[@name = 'title'] = current()/data[@name = 'title']]) + 1"/>
</position>
<total>
<xsl:value-of select="count(key('k1', data[@name = 'title']))"/>
</total>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Last edited by Martin Honnen; August 18th, 2009 at 08:40 AM..
Reason: adding example using key
|
|

August 18th, 2009, 07:33 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Martin
Thank you for your response. I really appriciate it.
However iam not able to save the below code in xml spy.
<xsl:value-of select="count(preceding-sibling::item[data[@name = 'title'] = current()/data[@name = 'title']]) + 1"/>
i suspect on the square backets.syntactically some thing is wrong here.
Thanks
Uday
|
|

August 19th, 2009, 04:44 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
The syntax of
<xsl:value-of select="count(preceding-sibling::item[data[@name = 'title'] = current()/data[@name = 'title']]) + 1"/>
is correct. It compiles fine in Saxon.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 19th, 2009, 06:09 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Quote:
Originally Posted by udaybob
However iam not able to save the below code in xml spy.
<xsl:value-of select="count(preceding-sibling::item[data[@name = 'title'] = current()/data[@name = 'title']]) + 1"/>
i suspect on the square backets.syntactically some thing is wrong here.
|
I tested the posted code successfully so I am not sure why you say XML Spy complains about the syntax.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

August 19th, 2009, 11:25 AM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry guys..My bad. Its working fine.Thank you soo much for your contribution.
Can i stick to this forum for xslt 1.0 quires. Or is this place for only for 2.0 version??
Thanks mhkay and Martin Honnen
Uday
|
|

August 19th, 2009, 11:37 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You're welcome to post XSLT 1.0 question here. But as with any question, there is no guarantee anyone will answer.
(Some of us - I speak for myself - simply don't find it very interesting to solve problems in XSLT 1.0 when the 2.0 solution is so much easier. Sorry, but we can choose which questions we want to answer - it's a service that relies entirely on volunteers.)
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

August 19th, 2009, 01:34 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thank you. I can understand. For now we are working on xslt 1.0. We have plans of migrating to 2.0 in future.
Thanks
Uday
Last edited by udaybob; August 19th, 2009 at 01:36 PM..
|
|
 |