Section1
Exercise 12.1.1
a) //PC/RAM
<RAM>1024</RAM>
b) //@price
2114
995
1150
c) //Printer
<Printer model=”3002″ price=”239″>…</Printer>
d) /Products/Maker[Printer/Type = “laser”]/@name
E
H
e) /Products/Maker[PC or Laptop]/@name
f) //PC[HardDisk >= 200]/@model
g) /Products/Maker[count(PC) >= 2]/@name
Exercise 12.1.2
a) //Ship/@name
Kongo
Hiei
Kirishima
b) /Ships/Class[@displacement > 35000]
C) //Ship[@launched < 1917]
<Ship name=”Kongo” launched=”1913″ />
D) //Ship [Battle/@outcome = “sunk”]
E) /Ships/Class/Ship [@name = ../@name]/@launched
1913
F) /Ships/Class/Ship [Battle]/@name
Kirishima
g) /Ships/Class/Ship [count(Battle) >= 2]
<Ship name=”Prince of Wales” launched=”1941″>…</Ship>
Section 2
Exercise 12.2.1
a)
let $products := doc(“products.xml”)
b)
<CheapPrinters>
{
let $products := doc(“products.xml”)
c)
let $products := doc(“products.xml”)
D)
let $products := doc(“products.xml”)
E)
let $products := doc(“products.xml”)
note: without [PC] from line 2 will qualify makers that do not produce any PC
F)
let $products := doc(“products.xml”)
Exercise 12.2.2
a)
let $ships := doc(“Ships.xml”)
b)
let $ships := doc(“Ships.xml”)
c)
let $ships := doc(“Ships.xml”)
d)
let $ships := doc(“Ships.xml”)
for $class in $ships//Class
where count($class/Ship) >= 3
return data($class/@name)
e)
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)
g)
let $ships := doc(“Ships.xml”)
let $battles := distinct-values(data($ships//Battle))
or
let $ships := doc(“Ships.xml”)
Exercise 12.2.3
let $stars := doc(“stars.xml”)
Exercise 12.2.4
It is not possible to have expression E and F such that the expression every $x in E satisfies F is
Section 3
Exercise 12.3.1
a)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:template match=”/”>
b)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
<th>Model</th>
<th>Price</th>
</tr>
<xsl:for-each select=”//PC”>
<tr>
<td>
<xsl:value-of select=”@model” />
</td>
<td>
c)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
<th>Model</th>
<th>Price</th>
<th>Speed</th>
<th>Ram</th>
</tr>
<xsl:for-each select=”//Laptop”>
<tr>
<td><xsl:value-of select=”@model” /></td>
<td><xsl:value-of select=”@price” /></td>
<td><xsl:value-of select=”Speed” /></td>
<th>Ram</th>
</tr>
<xsl:for-each select=”//PC”>
<tr>
<td><xsl:value-of select=”@model” /></td>
<td><xsl:value-of select=”@price” /></td>
<td><xsl:value-of select=”Speed” /></td>
d)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
e)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
<xsl:template match=”/”>
<xsl:element name=”Products”>
<xsl:for-each select=”//PC”>
<xsl:for-each select=”//Printer”>
<xsl:element name=”Product”>
<xsl:attribute name=”type”>
<xsl:value-of select=”name()”/>
</xsl:attribute>
<xsl:attribute name=”maker”>
f)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
<xsl:output method=”text”/>
</xsl:text>
<xsl:for-each select=”//PC”>
<xsl:value-of select=”@model” />
<xsl:text> &amp; </xsl:text>
Exercise 12.3.2
a)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
<xsl:template match=”/”>
<th>Name</th>
<th>Launched</th>
</tr>
<xsl:for-each select=”Ship”>
<tr>
<td><xsl:value-of select=”@name”/></td>
<td><xsl:value-of select=”@launched”/></td>
</tr>
</xsl:for-each>
b)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
c)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
<xsl:template match=”/”>
d)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns:fo=”http://www.w3.org/1999/XSL/Format”>
e)
<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″
</xsl:attribute>
<xsl:attribute name=”numGuns”>
<xsl:value-of select=”@numGuns”/>
</xsl:attribute>
<xsl:attribute name=”bore”>
<xsl:value-of select=”@bore”/>
</xsl:attribute>
<xsl:attribute name=”displacement”>
<xsl:value-of select=”@displacement”/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>