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

gayathri grandhe
gayathri grandhe used Ask the Experts™
on
in order to repeat elements by using for-each loop instead of giving path in xslt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
Can you provide some examples of what you are providing as input to the transformation and what you expect as output?

Author

Commented:
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.
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
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

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Information Architect
Top Expert 2006
Commented:
If you have to use XSLT 1.0,
- you could rely on the node-set extension function and have a piece of XML with a number of child elements required for the iteration
- or in plain vanilla XSLT, use recursion

call a template
        <xsl:call-template name="iterate">
            <xsl:with-param name="left" select="3"/>
        </xsl:call-template>

Open in new window


and have a recursive template like this
    <xsl:template name="iterate">
        <xsl:param name="left" select="0"/>
        <!-- do something here -->
        <xsl:text>printf.</xsl:text>
        <xsl:if test="$left > 1">
            <xsl:call-template name="iterate">
                <xsl:with-param name="left" select="$left - 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

Open in new window

Author

Commented:
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.
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
Please note that I clearly wrote
If you would be using XSLT2 this is fairly easy
Are you using XSLT2?
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
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

Author

Commented:
<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.
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
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

Author

Commented:
instead of xpath is there any other possible way
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
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
Gertone (Geert Bormans)Information Architect
Top Expert 2006
Commented:
Just guessing here...
maybe this is what you need

    <xsl:template name="iterate">
        <xsl:param name="left" select="0"/>
        <!-- do something here -->
        <xsl:choose>
            <xsl:when test="$left = 3">
                <insurance>akil</insurance>
            </xsl:when>
            <xsl:when test="$left = 2">
                <insurance>hari</insurance>                
            </xsl:when>
        </xsl:choose>
        <xsl:if test="$left > 1">
            <xsl:call-template name="iterate">
                <xsl:with-param name="left" select="$left - 1"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>

Open in new window


It is a bad idea, not flexible, error prone and more like java than like XSLT
but hey, if this is what you think you need, give it a go

Author

Commented:
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
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
well, try my latest suggestion
(I don't think you need to do it that way, but let's just try that)

Author

Commented:
how to retrieve namespace of one xml to another xml by using xslt through xpath

Author

Commented:
I mean how to extract the namespace of xml by using xslt through xpath. is it is possible?
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
yes, but that is a completely different question, you should not mix questions, ask a new one and close this one please
Gertone (Geert Bormans)Information Architect
Top Expert 2006

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial