loop in xslt

Discussion in 'XML' started by Eyal, Apr 15, 2004.

  1. Eyal

    Eyal Guest

    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?
     
    Eyal, Apr 15, 2004
    #1
    1. Advertising

  2. Eyal

    Gadrin77 Guest

    (Eyal) wrote in message news:<>...
    > 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
     
    Gadrin77, Apr 16, 2004
    #2
    1. Advertising

  3. (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"
    xmlns:xsl="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
     
    Ben Edgington, Apr 16, 2004
    #3
  4. Eyal

    Eyal Guest

    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"
    > xmlns:xsl="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
     
    Eyal, Apr 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    735
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,730
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    647
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    733
    Henry S. Thompson
    Oct 19, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    419
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page