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.

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>

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.... :)

I have a similar problem - any help here please
Here's my XML

<root>
<node>
<empno>1</empno>
<name>ABC</name>
<DOB>01/01/22</DOB>
</node>
<node>
<empno>2</empno>
<name>DEF</name>
<DOB>02/02/21</DOB>
</node>
<node>
<empno>3</empno>
<name>GHI</name>
<DOB>03/03/21</DOB>
</node>
</root>

I want to get an output in XML format like this:
<root>
<node>
<empno>1</sno>
<name>ABC</name>
<DOB>01/01/22</DOB>
</node>
<node>
<empno>2</empno>
<name>GHI</name>
<DOB>03/03/21</DOB>
</node>
</root>

Due to some reason i do not want to print the DEF - in my output but the other two should come but with the <empno> in the correct sequence. any help here will be much appreciated.
Thanks
Bala.

commented: Your post is buried under 11 years of discussion. Avoid that and make new posts if you want to discuss. +16
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.