Copy XML replacing selected node contents in XSLT (2.0)
Posted: Tue Dec 11, 2007 2:09 pm
I'm wondering if there is a way in XSLT (2.0 is fine) to copy some part of XML replacing some node values at the same time. Here is an example to visualize the matter:
<invoice>
[...]
<apples>
<apple id=1"><price>100</price></apple>
<apple id=2"><price>120</price></apple>
<apple id=3"><price>90</price></apple>
</apples>
<update-prices>
<modification><price apple-id="1">112</price></modification>
<modification><price apple-id="2">132</price></modification>
<modification><price apple-id="3">102</price></modification>
</update-prices>
[...]
</invoice>
So, I would like to prepare an XSLT stylesheet which copies the whole <invoice> element with its children, but replacing the apple prices [isn't $100 for an apple high enough? damn inflation...] with new prices that are included in the <update-prices> element.
It would be nice to delete the <update-prices> element in the resulting document as well.
So the result XML should look like this:
<invoice>
[...]
<apples>
<apple id=1"><price>112</price></apple>
<apple id=2"><price>132</price></apple>
<apple id=3"><price>102</price></apple>
</apples>
[...]
</invoice>
I would not like to map every possible node that can appear in the XML file accordning to the schema. I would just like to copy and paste the XML replacing some of the values using a pattern. Is there a non-hacker way to do this in XSLT?
<invoice>
[...]
<apples>
<apple id=1"><price>100</price></apple>
<apple id=2"><price>120</price></apple>
<apple id=3"><price>90</price></apple>
</apples>
<update-prices>
<modification><price apple-id="1">112</price></modification>
<modification><price apple-id="2">132</price></modification>
<modification><price apple-id="3">102</price></modification>
</update-prices>
[...]
</invoice>
So, I would like to prepare an XSLT stylesheet which copies the whole <invoice> element with its children, but replacing the apple prices [isn't $100 for an apple high enough? damn inflation...] with new prices that are included in the <update-prices> element.
It would be nice to delete the <update-prices> element in the resulting document as well.
So the result XML should look like this:
<invoice>
[...]
<apples>
<apple id=1"><price>112</price></apple>
<apple id=2"><price>132</price></apple>
<apple id=3"><price>102</price></apple>
</apples>
[...]
</invoice>
I would not like to map every possible node that can appear in the XML file accordning to the schema. I would just like to copy and paste the XML replacing some of the values using a pattern. Is there a non-hacker way to do this in XSLT?