Solved

Very Urgent: increment variable in XSL

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML filtering Windows Event Viewer 10 55
How to use xmlReader and idatareader  ? 4 56
XSLT XML 4 19
what are list of ebay api errors 1 19
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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