Ονοματεπώνυμο: Αριθμός Μητρώου: Πανεπιστήμιο Κρήτης Τμήμα Επιστήμης Υπολογιστών ΗΥ-561 Διαχείριση Δεδομένων στο Παγκόσμιο Ιστό Ειρήνη Φουντουλάκη Βασίλης Χριστοφίδης Άσκηση 1 (8 μονάδες) Τελική Εξέταση (3 ώρες) Ημερομηνία: Πέμπτη 19 Ιουνίου 2010 Θεωρείστε το παρακάτω XML schema: Consider the following XML schema: <?xml version="1.0" encoding="iso-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:simpletype name="stringtype"> <xs:restriction base="xs:string"/> </xs:simpletype> <xs:simpletype name="inttype"> <xs:restriction base="xs:positiveinteger"/> </xs:simpletype> <xs:simpletype name="dectype"> <xs:restriction base="xs:decimal"/> </xs:simpletype> <xs:simpletype name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpletype> <xs:complextype name="shiptotype"> <xs:sequence> <xs:element name="name" type="stringtype"/> <xs:element name="address" type="stringtype"/> <xs:element name="city" type="stringtype"/> <xs:element name="country" type="stringtype"/> </xs:sequence> </xs:complextype> <xs:complextype name="itemtype"> <xs:sequence> <xs:element name="title" type="stringtype"/> <xs:element name="note" type="stringtype" minoccurs="0"/> <xs:element name="quantity" type="inttype"/> <xs:element name="price" type="dectype"/> </xs:sequence> </xs:complextype> <xs:complextype name="shipordertype">
<xs:sequence> <xs:element name="orderperson" type="stringtype"/> <xs:element name="shipto" type="shiptotype"/> <xs:element name="item" maxoccurs="unbounded" type="itemtype"/> </xs:sequence> <xs:attribute name="orderid" type="orderidtype" use="required"/> </xs:complextype> <xs:element name="shiporderdb"> <xs:complextype> <xs:sequence> <xs:element name="shiporder" maxoccurs="unbounded" type="shipordertype"/> </xs:sequence> </xs:complextype> </xs:element> </xs:schema> Δημιουργείστε ένα έγκυρο (valid) XML έγγραφο για το παραπάνω σχήμα. Η ρίζα (root element) του εγγράφου είναι ένα στοιχείο "shiporderdb" το οποίο έχει δύο στοιχεία "shiporder". Το έγγραφο που περιέχει το σχήμα XML αποθηκεύεται στο αρχείο shiporder.xsd και στον ίδιο φάκελο με το XML έγγραφο που θα δημιουργήσετε. Create a valid XML document with a "shiporderdb" root element and two "shiporder" elements. We assume that the XML schema is stored in the file shiporder.xsd in the same directory as the XML document. <?xml version="1.0" encoding="iso-8859-1"?> <shiporderdb xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:nonamespaceschemalocation="shiporder.xsd"> <shiporder orderid="889923"> <orderperson>irini Fundulaki</orderperson> <shipto> <name>vassilis Christophides</name> <address>1234 Panepistimou St</address> <city>athens</city> <country>greece</country> </shipto> <item> <title>data Base Systems: The Complete Book</title> <note>second Edition</note> <quantity>1</quantity> <price>49.90</price> </item> <item> <title>data Mining: Concepts and Techniques</title> <quantity>1</quantity> <price>59.90</price>
</item> </shiporder> <shiporder orderid="712456"> <orderperson>vassilis Christophides</orderperson> <shipto> <name>irini Fundulaki</name> <address>7890 Panepistimiou St</address> <city>athens</city> <country>greece</country> </shipto> <item> <title>the Kyte Runner</title> <note>christmas sale</note> <quantity>1</quantity> <price>10.50</price> </item> <item> <title>hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder> </shiporderdb> Άσκηση 2 (23 μονάδες) Θεωρείστε το έγγραφο XML ShiporderDB.xml το οποίο ανταποκρίνεται στο XML schema της άσκησης 1. Γράψτε την αντίστοιχη επερώτηση XQuery για κάθε μία από τις ερωτήσεις (a) (d). Εξαλείψτε στις περιπτώσεις που μπορείτε τα αντίγραφα (duplicates) στα αποτελέσματα. Consider an XML document ShiporderDB.xml that conforms to the above XML schema. Formulate the following queries on this XML document in XQuery. Where applicable, eliminate duplicate results. a) (5 μονάδες) Βρείτε τα στοιχεία item τα οποία έχουν αποσταλεί ( shipto ) στην Αθήνα και επιστρέψτε ως ακολουθία εκείνα τα οποία έχουν την μέγιστη χρηματική αξία ( price ). Επιστρέψτε τα στοιχεία αυτά ως ακολουθία. Find the XML element «item» shipped to Athens (shipto) with the maximum price among these items, as a sequence of item elements. let $items := doc( ShiporderDB.xml )/shiporderdb/shiporder [contains(shipto/city, Athens )]/item let $max := max($items/price) for $i in doc( ShiporderDB.xml )/shiporderdb/shiporder [contains(shipto/city, Athens )]/item
where $i/price = $max return $i b) (8 μονάδες) Για όλα τα στοιχεία shiporder τα οποία περιέχουν ένα στοιχείο item με την λέξη Data στο υπο-στοιχείο τους title, επιστρέψτε ένα στοιχείο result το οποίο περιέχει το στοιχείο orderid και τον αριθμό των στοιχείων item του shiporder. For all «shiporder» elements that contain an element «item» that has Data in its subelement «title», produce a «result» element consisting of the orderid and the number of «item» elements of the shiporder element. for $s in doc( ShiporderDB.xml )/shiporderdb/shiporder where some $i in $s/item satisfies contains($i/title, Data ) return <result> {$s/@orderid} {count($s/item)} </result> c) (5 μονάδες) Για όλα τα στοιχεία orderperson που έχουν αποστείλει μία παραγγελία ( order ) στην Ελλάδα, επιστρέψτε το στοιχείο orderperson και μία ακολουθία των item που έχουν αποσταλεί. Να συμπεριλάβετε όλη την πληροφορία σχετικά με ένα στοιχείο orderperson στο ίδιο στοιχείο αποτελέσματος. For all «orderperson» elements who have shipped an order to Greece, find the orderperson and the sequence of all «item» elements that have been shipped. Include all information belonging to the same «orderperson» elements into one result element. for $o in distinctvalues(doc("shiporderdb.xml")/shiporderdb/shiporder[shipto/co untry = "Greece"]/orderperson) return <result> {$o} { for $s in doc("shiporderdb.xml")/shiporderdb/shiporder where $s/child::orderperson = $o return $s/item} </result>
d) (5 μονάδες) Find all «shiporder» elements that contain an «item» element that both has Data in its subelement «title» and has a «price» element with value higher than 50 euros. Επιστρέψτε όλα τα στοιχεία shiporder τα οποία περιέχουν ένα στοιχείο item το οποίο έχει την λέξη Data στο υπο-στοιχείο του title, και αξία ( price ) μεγαλύτερη από 50 euros. for $i1 in doc( ShiporderDB.xml )/shiporderdb/shiporder/item [contains(title, Data )] [price > 50.0] Άσκηση 3 (20 μονάδες) Για το δένδρο Τ το οποίο φαίνεται στην εικόνα, επιστρέψτε το αποτέλεσμα των παρακάτω εκφράσεων XPath χρησιμοποιώντας τον αριθμό κάθε κόμβου. For the tree T on the right, write numbers of nodes selected by the following XPath expressions. a) //a 1,4,5,8,9 b) /*//*//a[preceding::a] c) //*[.//d] 8,9 1,2,4 d) /*[not(a and b)]
1 e) //*[count(.//*)=count(ancestor::*)] 4 f) //c[position() = last()] - g) /descendant:*[position() mod 2 = count(.//*)] 6,8 h) //*[count(*)>1 and not(child::*[not(self::a)])] 7 i) //*[preceding-sibling::b] 4,7 Άσκηση 4 (34 μονάδες) a) (8 μονάδες) Επιστέψτε τον PRE/POST πίνακα για το παρακάτω δένδρο. Give the PRE/POST table for the tree <a> <b> <c/> </b> <c> <d/> <d> <b/> <d/> </d> </c> <d/> <b> <c>
</a> </b> </c> <b/> <b/> <d/> Pre Post Label 1 14 a 2 2 b 3 1 c 4 7 c 5 3 d 6 6 d 7 4 b 8 5 d 9 8 d 10 13 b 11 11 c 12 9 b 13 10 d 14 12 b b) (10 μονάδες) Δώστε τον ψευδοκώδικα του αλγορίθμου ο οποίος υπολογίζει την διάταξη POST ενός δένδρου με έναν επαναληπτικό τρόπο (iteratively), δηλαδή χωρίς αναδρομικές κλήσεις. Μπορείτε να χρησιμοποιήσετε τις συναρτήσεις firstchild(n),nextsibling(n), και parent(n) για ένα κόμβο n. Give pseudo code that computes the POST order of a tree in an iterative way, i.e., without any recursive calls(!). You can use firstchild(n),nextsibling(n), and parent(n) for a node n. int i=1; Node n=root; repeat{ while(firstchild(n)!=null) n=firstchild(n); post(i)=n; i++; while(nextsibling(n)==nil){ n=parent(n); if(n==null) break; post(i)=n; i++; }
n=nextsibling(n); } Χρησιμοποιώντας τις διατάξεις PRE/POST για τους κόμβους ενός δένδρου εξηγήστε πως μπορούμε να βρούμε: Using the PRE/POST-encoding, explain how to obtain c) (4 μονάδες) τους προγόνους ενός κόμβου the ancestors of a node Given (pre, post) of a node, its ancestors are all nodes with pre-value < pre and post-value > post. d) (4 μονάδες) το τελευταίο παιδί ενός κόμβου the last child of a node If there is a node with pre-value > pre and with post-value=post-1, then that is the last child of (pre, post) e) (8 μονάδες) το μέγιστο βάθος του υποδένδρου ενός κόμβου the maximal depth of the subtree at a node. int maxdepth(int pr){ size(int p): int{ int s=0; for(int pr2=p+1; post(pr2)<post(p); pr2++) s++ return s; } int D=0; int u,l; L=pr+size(pr)-post(pr); for(int pr2=pr+1; post(pr2)<post(pr); pr2++){ u=pr2+size(pr2)-post(pr2)-l; if(u>d) D=u; return D; } Άσκηση 5(30 μονάδες) Δεδομένου του πίνακα PRE/POST/SIZE, γράψτε τις SQL επερωτήσεις οι οποίες υπολογίζουν τα αποτελέσματα των παρακάτων XPath επερωτήσεων.
Σημειώστε την εξίσωση level(n) = pre(n) post(n)+size(n), όπου size(n) είναι ο αριθμός των απογόνων του n συμπεριλαμβανομένου του n. Θεωρείστε ότι η ρίζα του XML δέντρου έχει preorder ίσο με 0. Given a PRE/POST/SIZE table, show SQL queries for the XPath queries Recall: level(n) = pre(n) post(n)+size(n), where n denotes the number of the descendents of n, including n. Consider that the root element of the XML tree has preorder equal to 0. a) (4 μονάδες) /* SELECT DISTINCT r1.pre FROM doc_tbl r1 WHERE r1.pre=1 ORDERED BY r1.pr b) (8 μονάδες) /a/b/* SELECT DISTINCT r4.pre FROM doc_tbl r1, r2, r3, r4 WHERE r1.pre=0 AND r2.pre>r1.pre AND r2.post<r1.post AND (r2.pre-r2.post+r2.size)=(r1.pre-r1.post+r1.size)+1 AND r2.tag= a AND r3.pre>r2.pre AND r3.post<r2.post AND (r3.pre-r3.post+r3.size)=(r2.pre-r2.post+r2.size)+1 AND r3.tag= b AND r4.pre>r3.pre AND r4.post<r3.post AND (r4.pre-r4.post+r4.size)=(r3.pre-r3.post+r3.size)+1 ORDERED BY r4.pre c) (8 μονάδες) //a/*//b SELECT DISTINCT r4.pre FROM doc_tbl r1, r2, r3, r4 WHERE r1.pre=0 AND r2.pre>r1.pre AND r2.post<r1.post AND r2.tag= a AND r3.pre>r2.pre AND r3.post<r2.post AND (r3.pre-r3.post+r3.size)=(r2.pre-r2.post+r2.size)+1 AND r4.pre>r3.pre AND r4.post<r3.post AND r4.tag= b ORDERED BY r4.pre d) (10 μονάδες) //a/following-sibling::b
SELECT DISTINCT r4.pre FROM doc_tbl r1, r2, r3, r4 WHERE r1.pre=0 AND r2.pre>r1.pre AND r2.post<r1.post AND r2.tag= a // parent of a-node AND r3.pre<r2.pre AND r3.post>r2.post AND (r3.pre-r3.post+r3.size)=(r2.pre-r2.post+r2.size)-1 // after a-node AND r4.pre>r2.pre // before parent AND r4.post<r3.post // same level as a-node AND (r4.pre-r4.post+r4.size)=(r2.pre-r2.post+r2.size) AND r4.tag= b ORDERED BY r4.pre