Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old August 18th, 2009, 08:31 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

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
 
Old August 18th, 2009, 07:33 PM
Registered User
 
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old August 19th, 2009, 04:44 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old August 19th, 2009, 06:09 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Quote:
Originally Posted by udaybob View Post
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
 
Old August 19th, 2009, 11:25 AM
Registered User
 
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

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
 
Old August 19th, 2009, 11:37 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old August 19th, 2009, 01:34 PM
Registered User
 
Join Date: Aug 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
count in XSLT, Need Help Please !!! techza XSLT 0 December 15th, 2008 01:35 AM
XSL: Count = Count + 1 elayaraja.s XSLT 3 July 18th, 2008 03:21 AM
XSLT count() sani723 XSLT 2 March 26th, 2007 08:55 AM
is there any in built function to count page count g.tamilselvan MySQL 1 February 15th, 2006 07:43 AM
Count, sum, count a value, return records CongoGrey Access 1 April 18th, 2005 02:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.