Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Very Urgent: increment variable in XSL

Posted on 2006-11-09
5
Medium Priority
?
2,785 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

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…
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, …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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