Solved

Very Urgent: increment variable in XSL

Posted on 2006-11-09
5
2,762 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
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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
router and equipment 3 79
Merge 2 XML Files together 5 46
Convert Oracle data into XML document 2 54
XML Data Missing in PHP SimpleXML 8 44
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, …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

937 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

4 Experts available now in Live!

Get 1:1 Help Now