View
214
Download
0
Embed Size (px)
Citation preview
1
XQuery :IV חלק
XML Query
2
DTDביבליוגרפיה -
<!ELEMENT bib (book*)><!ELEMENT book (title, (author+ | editor+ ), publisher, price)><!ATTLIST book year CDATA #REQUIRED ><!ELEMENT author (last, first)><!ELEMENT editor (last, first, affiliation )><!ELEMENT title (#PCDATA)><!ELEMENT last (#PCDATA)><!ELEMENT first (#PCDATA)><!ELEMENT affiliation (#PCDATA)><!ELEMENT publisher (#PCDATA)><!ELEMENT price (#PCDATA)>
3
books.xmlביבליוגרפיה – <bib>
<book year="1994"><title>TCP/IP Illustrated</title><author><last>Stevens</last><first>W.</first></author><publisher>Addison-Wesley</publisher><price>65.95</price>
</book><book year="1992">
<title>Advanced Programming in the UNIX Environment</title><author><last>Stevens</last><first>W.</first></author><publisher>Addison-Wesley</publisher><price>65.95</price>
</book>
<bib><book year="1994">
<title>TCP/IP Illustrated</title><author><last>Stevens</last><first>W.</first></author><publisher>Addison-Wesley</publisher><price>65.95</price>
</book><book year="1992">
<title>Advanced Programming in the UNIX Environment</title><author><last>Stevens</last><first>W.</first></author><publisher>Addison-Wesley</publisher><price>65.95</price>
</book>
4
…המשך ביבליוגרפיה<book year="2000">
<title>Data on the Web</title>
<author><last>Abiteboul</last><first>Serge</first></author>
<author><last>Buneman</last><first>Peter</first></author>
<author><last>Suciu</last><first>Dan</first></author>
<publisher>Morgan Kaufmann Publishers</publisher>
<price>65.95</price>
</book>
<book year="1999">
<title>The Economics of Technology and Content for Digital TV</title>
<editor><last>Gerbarg</last><first>Darcy</first><affiliation>CITI</affiliation></editor>
<publisher>Kluwer Academic Publishers</publisher>
<price>129.95</price>
</book>
</bib>
<book year="2000">
<title>Data on the Web</title>
<author><last>Abiteboul</last><first>Serge</first></author>
<author><last>Buneman</last><first>Peter</first></author>
<author><last>Suciu</last><first>Dan</first></author>
<publisher>Morgan Kaufmann Publishers</publisher>
<price>65.95</price>
</book>
<book year="1999">
<title>The Economics of Technology and Content for Digital TV</title>
<editor><last>Gerbarg</last><first>Darcy</first><affiliation>CITI</affiliation></editor>
<publisher>Kluwer Academic Publishers</publisher>
<price>129.95</price>
</book>
</bib>
5
Constructors (1)יוצרים –
Constructors משמשים ליצירת צמתי ומסמכיXML.:דוגמאות
יצירה פשוטה של צמתי אלמנט:>book><title>Database systems</title></book<
יצירת צמתים דינאמית:>booklist>{doc("books.xml")/bib/book/title}</booklist<
תוצאה:<booklist>
<title>TCP/IP Illustrated</title><title>Advanced Programming in the UNIX Environment</title>...
</booklist>
6
)2יוצרים (
יצירת מסמך XML:שלםdocument{
<!-- book titles in books.xml -->,<booklist>{doc("books.xml")/bib/book/title}</booklist>
}תוצאה:
<!-- book titles in books.xml --><booklist>
...</booklist>
7
)3יוצרים (
:דרך אלטרנטיבית ליצירת צמתים element book{ attribute year {1977},
element author {doc("books.xml")/bib/book/author[2]/*}, element {doc("books.xml")/bib/book[1]/price/name()} {4.3*10}}
תוצאה:<book year="1977"> <author><last>Buneman</last><first>Peter</first></author> <price>43</price></book>
8
FLWOR (For, Let, Where, Order by, Return)
For - for $b in doc("books.xml")/bib/book קושר את המשתנהb-לכל איבר שמוחזר ע"י ביטוי ה $in .
Let - let $authors := $b/author מציב את תוצאת הביטוי$b/author למשתנה $authors משמש .
בעיקר למניעת כפילויות בקוד. Where - where $authors/first = "Peter”
-מתאר תנאים עבור התוצאה. החישוב בחלק הreturn יבוצע אםתנאים אלו מתקיימים.
Order by - order by $b/title descending.מתאר את סדר המיון עבור התוצאה
Return - return $b/title -מתאר מה יוחזר ממבנה הFLWOR.
9
דוגמא?מה מבצע הביטוי מהשקף הקודם
for $b in doc("books.xml")/bib/booklet $authors := $b/authorwhere $authors/first = "Peter” order by $b/title descendingreturn $b/title
:תוצאה<title>Data on the Web</title>
:תשובההביטוי מחזיר את כל כותרות הספרים עם מחבר כלשהו
, ממוינים בסדר יורד לפי Peterששמו הפרטי הוא הכותרת.
10
דוגמא
.החזר את כל הספרים שנכתבו ע"י סרג' סוסיו:הצעה לפתרון
for $b in doc("books.xml")//book
where $b/author/first = "Serge" and
$b/author/last = "Suciu"
return $b
11
דוגמא: תוצאות מפתיעות<book year="2000">
<title>Data on the Web</title><author>
<last>Abiteboul</last><first>Serge</first>
</author><author>
<last>Buneman</last><first>Peter</first>
</author><author>
<last>Suciu</last><first>Dan</first>
</author><publisher>Morgan Kaufmann Publishers</publisher><price>65.95</price>
</book>
<book year="2000"><title>Data on the Web</title><author>
<last>Abiteboul</last><first>Serge</first>
</author><author>
<last>Buneman</last><first>Peter</first>
</author><author>
<last>Suciu</last><first>Dan</first>
</author><publisher>Morgan Kaufmann Publishers</publisher><price>65.95</price>
</book>
לא אותו סופרלא אותו סופר
12
דוגמא: הדרך הנכונה
for $b in doc("books.xml")//book
where $b/author[first="Serge" and last="Suciu"]
return $b מקבל ערך אמת אםתוצאת הביטוי לא ריקה
מקבל ערך אמת אםתוצאת הביטוי לא ריקה
13
Outer Joinדוגמא: ביצוע נתון קובץXML של ביקורות ספרים reviews.xml.
<reviews> <entry>
<title>Data on the Web</title><price>34.95</price><review> A very good discussion of semi-structured database
systems and XML. </review> </entry> <entry>
<title>Advanced Programming in the Unix environment</title><price>65.95</price><review> A clear and detailed discussion of UNIX programming.
</review> </entry>
<reviews> <entry>
<title>Data on the Web</title><price>34.95</price><review> A very good discussion of semi-structured database
systems and XML. </review> </entry> <entry>
<title>Advanced Programming in the Unix environment</title><price>65.95</price><review> A clear and detailed discussion of UNIX programming.
</review> </entry>
14
reviews.xmlהמשך
<entry> <title>TCP/IP Illustrated</title>
<price>65.95</price> <review> One of the best books on TCP/IP.
</review> </entry> <entry>
<title>TCP/IP Illustrated</title><price>66.95</price> <review> The book is a complete and detailed guide to the entire
TCP/IP protocol suite with an important difference from other books on the subject. </review> </entry></reviews>
<entry> <title>TCP/IP Illustrated</title>
<price>65.95</price> <review> One of the best books on TCP/IP.
</review> </entry> <entry>
<title>TCP/IP Illustrated</title><price>66.95</price> <review> The book is a complete and detailed guide to the entire
TCP/IP protocol suite with an important difference from other books on the subject. </review> </entry></reviews>
15
המשימה:
עבור כל הספרים שיש עבורם ביקורות במסמךreviews.xml החזר את פרטי הספרים (כותרת ושמות
מחברים) ביחד עם הביקורות בתוך צומת
book-with-review. עבור כל הספרים ללא ביקורות החזר רק את פרטיהם
. bookבצומת על התוצאה להיות בנוסחOuter Join כלומר כל ,
יכיל ביקורת אחת בלבד!book-with-reviewצומת
16
התוצאה הצפויה:<result>
<book-with-review><title>TCP/IP Illustrated</title><author><last>Stevens</last><first>W.</first></author><review>One of the best books on TCP/IP.</review>
</book-with-review><book-with-review>
<title>TCP/IP Illustrated</title><author><last>Stevens</last><first>W.</first></author><review>The book is a complete and detailed guide to the
entire TCP/IP protocol suite-with an important difference from other books on the subject.
</review></book-with-review><book-with-review>
<title>Advanced Programming in the Unix environment</title><author><last>Stevens</last><first>W.</first></author><review>A clear and detailed discussion of UNIX
programming.</review>
</book-with-review>
<result><book-with-review>
<title>TCP/IP Illustrated</title><author><last>Stevens</last><first>W.</first></author><review>One of the best books on TCP/IP.</review>
</book-with-review><book-with-review>
<title>TCP/IP Illustrated</title><author><last>Stevens</last><first>W.</first></author><review>The book is a complete and detailed guide to the
entire TCP/IP protocol suite-with an important difference from other books on the subject.
</review></book-with-review><book-with-review>
<title>Advanced Programming in the Unix environment</title><author><last>Stevens</last><first>W.</first></author><review>A clear and detailed discussion of UNIX
programming.</review>
</book-with-review>
17
התוצאה הצפויה המשך...
<book-with-review><title>Data on the Web</title><author><last>Abiteboul</last><first>Serge</first></author><author><last>Buneman</last><first>Peter</first></author><author><last>Suciu</last><first>Dan</first></author><review>A very good discussion of semi-structured database
systems and XML.</review>
</book-with-review><book>
<title>The Economics of Technology and Content for Digital TV</title>
</book></result>
<book-with-review><title>Data on the Web</title><author><last>Abiteboul</last><first>Serge</first></author><author><last>Buneman</last><first>Peter</first></author><author><last>Suciu</last><first>Dan</first></author><review>A very good discussion of semi-structured database
systems and XML.</review>
</book-with-review><book>
<title>The Economics of Technology and Content for Digital TV</title>
</book></result>
לספר זה אין מחבריםואין ביקורת
לספר זה אין מחבריםואין ביקורת
18
הפתרון:<result> { for $b in doc("books.xml")//book let $t := (for $a in doc("reviews.xml")//entry where ($b/title = $a/title) return $a/review) return if ($t) then
for $c in $t return <book-with-review>{$b/title, $b/author, $c} </book-with-review>
else <book>{$b/title, $b/author}</book>}</result>
19
דוגמא – הפיכת היררכית המסמך
ברצוננו ליצור "מסמך חדש" שבו היררכית הצמתים.books.xmlתהיה הפוכה לזו של המסמך
בפרט, נרצה שצמתיpublisher יהיו כעת בראש ) של כל הספרים titleההיררכיה ויכילו כותרות (צמתי
שהוצאו לאור על ידם. שימו לב, שבמסמךbooks.xml צמתי publisher
booksנמצאים בתחתית ההיררכיה עם צמתי מעליהם.
20
הפיכת היררכית המסמך – התוצאה הצפויה
<listing><publisher> <name>Addison-Wesley</name> <title>Advanced Programming in the UNIX Environment</title> <title>TCP/IP Illustrated</title></publisher><publisher> <name>Kluwer Academic Publishers</name> <title>The Economics of Technology and Content for Digital TV</title></publisher><publisher> <name>Morgan Kaufmann Publishers</name> <title>Data on the Web</title></publisher>
</listing>
<listing><publisher> <name>Addison-Wesley</name> <title>Advanced Programming in the UNIX Environment</title> <title>TCP/IP Illustrated</title></publisher><publisher> <name>Kluwer Academic Publishers</name> <title>The Economics of Technology and Content for Digital TV</title></publisher><publisher> <name>Morgan Kaufmann Publishers</name> <title>Data on the Web</title></publisher>
</listing>
21
:הפתרון <listing>{
for $p in distinct-values(doc("books.xml")//publisher) order by $p return <publisher>
<name> { $p }</name>
{ for $b in doc("books.xml")/bib/book where $b/publisher = $p order by $b/title return $b/title }
</publisher> }</listing>
מחזיר את כל ערכי הטקסטהשונים של הצמתים
מחזיר את כל ערכי הטקסטהשונים של הצמתים
22
פונקציות רקורסיביותxquery version "1.0";declare function local:toc($book-or-section as element()) as element()*{ for $s in $book-or-section/section return <section> { $s/@title, local:toc($s) } </section>};<toc> {
for $b in doc("books.xml")//book return local:toc($b) }</toc>
23
התוצאה
<toc><section title="Intro">
<section title="Background"/><section title="RelatedWork"/>
</section><section title="Definitions"/><section title="The System">
<section title="Design"/><section title="Implementation">
<section title="Overview"/><section title="Detailed"/>
</section></section>
</toc>