Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Very Urgent: increment variable in XSL

Posted on 2006-11-09
5
Medium Priority
?
2,796 Views
Last Modified: 2010-05-19
Dear Friends,

Could you please help me to get the solution for the problem?

I do have one xsl file and need to have generate number and assign the value to a an attribute (id)in the (Element) tag. Please find the peace of code.

<xsl:variable name="a">1</xsl:variable>
<xsl:for-each select="sss/survey/record/variable">
<xsl:if test="@type='single'">
                                    
<Element CompoundQuestionType="qSP_vertical" DataType="LIST" DisplayListComment="false" ElementType="Question" Format="" Grouping="None" HideQuestionLabel="false" Hidden="false" Enabled="true" id="" Name="{./@ident}" >
                                          
<xsl:attribute name="id">
<xsl:value-of select="$a+1"/>
</xsl:attribute>

I tried the above code but its writting 2 only to all the "id" attribute values in the all Element tags.I should get id values like 1,2,3...... for different Element tag.

Please help me to figure out this issue. It very urgent

Many Thanks,
Asish
0
Comment
Question by:mgminfotech
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 200 total points
ID: 17904823
Hi Asish,

Variables in XSLT are constants, you can't change them

There are basically two options to achieve what you need
1. use position() or count(preceding-sibling::variable) to get the number
2. call a template recursively and pass the parameter with a new value to the template

The second is the more generic solution.
The first has the least impact on your current solution and would work here I believe

Try this
<xsl:attribute name="id">
<xsl:value-of select="position()"/>
</xsl:attribute>


Cheers!
0
 
LVL 12

Assisted Solution

by:enachemc
enachemc earned 200 total points
ID: 17920650
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 17920912
enachemc,

I have some observations with your suggestion

- saxon:assignable is a feature only supported by one specific XSLT processor (Saxon)
- the Saxon developer, Michael Kay, is not particulary fond of this extension and thinks about dropping it in future releases (see his blog or books)
- adding features that make templates not side effect free add error risks to XSLT code

I have never found a real need for saxon:assignable, even when using Saxon
I always found an acceptable workaround,
one that is acceptable because it adds robustness to the code and keeps processor independence

Having said that, the need for an assignable variable can be replaced by the simple use of position() in this case

Cheers

Geert
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question