You are on page 1of 9

Lab XML

Exercise 11.1.2 p.487:
Ajouter les faits suivants
_Star Wars Movie was directed by George Lucas, and was produced by Gary Kurtz

_Carrie Fisher and Mark Hamill appeared in Empire Strikes Back Movie and Return of the Jedi Movie

_ Fox studio, is a studio in Hollywood. It owns Empire Strikes Back Movie and Return of the Jedi Movie

Instructor : Rim Moussa


Test XPath/XQuery on-line https://www.videlibri.de/cgi-bin/xidelcgi

Exercise 11.1.2 p.528:

(a) Find the names of all ships.


//Ship/@name

(b) Find all the Class elements for classes with a displacement larger than 35000.

//Class[@displacement > 35000]

(c) Find all the Ship elements for ships that were launched before 1917.

Instructor : Rim Moussa


//Ship[@launched < 1917]
(d) Find the names of the ships that were sunk.
//Battle[@outcome = "sunk"]/../@name

ou avec XQuery

let $ships := //Ship


for $s in $ships
where $s/Battle[@outcome = "sunk"]
return data($s/@name)

(e) Find the years in which ships having the same name as their class were launched.
//Ship[@name = ../@name]/@launched

(f) Find the names of all ships that were in battles.


//Ship[Battle]
//Battle[@outcome != "nil"]/../@name

(g) Find the Ship elements for all ships that fought in two or more battles.
//Ship[count(Battle) >= 2]

Exercise 12.2.2 p.543: XQuery

(a) Find the names of the classes that had at least 10 guns.
→ sol 1

let $ships := doc("/home/rym/Downloads/data_XML/ships.xml")

for $class in $ships/Ships/Class

where $class/@numGuns >= 10

return data($class/@name)

→ sol 2

let $x := //Class

for $class in $x

where $class/@numGuns >= 10

return data($class/@name)

(b) Find the names of the ships that had at least 10 guns.
(c) Find the names of the ships that were sunk.
(d) Find the names of the classes with at least 3 ships.
(e) Find the names of the classes such that no ship of that class was in a battle.
(f) Find the names of the classes that had at least two ships launched in the same year.
(g) Produce a sequence of items of the form
<Battle name = x> <Ship name = y />… < /Battle>

Instructor : Rim Moussa


Instructor : Rim Moussa
Exercise 12.1.2

a) //Ship/@name

Kongo

Hiei

Kirishima

Hurana

North Carolina

Washington

Tennessee

California

King George V

Prince of Wales

Duke of York

Howe

Anson

b) /Ships/Class[@displacement > 35000]

<Class name="North Carolina" type = "bb" country="USA" numGuns="9" displacement="37000">...</Class>

C) //Ship[@launched < 1917]

<Ship name="Kongo" launched="1913" />

<Ship name="Hiei" launched="1914" />

<Ship name="Kirishima" launched="1915">...</Ship>

<Ship name="Hurana" launched="1915" />

D) //Ship [Battle/@outcome = "sunk"]

<Ship name="Kirishima launched="1915">...</Ship>

<Ship name="Prince of Wales" launched="1941">...</Ship>

E) /Ships/Class/Ship [@name = ../@name]/@launched

Instructor : Rim Moussa


1913

1941

1920

1940

F) /Ships/Class/Ship [Battle]/@name

Kirishima

Washington

Tennessee

California

Prince of Wales

Duke of York

g) /Ships/Class/Ship [count(Battle) >= 2]

<Ship name="Prince of Wales" launched="1941">...</Ship>

Exercise 12.2.2

a)

let $ships := doc("Ships.xml")

for $class in $ships/Ships/Class

where $class/@numGuns >= 10

return data($class/@name)

b)

let $ships := doc("Ships.xml")

for $class in $ships/Ships/Class

where $class/@numGuns >= 10

return data($class/Ship/@name)

c)

let $ships := doc("Ships.xml")


Instructor : Rim Moussa
for $ship in $ships//Ship[Battle]

where $ship/Battle/@outcome = "sunk"

return data($ship/@name)

d)

let $ships := doc("Ships.xml")

for $class in $ships//Class

where count($class/Ship) >= 3

return data($class/@name)

e)

let $ships := doc("Ships.xml")

for $class in $ships//Class

where every $ship in $class/Ship satisfies count($ship/Battle) = 0

return data($class/@name)

or

let $ships := doc("Ships.xml")

for $class in $ships//Class

where count($class//Battle) = 0

return data($class/@name)

f)

let $ships := doc("Ships.xml")

for $class in $ships//Class

where some $ship in $class/Ship satisfies count($class/Ship[@launched = $ship/@launched]) >= 2

return data($class/@name)

Instructor : Rim Moussa


Instructor : Rim Moussa
g)

let $ships := doc("Ships.xml")

let $battles := distinct-values(data($ships//Battle))

for $battle in $battles

return <Battle name = "{$battle}">

for $ship in $ships//Ship[Battle]

where data($ship/Battle) = $battle

return <Ship name = "{data($ship/@name)}" />

}</Battle>

Instructor : Rim Moussa

You might also like