Is it possible to use XSLT to change part of the element name to another element name with increment number.
Like I only want to change the element name with UPC_ at the beginning
<product>
<UPC_US>123</UPC_US>
<UPC_UK>223</UPC_UK>
<UPC_JP>345</UPC_JP>
<other>unchange</other>
</product>
<product>
<UPC_US>1234</UPC_US>
<UPC_CA>1235</UPC_CA>
<other>unchange</other>
</product>
to this?
<product>
<UPC_1>123</UPC_1>
<UPC_2>223</UPC_2>
<UPC_3>345</UPC_3>
<other>unchange</other>
</product>
<product>
<UPC_1>1234</UPC_1>
<UPC_2>1235</UPC_2>
<other>unchange</other>
</product>
This can be achieved quite easily with XSLT. First of all you would create a template to match elements beginning with UPC
Then you would create a new element, with your revised name, based on the element's position
Note the use of "Attribute Value Templates" here in creating the name. The curly braces indicate this is an expression to be evaluated, not output literally.
Here is the full XSLT
When applied your XML, the following is output
Having said that, it would only work if your UPC elements are always the first elements. If you had this as input
The output would be this
If this is not what you want, you could instead make use of xsl:number to count the elements. Try this XSLT instead
In this case, the second example would yield this
In both cases, make note of the use of the Indentity Transform template to copy all other elements as-is.
Yes it is possible.
xsl:element
allows you to create a new element name dynamically.