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 October 26th, 2009, 11:45 AM
Registered User
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Incrementing a variable in xsl

Hello all.

I know that variables in xsl are immutable and cannot change but i need to work out a way to increment a variable which is then added on as a value to a tag.

As a solution ive read that recursive calls to a template achieve the same goal as incrementing a variable but i am new to xsl and am unsure how to proceed.

The scenario is as follows:

I have an example xml file of the following format :
Code:
<section>
  <field id ="date1">
    <datefield>
  </field>
  <field id ="date2">
    <datefield>
  </field>
  <field id ="Name">
    <textfield>
  </field>
  <field id ="Num1">
    <numberfield>
  </field>.....
</section>
I now need to loop through all fields and if they are a numberfield or datefield add a new element containing a count of the number of occurrences. eg
Code:
<xsl:for-each select="numberfield>
  <countValue>
    <xsl:attribute name="count"> ??incremental value here </xsl:attribute>
</xsl:for-each>
<xsl:for-each select="datefield>
  <countValue>
    <xsl:attribute name="count"> ??incremental value here </xsl:attribute>
</xsl:for-each>
This would theoretically give me the elements

date1 with a countValue of 1
date2 with a countValue of 2
num1 with a countValue of 3

Is this possible?

Many thanks for your anticipated assistance - i hope i have provided enough information to explain the problem.

Will

Last edited by wwilson; October 26th, 2009 at 11:55 AM..
 
Old October 26th, 2009, 11:55 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>but i need to work out a way to increment a variable

No, you don't. You need to find a way of solving your problem that doesn't involve incrementing variables.

>I have an example xml file of the following format

That example isn't well-formed XML

>This would theoretically give me the elements

I can't see how. Your pseudocode surely allocates numbers to the number fields before the date fields, whereas your suggested output is the other way around.

The solution to your problem almost certainly involves xsl:number, but unless you can explain the problem with a bit more attention to detail, I can't be more specific.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old October 26th, 2009, 01:03 PM
Registered User
 
Join Date: Oct 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the prompt reply - i really appreciate it.

Sorry i was trying to be brief in explaining the problem and only providing snippets of the xsl which you are right are not well form but just written to give background to the problem. You are right this may have been too brief.

To provide some more information i have an xml file. This contains a section node under which there are multiple fields elements of different types. As shown by the first code snippet.

I am only interested in numberFields and dateFields. Each occurrence of a numberField or dateField needs to trigger a count.

I then use a generic xml file to process the first xml file using a loop to iterate through each occurrence of a particular field type. For those field types that are numberFields and DateFields i need to add a new element corresponding to a flag to reflect the countValue.

In this way each instance of a numberField and dateField will be allocated a unique numerical flag.

Therefore correcting my original example the correct output should have been :

num1 with a countValue of 1
date1 with a countValue of 2
date2 with a countValue of 3
 
Old October 26th, 2009, 01:29 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I suspect you can use <xsl:number count="numberfield | datefield"/> but the exact pattern depends on your actual XML which you have not shown in a coherent form.

The only slight reservation is that this assumes you are processing the numbers and dates in document order. If you are processing the numbers first, but they appear after the dates, then you may be better off using an expression such as "position() + count(../numberfield)" while processing the dates.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrementing a variable in XSL chandra_perni XSLT 6 July 6th, 2009 07:39 AM
Incrementing the variable value Swetha XSLT 2 April 28th, 2008 04:43 PM
Way to incrementing value of variable in xsl vikkiefd XSLT 14 March 12th, 2008 10:33 PM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 10:41 AM
incrementing value of a variable akibaMaila VB.NET 2002/2003 Basics 4 July 5th, 2005 12:04 PM





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