I have this source XML:
<?xml version="1.0"?>
<root>
<item1>
<name>test</name>
<price>160</price>
<stock>4</stock>
<country>Belgium</country>
</item1>
<item2>
<name>Alfa</name>
<price>140</price>
<stock>3</stock>
<country>Turkey</country>
</item2>
<item3>
<name>Beta</name>
<price>110</price>
<stock>48</stock>
<country>Holland</country>
</item3>
<product id="p1">
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</product>
<product id="p2">
<name>Golf</name>
<price>1000</price>
<stock>5</stock>
<country>Germany</country>
</product>
<product id="p3">
<name>Alfa</name>
<price>1200</price>
<stock>19</stock>
<country>Germany</country>
</product>
<product id="p4">
<name>Foxtrot</name>
<price>1500</price>
<stock>5</stock>
<country>Australia</country>
</product>
<product id="p5">
<name>Tango</name>
<price>1225</price>
<stock>3</stock>
<country>Japan</country>
</product>
</root>
And I want it to look like this:
<?xml version="1.0"?>
<root>
<action>
<name>test</name>
<price>160</price>
<stock>4</stock>
<country>Belgium</country>
</action>
<action>
<name>Alfa</name>
<price>140</price>
<stock>3</stock>
<country>Turkey</country>
</action>
<action>
<name>Beta</name>
<price>110</price>
<stock>48</stock>
<country>Holland</country>
</action>
<action>
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</action>
<action>
<name>Golf</name>
<price>1000</price>
<stock>5</stock>
<country>Germany</country>
</action>
<action>
<name>Alfa</name>
<price>1200</price>
<stock>19</stock>
<country>Germany</country>
</action>
<action>
<name>Foxtrot</name>
<price>1500</price>
<stock>5</stock>
<country>Australia</country>
</action>
<action>
<name>Tango</name>
<price>1225</price>
<stock>3</stock>
<country>Japan</country>
</action>
</root>
So the <item1>
to <item3
> turn into
<action>
And the <product>
loses its attribute, and also turns into
<action>
Can somebody show me how to do this with XSLT? Because I have 2 different XSL's now. And I want to combine them to create just one XSL.
Thanks in advance
Sounds easy:
If you want us to combine your existing code you need to post it.
This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
The identity rule copies every node "as-is".
We have two templates overriding the identity rule. The first template matches any
product
or any element whose name starts with the stringitem
. It effectively "renames" the matched element toaction
by creating and outputting anaction
element.The second overriding template matches any
id
attribute of anyproduct
element. This template has no body, which effectively "deletes" the matched attribute -- it is not copied/recreated in the output.You could also just step through it and built it however you like. Ex: