Hi,

i want to renaming tag in xml file with using xslt.
Can anybody help me.

<?xml version="1.0"?>
<section>
   <p>1</p>
   <p>2</p>
   <p>3</p>
   <p>4</p>
 </section>

OUTPUT:-

<?xml version="1.0"?>
   <section>
    <paraone>1</paraone>
    <paratwo>2</paratwo>
    <parathree>3</parathree>
    <parafour>4</parafour>
</section>

not going to work like that, you don't want to "rename a tag" (you won't do that anyway, you'd map one tag in the input to another in the output) but map tags to different tags based on the order in the source document.
As there is no hieratchical order guaranteed by the xml standard, that mapping cannot be created.
In other words, there's no guarantee that on successive parsings of your source you'd get the <p> tags in the order 1,2,3,4 each time.

The above poster isn't EXACTLY correct. This quote "no hieratchical order guaranteed by the xml standard" isn't quite true. Document order is maintained and the order of those elements is maintained in the tree structure when it is parsed.

Using things like the "position()" function you can get back the "order" of a particular set of nodes. This could be used to dynamically generate names for elements in the ouput tree. While I don't see a good way of created nodes like "pone, ptwo" you can create names that give them an order.

The following XSLT 1.0 will rename the <p> elements based on their relative position to the <section> node.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

	<xsl:template match="section">
		<xsl:copy>
			<xsl:apply-templates select="p"/>
		</xsl:copy>
	</xsl:template>

	<xsl:template match="p">
		<xsl:element name="{concat(local-name(.), position())}" >
			<xsl:value-of select="." />
		</xsl:element>			
	</xsl:template>
</xsl:stylesheet>

It produces the following output using your input sample.

<section>
	<p1>1</p1>
	<p2>2</p2>
	<p3>3</p3>
	<p4>4</p4>
</section>

While this isn't exactly what you wanted, it does demonstrate the ability to use the position of the element nodes in the input tree to determine something in the output tree.

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.