Thank you jkmyoung. I have applied your idea to what I want to do, but when I have more elements, the xsl:variable value did not not change, so I got the wrong result. Please see below for more information what I've done.
Source XML.
<Delivery>
<ShipTo>
<Name>Jame Good</Name>
<Name>A Com.</Name>
<Address>A Street</Address>
<Address>A City</Address>
</ShipTo>
<BillTo>
<Name>Bill Good</Name>
<Name>B Com.</Name>
<Address>B City</Address>
<Address>B City</Address>
<Address>B State</Address>
</BillTo>
</Delivery>
Existing XSL.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Delivery">
<Delivery>
<xsl:apply-templates/>
</Delivery>
</xsl:template>
<xsl:template match="//*/Name">
<xsl:variable name="newname">
<xsl:choose>
<xsl:when test="//ShipTo/Name">
<xsl:text>ShipToName</xsl:text>
</xsl:when>
<xsl:when test="//BillTo/Name">
<xsl:text>BillToName</xsl:text>
</xsl:when>
</xsl:choose>
<xsl:number count="Name"/>
</xsl:variable>
<xsl:element name="{$newname}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="//*/Address">
<xsl:variable name="newname">
<xsl:choose>
<xsl:when test="//ShipTo/Address">
<xsl:text>ShipToAddress</xsl:text>
</xsl:when>
<xsl:when test="//BillTo/Address">
<xsl:text>BillToAddress</xsl:text>
</xsl:when>
</xsl:choose>
<xsl:number count="Address"/>
</xsl:variable>
<xsl:element name="{$newname}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Existing result which is still wrong.
<?xml version="1.0" encoding="UTF-8"?>
<Delivery>
<ShipToName1>Jame Good</ShipToName1>
<ShipToName2>A Com.</ShipToName2>
<ShipToAddress1>A Street</ShipToAddress1>
<ShipToAddress2>A City</ShipToAddress2>
<ShipToName1>Bill Good</ShipToName1> * the name did not change to BillToName1
<ShipToName2>B Com.</ShipToName2> * the name did not change to BillToName2
<ShipToAddress1>B Street</ShipToAddress1> * the name did not change to BillToAddress1
<ShipToAddress2>B City</ShipToAddress2> * the name did not change to BillToAddress2
<ShipToAddress3>B State</ShipToAddress3> * the name did not change to BillToAddress3
</Delivery>
The required result XML
<Delivery>
<ShipToName1>Jame Good</name>
<ShipToName2>A Com.</name>
<ShipToAddress1>A Street</ShipToAddress1>
<ShipToAddress2>A City</ShipToAddress2>
<BillToName1>Bill Good</BillToName1>
<BillToName2>Bill Good</BillToName2>
<BillToAddress1>B Com.</BillToAddress1>
</Delivery>
Can anyone suggest me how to fix the xsl to get the result as requirement please. Thank you advance.