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

<?xml version="1.0"?>


<?xml version="1.0"?>

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

6 Years
Discussion Span
Last Post by iceandrews

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 match="section">
			<xsl:apply-templates select="p"/>

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

It produces the following output using your input sample.


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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.