Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Counter in XSLT

 
Dhaval Hansaliya
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,

i have created a simple XSLT file which does XML-to-XML transformation.

Questions:
- i want to declare a counter which will be used to assign sr. number to every record in 001, 002, 003... format. How can i declare such counter with 00[0-9] format?
- i want to remove leading 0s from one of the int field. like 003045 should be converted to 3045.

i have tried to use xPath functions after adding http://www.w3.org/2005/xpath-functions namespace to my XSLT, but it gives an error that such function cannot be found.

kindly share your suggestions.

thanks,
 
Paul Clapham
Sheriff
Pie
Posts: 20946
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XSLT is a functional language, not a procedural language, so you can't declare a counter. You can use xsl:number to get the position of the current node in its parent, if that helps.

You can coerce a string to a number by using the XPath number() function.

You can't use XPath 2.0 functions unless you are using a transformer which supports XSLT 2.0. Which you most likely aren't.

 
Dhaval Hansaliya
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

thanks for replay.

But, i need to implement ++ operator kind of functionality in XSLT.

Eg.
 
Paul Clapham
Sheriff
Pie
Posts: 20946
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Advice: stop thinking about procedural languages when you're coding XSLT. It's just going to confuse you and lead you into trying things which can't be done. It's a waste of time.

For example in this case you're thinking about "i++" which can't be done in functional languages and that is preventing you from getting to the position() function, which is what you actually want. Get yourself some tutorials and books about XSLT, read examples until you can detox yourself from this procedural obsession.
 
Dhaval Hansaliya
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right Paul.

I am a Java/J2EE developer and it is difficult for me to think in Functional Language style in initial learning .
I have found out the usage of position() function to find the counts using below code.



thanks,
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic