Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMPSCI 752

Big data management

Assignment 1 / Semester 1, 2023

Semi-structured Data Modelling and Access

Model Answers

Application domain. Consider the tree in Figure 1. Element nodes are labelled by E. The tag of a node is either c or d. The context node for Question 1(b) is marked in pink.

  

Fig. 1. XML Tree for Question 1

1. XPath.

(a) For each node of the tree, write down the corresponding pre- and post-identifier. [5 marks]

Solution:

 

(b) With respect to the given context node, marked in pink, evaluate the following axes on the tree. List the pre-identifier of each node that each axis returns.

i. preceding::d

ii. descendant-or-self::c

iii. following-sibling::*                                                                            [5 marks]

Solution:

i. nodes 2, 4, 5, 7, 8

ii. nodes 10, 13

iii. no node

(c) Convert each of the following queries into the corresponding XPath expression, and evaluate it on the tree by listing the pre-identifier of the nodes returned.

i. Return the rst of the c-element nodes that are children of the second of the d-element nodes that are children of some element node.

Solution:

 //d[2]/c[1] or //*/d[2]/c[1]

 node 10

ii. Return every element node whose number of ancestor-nodes is the same as its number of descendant-nodes.

Solution:

 //*[count(ancestor::*)=count(descendant::*)]

 nodes 3, 6, 10, 13

iii. Return every node that has only leaves as children.                               [5 marks]

Solution:

 //*[*[count(*)=0]]

 nodes 3, 6, 10, 13

Application domain. For Question 2, consider the following XML document.

< a r t i s t s >

< a r t i s t   name= ‘ ‘ V i n n i e  Van  Voov”>

<p e r i o d >I m p r e s s i o n </p e r i o d>

<p i e c e s >2000</ p i e c e s >

<r a t i n g >10</ r a t i n g >

<age >37</age>

</ a r t i s t >

< a r t i s t   name= ‘ ‘ Henri   H e r o i c”>

<p e r i o d >E x p r e s s i o n </p e r i o d>

<p i e c e s >900</ p i e c e s >

<r a t i n g >9</r a t i n g >

<age >85</age>

</ a r t i s t >

< a r t i s t   name= ‘ ‘ P i c a n t e   P u b l i c”>

<p e r i o d >E x p r e s s i o n </p e r i o d>

<p i e c e s >50000</ p i e c e s >

<r a t i n g >10</ r a t i n g >

<age >92</age>

</ a r t i s t >

</ a r t i s t s >

2. XQuery.

Write down the following English language queries in XQuery, and show the result of evaluating the XQuery query on the given XML document.

(a) For each distinct period p, for which no artist in that period p has a rating lower than 10, return an element node that lists the period and, for each artist of that period, an attribute child artist that lists the name of the artist. [5 marks]

Solution:

f o r   $p   i n   d i s t i n c t −v a l u e s ( / / p e r i o d )

where   e v e r y   $ r   i n   // a r t i s t [ p e r i o d=$p ] / r a t i n g   s a t i s f i e s   ( $r >=10) r e t u r n

<p e r i o d  name= ‘ ‘{ $p}”>

{ f o r   $a   i n   // a r t i s t [ p e r i o d=$p ]

r e t u r n  <a r t i s t >{$a /@name}</ a r t i s t >}

</p e r i o d>

<xml>

<p e r i o d  name= ‘ ‘ I m p r e s s i o n”>

< a r t i s t   name=”V i n n i e  Van  Voov”/>

</p e r i o d>

</xml>

(b) For each pair of artists, where each of the two artists has produced at least  1000 pieces, return an element node pair with two element children artist that contain the name of the artist as a string, respectively. For full marks, a pair cannot contain the same artist twice, and there must not be any pairs where the order of the artists is simply swapped.               [5 marks]

Solution:

f o r   $a1   i n   // a r t i s t

f o r   $a2   i n   // a r t i s t [ p r e c e d i n g −s i b l i n g : : a r t i s t=$a1 ]

where   $a1 / p i e c e s   >=1000  and  $a2 / p i e c e s   >=1000

r e t u r n

<p a i r>

<a r t i s t >{ s t r i n g ( $a1 /@name)}</ a r t i s t >

<a r t i s t >{ s t r i n g ( $a2 /@name)}</ a r t i s t >

</p a i r>

<p a i r>

<a r t i s t >V i n n i e  Van  Voov</ a r t i s t >

<a r t i s t >P i c a n t e   P u b l i c </ a r t i s t >

</p a i r>

(c) For every distinct rating, return an element node rating with an attribute child called value that lists the rating and an element child average age that contains the average age of artists with that rating.       [5 marks]

Solution:

f o r   $ r   i n   d i s t i n c t −v a l u e s ( / / r a t i n g )

l e t   $c := avg ( / / a r t i s t [ r a t i n g=$ r ] / age )

r e t u r n

<r a t i n g   v a l u e = ‘ ‘{ $ r }”>

<a v e r a g e  a g e >{$c}</ a v e r a g e  a g e >

</ r a t i n g >

 

<xml>

<r a t i n g   v a l u e = ‘ ‘10”>

<a v e r a g e  a g e >64.5</ a v e r a g e  a g e >

</ r a t i n g >

<r a t i n g   v a l u e =‘‘9”>

<a v e r a g e  a g e >85</ a v e r a g e  a g e >

</ r a t i n g >

</xml>