loop in xslt

E

Eyal

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?
 
G

Gadrin77

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
 
B

Ben Edgington

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
 
E

Eyal

Thanks Ben, It works great.

Ben Edgington said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top