I have an XML file that I need to modify. First I need to make a group, and then exclude a couple of nodes.
<cars>
<car>
<make>Toyota</make>
<model>Camry</model>
<color>White</color>
<price>123</price>
</car>
<car>
<make>Honda</make>
<model>Accord</model>
<color>White</color>
<price>423</price>
</car>
</cars>
Here is my code that does the conversion:
<root>
{
for $color in distinct-values(doc('cars.xml')//cars/car/color)
let $car := doc('cars.xml')//cars/car
return <appearance color="{$color}">
{ $car[color eq $color ] }
</appearance>
}
</root>
I get:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<appearance color="White">
<car>
<make>Toyota</make>
<model>Camry</model>
<color>White</color>
<price>123</price>
</car>
<car>
<make>Honda</make>
<model>Accord</model>
<color>White</color>
<price>423</price>
</car>
</appearance>
</root>
This is 95% of what I need, except for one problem. I need to exclude the color and price nodes from the final XML, while preserving the grouping.
I tried to do the following in my return expression: {$ car [color eq $ color] / * [not (self :: price)] [not (self :: color)]}
but he completely eliminates the element "car"! Any tips?
source
share