#  DC/SD_Q1 Return the item that has matching item id attribute value (I1).

for $item in input()/catalog/:item[@id="I1"]
return
    $item


# DC/SD_Q2 Find the title of the item which has matching author first name (Ben).

for $item in input()/catalog/:item
where $item/authors/author/name/first_name = "Ben"
return
    $item/title


# DC/SD_Q3 Group items released in a certain year (1990), by publisher name and calculate the total number of items for each group.

for $a in distinct-values (input()/catalog/:item
    [date_of_release >= "1990-01-01"]
    [date_of_release < "1991-01-01"]/publisher/name)
let $b := input()/catalog/:item/publisher[name=$a]
return
    <Output>
        <Publisher>{$a/text()}</Publisher>
        <NumberOfItems>{count($b)}</NumberOfItems>
    </Output>

# DC/SD_Q4 List the item id of the previous item of a matching item with id attribute value (I2).

let $item := input()/catalog/:item[@id="I2"]
for  $prevItem in input()/catalog/:item
    [. << $item][position() = last()]
return
    <Output>
        <CurrentItem>{$item/@id}</CurrentItem>
        <PreviousItem>{$prevItem/@id}</PreviousItem>
    </Output>

# DC/SD_Q5 Return the information about the first author of item with a matching id attribute value (I3).

for $a in input()/catalog/:item[@id="I3"]
return
    $a/authors/author[1]


# DC/SD_Q6 Return item information where some authors are from certain country (Canada).

for $item in input()/catalog/:item
where some $auth in
    $item/authors/author/contact_information/mailing_address
satisfies $auth/name_of_country = "Canada"
return
    $item


# DC/SD_Q7 Return item information where all its authors are from certain country (Canada).

for $item in input()/catalog/:item
where every $add in
    $item/authors/author/contact_information/mailing_address
satisfies $add/name_of_country = "Canada"
return
    $item


# DC/SD_Q8 Return the publisher of an item with id attribute value (I4).

for $a in input()/catalog/*[@id="I4"]
return
    $a/publisher


# DC/SD_Q9 Return the ISBN of an item with id attribute value (I5).

for $a in input()/catalog/:item
where $a/@id="I5"
return
    $a//ISBN/text()


# DC/SD_Q10 List the item titles ordered alphabetically by publisher name, with release date within a certain time period (from 1990-01-01 to 1995-01-01).

for $a in input()/catalog/:item
where $a/date_of_release gt "1990-01-01" and
    $a/date_of_release lt "1995-01-01"
order by $a/publisher/name
return
    <Output>
        {$a/title}
        {$a/publisher}
    </Output>

# DC/SD_Q11 List the item titles in descending order by date of release with date of release within a certain time range (from 1990-01-01 to 1995-01-01.

for $a in input()/catalog/:item
where $a/date_of_release gt "1990-01-01" and
    $a/date_of_release lt "1995-01-01"
order by $a/data_of_release descending
return
    <Output>
        {$a/title}
        {$a/date_of_release}
    </Output>

# DC/SD_Q12 Get the mailing address of the first author of certain item with id attribute value (I6).

for $a in input()/catalog/:item[@id="I6"]
return
    <Output>
        {$a/authors/author[1]/contact_information/mailing_address}
    </Output>

# DC/SD_Q14 Return the names of publishers who publish books between a period of time (from 1990-01-01 to 1991-01-01) but do not have FAX number.

for $a in input()/catalog/:item
where $a/date_of_release gt "1990-01-01" and
           $a/date_of_release lt "1991-01-01" and
           empty($a/publisher/contact_information/FAX_number)
return
      <Output>
        {$a/publisher/name}
      </Output>

# DC/SD_Q17 Return the ids of items whose descriptions contain a certain word (``hockey").

for $a in input()/catalog/:item
where contains ($a/description, "hockey")
return
<Output>
    {$a/@id}
</Output>

# DC/SD_Q19 Retrieve the item titles related by certain item with id attribute value (I7).

for $item in input()/catalog/:item[@id="I7"],
    $related in input()/catalog/:item
where $item/related_items/related_item/item_id = $related/@id
return
    <Output>
        {$related/title}
    </Output>

# DC/SD_Q20 Retrieve the item title whose size (length*width*height) is bigger than certain number (500000).

for $size in input()/catalog/:item/attributes/size_of_book
where $size/length*$size/width*$size/height > 500000
return
    <Output>
        {$size/../../title}
    </Output>

