Re: A Counter Variable in XSLT?
Grant, Without seeing more of your problem context, I can't say for sure, but you might consider some options before resorting to mutable variables. 1. Will <xsl:number> suffice? See http://www.w3.org/TR/xslt#number. 2. If <xsl:number> won't do, and you are counting nodes, or any characteristics that can be determined about nodes, you might be able to use the count() function on a suitable node-set (specified with an appropriate XPath expression). Or in some contexts you can use the position() function. 3. If neither of options 1 or 2 is suitable, you can probably count what you want by using a recursive named template. I won't give an example here, since I don't know if it is appropriate for your problem. But this is about the only way you can do something like: $matchNumber = $matchNumber + 1 (without using extensions). I can answer the first part of your question: no, variable bindings in XSLT are scoped to the following siblings (and their descendants) of the element in which they are "declared" with <xsl:variable>. Once declared, the values cannot be changed. You could have identically named variables in every template in your stylesheet if you wanted, and they would all be distinct. You can have top-level bindings that are visible everywhere, but the values can't be changed anywhere else. The particulars are presented succinctly, but completely, at http://www.w3.org/TR/xslt#variables. As for extensions that allow mutable variable bindings, I'm sure others on the list can speak about that. I've never encountered a transformation problem that required it, though. Hope this helps. Paul Tyson, Principal Consultant Precision Documents paul@xxxxxxxxxxxxxxxxxxxxxx http://precisiondocuments.com "The art and science of document engineering." ----- Original Message ----- From: "Grant Mc Auley" <gmcauley@xxxxxxxxxxx> To: "Xsl-List@Lists. Mulberrytech. Com" <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> Sent: Thursday, October 04, 2001 5:55 PM Subject: A Counter Variable in XSLT? > Greetings, > > I want to create a "counter variable" - one that I can increment > within a stylesheet. Something like: > > <xsl:variable name="matchNumber" select="'0'"/> > > (and later...) > > <xsl:value-of select="$matchNumber++"/> > > I know $matchNumber++ won't work, but is there anyway to do this > in XSLT, or do I have to use an extension? (I am currently using > Xalan) > > Thank you, > > Grant Mc Auley > > > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list > XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format