Link to home
Start Free TrialLog in
Avatar of gayathri grandhe
gayathri grandhe

asked on

could you please suggest me that in order to repeat elements by using for-each loop instead of giving path in xslt

in order to repeat elements by using for-each loop instead of giving path in xslt
Avatar of mccarl
mccarl
Flag of Australia image

Can you provide some examples of what you are providing as input to the transformation and what you expect as output?
Avatar of gayathri grandhe
gayathri grandhe

ASKER

Normally is there any other way instead of giving path to the for-each loop in xslt.
For Example:In C,C++ there is a for loop in order to repeat the elements such as
 for(i=0;i<3;i++)
{
printf("test");
}
in the above example the printf statement executes 3 times.
like the same way i need to repeat the steps in the xslt by using for-each loop.
could you please provide the solution to that as soon as possible.
You usually get the answers you deserve based on the clarity of the question.
Please, if you ever ask a question again, make sure it can be understood

If you would be using XSLT2 this is fairly easy
        <xsl:for-each select="for $i in 1 to 3 return $i">
            <!-- do something here -->
            <xsl:value-of select="."/>
        </xsl:for-each>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Greet,


I used the logic which you have given. but the following error is generating for me.

Error:Expected token 'eof' found '$'. for -->$<--i in 1 to 2 return $i
 

And i used this piece of code.but the error is generating.

<xsl:for-each select="for $i in 1 to 2 return $i">
 
 <xsl:element name="Policies">
<xsl:attribute name="Update">successfull</xsl:attribute>
<xsl:element name="AssuredRefernceihlp">correct</xsl:element>
<xsl:element name="PoliciesOption">Quote65</xsl:element>
</xsl:element>
 
</xsl:for-each>

could you please suggest me in that.
Please note that I clearly wrote
If you would be using XSLT2 this is fairly easy
Are you using XSLT2?
It is clear that the select in the for each does not accept the '$' so it does not accept an XPath 2.0 expression
so you are obviously not using XSLT
tell us which XSLT processor you are using maybe we can switch that to XSLT2
if not use the recursive solution that works in any case
<xsl:call-template name="iterate">
            <xsl:with-param name="left" select="3"/>
        </xsl:call-template>

I used this solution it is working fine now.

But I have another question  is that is there any possible way "that tags want to be repeated and the values should be different" by using for-each loop in xslt.
I need code for this. could you please assist me.

For example :<insurance>akil</insurance>
<insurance>hari</insurance>
like this tags need to be repeated. But the values must be different.
that it should be done with for-each loop in xslt.
Yes, that is possible
but you need to explain the logic

It sounds as if you should use some sort of lookup XML
<ins>
<in>akil</in>
<in>hari</in>
</ins>
and use XPath on those and forget about the for-each iterator
Your wording of your issues is very vague and underspecified, so I can't comment on the entire project
but I fear you are thinking too proocedural
instead of xpath is there any other possible way
Maybe XPath is the best way,
but you are not answering my questions
but you need to explain the logic

You launch some vague bizarre requirements,
you are not explaining what you need, you are just saying that it has to be without XPath

Unless you explain very clear and in detail what exactly you need,
I will consider this question answered
please accept #a42052333
as the answer and we are done with this
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually i need to do xslt transformation from one xml(1st xml)to another xml(2nd xml) by using xslt.
But the two xml's format are completely different.
but in the second xml the tags (elements)are repeating with different values that tags are not there in 1st xml. so i thought that we cannot use xpath for that.
so, for that purpose iam asking for the logic of the iterators
well, try my latest suggestion
(I don't think you need to do it that way, but let's just try that)
how to retrieve namespace of one xml to another xml by using xslt through xpath
I mean how to extract the namespace of xml by using xslt through xpath. is it is possible?
yes, but that is a completely different question, you should not mix questions, ask a new one and close this one please
It is obvious that the solutions provided were used and should be accepted. They are definitely complete.
The original poster of the question tried to add a completely different questions inside this thread and then ran off... kind of sad