I have an XML document describing a car mart. The car mart is a collectiion of parkings. In each parking, there are many cars. Each car belongs to one category - new/used/rent. The document looks like this:
<car_mart>
<parking>
<cars>
<car cat="new">
<licence>1PYV582</licence>
<price>1000</price>
</car>
<car cat="new">
<licence>6GD5489</licence>
<price>2200</price>
</car>
<car cat="rent">
<rental>20</rental>
</car>
<car cat="new">
<licence>5FN1254</licence>
<price>1500</price>
</car>
<car cat="rent">
<rental>25</rental>
</car>
<car cat="new">
<licence>6HB4524</licence>
<price>5800</price>
</car>
<car cat="rent">
<rental>10</rental>
</car>
</cars>
</parking>
<parking>
<cars>
<car cat="used">
<licence>8UA1294</licence>
<price>800</price>
</car>
</cars>
</parking>
<!--<parking>
...
<parking>-->
<cat tag="new">
<cat_name>New</cat_name>
</cat>
<cat tag="used">
<cat_name>Used</cat_name>
</cat>
<cat tag="rent">
<cat_name>Rent</cat_name>
</cat>
</car_mart>
I would like to extract information using XQuery. The result should look like this:
<cat id="new" total_cost="10500">
<cat_name>New</cat_name>
<car price="1000"/>
<car price="2200"/>
<car price="1500"/>
<car price="5800"/>
</cat>
<cat id="used" total_cost="800">
<cat_name>Used</cat_name>
<car price="800"/>
</cat>
<cat id="rent" total_cost="55">
<cat_name>Rent</cat_name>
</cat>
Till now, I came to this piece of XQuery (3.0). The output is obviously not correct.
<car_list>{
for $cat in /car_mart/cat
return
<cat id="{$cat/@tag}" total_cost="{sum(/car_mart/parking/cars/car/cost)}">
{$cat/cat_name}
{for $car in /car_mart/parking/cars/car
return
if($car/[@cat=$cat/@tag])
then <car price="{$car/price}"></car>
else ()
}
</cat>
}</car_list>
I see 2 main problems that I do not know how to resolve:
in one category, only cars of that category should be listed (now, all of them are)
counting total_sum of that category
Thank you very much