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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top