I am trying to convert 1 XML into another and want to increment the variable as many times the loop executes.

XML Code-

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>

Want output as-

<root>
<singer> 
singer no. 1 
[B]<!--want this no. to be changed as many times the loop executes-->[/B]<title>Empire Burlesque</title> 
</singer>
<singer> 
singer no. 2
<!--not able to get this value through variable-->
<title>Hide your heart</title>
</singer>
</root>
XSLT-
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="num">1</xsl:variable>
<xsl:template match="/">
  <root>
      <xsl:apply-templates/>
  </root>
</xsl:template>

<xsl:template match="catalog">
  <xsl:for-each select="cd">
     <singer>
            singer no. <xsl:value-of select="$num">
                 <xsl:copy-of select="title"/>
      </singer>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

I have no clue how i could i do this. Please advise.

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Have got the solution for this particular problem by using 'position()', however the purpose is yet not solved as i want the position of the parent element in the child element.
solution-

<xsl:template match="catalog">
        <xsl:for-each select="cd">
            <singer>
               singer no. <xsl:value-of select="position()"/>
                <xsl:copy-of select="title"/>
            </singer>
        </xsl:for-each>
    </xsl:template>

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

XSL variables are immutable. You cannot change the value of a variable once it is set.

If I understand your requirements correctly you are trying to number a simple list. If this is the case, try the following.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<root>
<xsl:apply-templates/>
</root>
</xsl:template>

<xsl:template match="cd">
<singer>
singer no. <xsl:number />
<xsl:copy-of select="./title"/>
</singer>
</xsl:template>

</xsl:stylesheet>

U have to use recursion for this task. Give me an instance and i'll solve that problem.... :)

This question has already been answered. Start a new discussion instead.