Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > loop in xslt

Reply
Thread Tools

loop in xslt

 
 
Eyal
Guest
Posts: n/a
 
      04-15-2004
Hello,
I am new to xslt and try to have an argument that for each iteration
of the "FOR-EACH" will grow in one:

<xsl:for-each select="//AssetCode">
<node id="_1">
<xsl:attribute name="text">
<xsl:value-of select="."/>
</xsl:attribute>
</node>


I want id to be in the next iteration _2 and _3 in the next one
etc....

any ideas of how to do it?
 
Reply With Quote
 
 
 
 
Gadrin77
Guest
Posts: n/a
 
      04-16-2004
(Eyal) wrote in message news:<. com>...
> Hello,
> I am new to xslt and try to have an argument that for each iteration
> of the "FOR-EACH" will grow in one:
>
> <xsl:for-each select="//AssetCode">
> <node id="_1">
> <xsl:attribute name="text">
> <xsl:value-of select="."/>
> </xsl:attribute>
> </node>
>
>
> I want id to be in the next iteration _2 and _3 in the next one
> etc....
>
> any ideas of how to do it?


I learned a lot from this article.

http://www.xml.com/pub/a/2001/08/01/gettingloopy.html
 
Reply With Quote
 
 
 
 
Ben Edgington
Guest
Posts: n/a
 
      04-16-2004
(Eyal) writes:

> Hello,
> I am new to xslt and try to have an argument that for each iteration
> of the "FOR-EACH" will grow in one:
>
> <xsl:for-each select="//AssetCode">
> <node id="_1">
> <xsl:attribute name="text">
> <xsl:value-of select="."/>
> </xsl:attribute>
> </node>
>
>
> I want id to be in the next iteration _2 and _3 in the next one
> etc....
>
> any ideas of how to do it?


Use the XPath position() function.

These data

<test>
<AssetCode>abc</AssetCode>
<AssetCode>def</AssetCode>
<AssetCode>ghi</AssetCode>
<AssetCode>jkl</AssetCode>
</test>

with a very slight modification to your stylesheet:

<xsl:stylesheet
version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform"
>


<xsl:template match="/">
<xsl:for-each select="//AssetCode">
<node id="_{position()}">
<xsl:attribute name="text">
<xsl:value-of select="."/>
</xsl:attribute>
</node>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


Gives this output

<?xml version="1.0"?>
<node id="_1" text="abc"/><node id="_2" text="def"/>
<node id="_3" text="ghi"/><node id="_4" text="jkl"/>


Ben

--
Ben Edgington
Mail to the address above is discarded.
Mail to ben at that address might be read.
www.edginet.org
 
Reply With Quote
 
Eyal
Guest
Posts: n/a
 
      04-16-2004
Thanks Ben, It works great.

Ben Edgington <> wrote in message news:<>...
> (Eyal) writes:
>
> > Hello,
> > I am new to xslt and try to have an argument that for each iteration
> > of the "FOR-EACH" will grow in one:
> >
> > <xsl:for-each select="//AssetCode">
> > <node id="_1">
> > <xsl:attribute name="text">
> > <xsl:value-of select="."/>
> > </xsl:attribute>
> > </node>
> >
> >
> > I want id to be in the next iteration _2 and _3 in the next one
> > etc....
> >
> > any ideas of how to do it?

>
> Use the XPath position() function.
>
> These data
>
> <test>
> <AssetCode>abc</AssetCode>
> <AssetCode>def</AssetCode>
> <AssetCode>ghi</AssetCode>
> <AssetCode>jkl</AssetCode>
> </test>
>
> with a very slight modification to your stylesheet:
>
> <xsl:stylesheet
> version="1.0"
> xmlnssl="http://www.w3.org/1999/XSL/Transform"
> >

>
> <xsl:template match="/">
> <xsl:for-each select="//AssetCode">
> <node id="_{position()}">
> <xsl:attribute name="text">
> <xsl:value-of select="."/>
> </xsl:attribute>
> </node>
> </xsl:for-each>
> </xsl:template>
>
> </xsl:stylesheet>
>
>
> Gives this output
>
> <?xml version="1.0"?>
> <node id="_1" text="abc"/><node id="_2" text="def"/>
> <node id="_3" text="ghi"/><node id="_4" text="jkl"/>
>
>
> Ben

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57