1

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:

  1. in one category, only cars of that category should be listed (now, all of them are)

  2. counting total_sum of that category

Thank you very much

1 Answer 1

1

There are multiple problems in your query:

  1. You sum over the cost of all cars in the database, not the ones in each category.

  2. There is no element named cost in any car, only price and rental.

  3. Your syntax for the predicate in if($car/[@cat=$cat/@tag]) is wrong, the first forward slash / should not be there.

The if construct can also be replaced by a single predicate. Changing all of this leads to the following query:

<car_list>{
  for $cat in /car_mart/cat
  let $cars-in-cat := /car_mart/parking/cars/car[@cat=$cat/@tag]
  return <cat id="{$cat/@tag}" total_cost="{sum($cars-in-cat/(price|rental))}">{
    $cat/cat_name,
    for $car in $cars-in-cat[price]
    return <car price="{$car/price}"></car>
  }</cat>
}</car_list>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot. 3rd problem helped (such a little thing and it can change a lot...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.