Since XQuery is a powerful query language which can be used

advertisement
Since XQuery is a powerful query language which can be used to fill our project
requirement , so it is recommoned to implement the queries with it. And its easy to get
a tool so called Quip to execute the application.
We are required to implement the following queries , for instance the solution is
showed after the problems.
1.List all journal names appearing in a XML-bib-file. XQuery :
Let $ jn := decument(“xbib.xml”) // journal
line 1
Return distinct-values($jn)
line 2
The output :
<?xml version=”1.0”?>
<quit:result xmlns:quip = http://namespzces.softwareag.com/tamino/quip/>
<journal> Theoretical Computer Science </journal>
<journal>XML Journal</joutnal>
</quip:result>
Line 1 defines a variable $jn which is the journal element, and it is in one or
more levels deep from the first level.
Line 2 returns the distinct values of the variable if it has more than one value.
The output Shows there are two different journal names in this xbib.xml file.
2.List all titles of publications of author named 'X'.
XQuery:
For $f in document (“xbib.xml”) //entry
For $d in $f//author
Line 1
Line 2
Where ($d/firstname = “Bob” and $d/lastname = “Builder”)
Line 3
Return $f/*/title
Line 4
The output:
<?xml version=”1.0”?>
<quit:result xmlns:quip = http://namespzces.softwareag.com/tamino/quip/>
<title> Construction of XML – 2 </title>
<title>test</title>
</quip:result>
Line 1 generates a bound variable $f with element values entry
Line 2 generates a bound variable $d which is more than one level deep within
entry element
Line 3 specify the condition as that the first name is Bob and the last name is
Builder.
Line 4 returns the titles values of the variable $f .
The output Shows there are two different titles which has the same name Bob Builder
in this xbib.xml file.
3.Sort entries according to year.
XQuery:
for $a in document(“xbib.xml”)//entry sortby(*//year DESCENDING)
Line 1
Return $f/*//year
Line 2
The output:
<?xml version=”1.0”?>
<quit:result xmlns:quip = http://namespzces.softwareag.com/tamino/quip/>
<year> 2222 </year>
<year> 2002 </year>
<year> 2001 </year>
<year> 2001 </year>
<year> 2001 </year>
<year> 2001 </year>
</quip:result>
Line 1 binds a variable $a to values that is the descending sorted years in entry ;
Line 2 returns the more than one level element year’s values of that variable.
4.All co-authors to author 'X', sorted according to lastname followed by first
name, and with duplicates removed.
5.All entries where the same title appears several times (i.e., in different entries).
XQuery :
6.All authors who also appears as editors somewhere in the bibliography.
XQuery :
Let $ a:= decument(“xbib.xml”) // entry
line 1
Let $ t:= decument(“xbib.xml”) // author
line 2
Let $ e:= decument(“xbib.xml”) // editor
line 3
For $f in $e/lastname
line 4
Let $d :=$t/lastname
line5
Return if ($d = $f)
line6
Then($e/firstname, $f)
line7
Else( )
line8
The output :
<?xml version=”1.0”?>
<quit:result xmlns:quip = http://namespzces.softwareag.com/tamino/quip/>
<journal> Theoretical Computer Science </journal>
<journal>XML Journal</joutnal>
</quip:result>
7.Count the number of publications for author 'X' during year 'Y'.
Xquery:
For $b in document (“xbib.xml”)//entry [*//firstname = “Bob”and *//lastname=
“Builder” and *//year = “2222”]
Line 1
Return count($b)
Line2
The Output:
<?xml version=”1.0”?>
<quit:result xmlns:quip = http://namespzces.softwareag.com/tamino/quip/>
1
</quip:result>
Line 1 bind a variable $b with element values which firstname is “Bob” and
lastname is “Builder” and the year is “2222”
Line 2 return the how many element in bound.
The result shows there is one in the xbib.xml document which satisfy the
condition.
8. Make a transformation
that chooses all entries of type article, conference, and
phd thesis and include only the fields author, title, and year.
Xquery:
For $x in document(“xbib.xml”)//article
Line 1
For $a in $x//author
Line2
For $b in $x//title
Line3
For $c in $x//year
Line4
For $y in document(“xbib.xml”)//incollection
Line5
For $d in $y//author
Line 6
For $e in $y//title
Line 7
For $f in $y//year
Line8
Let $z :=($a,$b,$c,$d,$e,$f)
Line9
Return $z
Line10
The output :
<?xml version=”1.0”?>
<quit:result xmlns:quip = http://namespzces.softwareag.com/tamino/quip/>
<author>
<firstname>Alice</firstname>
<lastname>Andersen</lastname>
<title> A simple entry title </title>
<year>2002</year>
<author>
<firstname>Helly</firstname>
</quip:result>
Line 1 binds a variable $x which is the element “article”
Line 2 to Line 4 bind three variable $a,$b,$c which have the value of “author”,
“title” and “year” within “article” more than one level deep
Line 5 to line 8 repeat the same thing for element “incollection”
Line 9 bind a variable $z which holds the values of variables $a,$b,$c,$d,$e,$f
Line 10 returns the values of variable $z
Download