Solved

Very Urgent: increment variable in XSL

Posted on 2006-11-09
5
2,756 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
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 50 total points
Comment Utility
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 50 total points
Comment Utility
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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 Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now