*
The moose likes XML and Related Technologies and the fly likes Counter in XSLT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Counter in XSLT" Watch "Counter in XSLT" New topic
Author

Counter in XSLT

Dhaval Hansaliya
Greenhorn

Joined: Sep 14, 2009
Posts: 8
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 14, 2009
Posts: 8
Hi Paul,

thanks for replay.

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

Eg.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 14, 2009
Posts: 8
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,
 
jQuery in Action, 2nd edition
 
subject: Counter in XSLT
 
Similar Threads
Oracle XML Java Parser problem
Rendering XML in browser
Change XML element using Java
Need help with XSLT
Java XML parser to parse the XML file