1 חלק xquery :iv xml query. 2 ביבליוגרפיה - dtd 3 ביבליוגרפיה – books.xml...

23
1 קקקXQuery :IV XML Query

Post on 20-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

1

XQuery :IV חלק

XML Query

Page 2: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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)>

Page 3: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 4: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 5: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 6: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

6

)2יוצרים (

יצירת מסמך XML:שלםdocument{

<!-- book titles in books.xml -->,<booklist>{doc("books.xml")/bib/book/title}</booklist>

}תוצאה:

<!-- book titles in books.xml --><booklist>

...</booklist>

Page 7: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 8: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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.

Page 9: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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ששמו הפרטי הוא הכותרת.

Page 10: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

10

דוגמא

.החזר את כל הספרים שנכתבו ע"י סרג' סוסיו:הצעה לפתרון

for $b in doc("books.xml")//book

where $b/author/first = "Serge" and

$b/author/last = "Suciu"

return $b

Page 11: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

לא אותו סופרלא אותו סופר

Page 12: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

12

דוגמא: הדרך הנכונה

for $b in doc("books.xml")//book

where $b/author[first="Serge" and last="Suciu"]

return $b מקבל ערך אמת אםתוצאת הביטוי לא ריקה

מקבל ערך אמת אםתוצאת הביטוי לא ריקה

Page 13: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 14: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 15: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

15

המשימה:

עבור כל הספרים שיש עבורם ביקורות במסמךreviews.xml החזר את פרטי הספרים (כותרת ושמות

מחברים) ביחד עם הביקורות בתוך צומת

book-with-review. עבור כל הספרים ללא ביקורות החזר רק את פרטיהם

. bookבצומת על התוצאה להיות בנוסחOuter Join כלומר כל ,

יכיל ביקורת אחת בלבד!book-with-reviewצומת

Page 16: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 17: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

לספר זה אין מחבריםואין ביקורת

לספר זה אין מחבריםואין ביקורת

Page 18: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 19: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

19

דוגמא – הפיכת היררכית המסמך

ברצוננו ליצור "מסמך חדש" שבו היררכית הצמתים.books.xmlתהיה הפוכה לזו של המסמך

בפרט, נרצה שצמתיpublisher יהיו כעת בראש ) של כל הספרים titleההיררכיה ויכילו כותרות (צמתי

שהוצאו לאור על ידם. שימו לב, שבמסמךbooks.xml צמתי publisher

booksנמצאים בתחתית ההיררכיה עם צמתי מעליהם.

Page 20: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 21: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

מחזיר את כל ערכי הטקסטהשונים של הצמתים

מחזיר את כל ערכי הטקסטהשונים של הצמתים

Page 22: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>

Page 23: 1 חלק XQuery :IV XML Query. 2 ביבליוגרפיה - DTD 3 ביבליוגרפיה – books.xml TCP/IP Illustrated Stevens W. Addison-Wesley 65.95 Advanced Programming in

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>