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
gayathri grandheAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
Can you provide some examples of what you are providing as input to the transformation and what you expect as output?
0
gayathri grandheAuthor 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.
0
Gertone (Geert Bormans)Information ArchitectCommented:
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

0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Gertone (Geert Bormans)Information ArchitectCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gayathri grandheAuthor 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.
0
Gertone (Geert Bormans)Information ArchitectCommented:
Please note that I clearly wrote
If you would be using XSLT2 this is fairly easy
Are you using XSLT2?
0
Gertone (Geert Bormans)Information ArchitectCommented:
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
0
gayathri grandheAuthor 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.
0
Gertone (Geert Bormans)Information ArchitectCommented:
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
0
gayathri grandheAuthor Commented:
instead of xpath is there any other possible way
0
Gertone (Geert Bormans)Information ArchitectCommented:
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
0
Gertone (Geert Bormans)Information ArchitectCommented:
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
0
gayathri grandheAuthor 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
0
Gertone (Geert Bormans)Information ArchitectCommented:
well, try my latest suggestion
(I don't think you need to do it that way, but let's just try that)
0
gayathri grandheAuthor Commented:
how to retrieve namespace of one xml to another xml by using xslt through xpath
0
gayathri grandheAuthor Commented:
I mean how to extract the namespace of xml by using xslt through xpath. is it is possible?
0
Gertone (Geert Bormans)Information ArchitectCommented:
yes, but that is a completely different question, you should not mix questions, ask a new one and close this one please
0
Gertone (Geert Bormans)Information ArchitectCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XSLT

From novice to tech pro — start learning today.