1 / 32

XQUERY

XQUERY. Basi di Dati II Sara Romano. Cos’è XQuery?. XQuery è il linguaggio per interrogare dati XML; XQuery è per l’XML quello che SQL è per i database; XQuery è implementato sulla base delle espressioni XPath;

marlow
Télécharger la présentation

XQUERY

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XQUERY Basi di Dati II Sara Romano

  2. Cos’è XQuery? • XQuery è il linguaggio per interrogare dati XML; • XQuery è per l’XML quello che SQL è per i database; • XQuery è implementato sulla base delle espressioni XPath; • XQuery è una Raccomandazione del W3C. (XQuery 1.0 became a W3C Recommendation January 23, 2007)

  3. XPath XQuery XQuery e XPath • Il linguaggio XQuery 1.0 è progettato come superinsieme proprio di XPath 2.0: ogni espressione XPath è anche espressione XQuery. • XQuery 1.0 and XPath 2.0 presentano lo stesso data model e supportano le stesse funzioni ed operatori. • Sfrutta il potere espressivo di XPath, in aggiunta: • esegue il join di informazioni da fonti diverse; • genera nuovi frammenti XML; • funzioni definite dall’utente.

  4. Interrogazioni XQuery • Interrogazione è composta da un prologo seguito da espressioni FLWOR. • Prologo: le espressioni XQuery sono valutate relativamente ad un contesto. Definizione di parametri per il processore XQuery. • Esempio: • xquery version "1.0"; • declare default element namespace URI; • declare default function namespace URI; • import schema at URI;

  5. FLWOR Expression Permettono di ristrutturare ed unire informazioni provenienti da fonti diverse. For Let Where Order by Return Generazione di sequenze di tuple (opzionali) Filtraggio della sequenza (opzionale) Ordinamento della sequenza risultante (opzionale) Costruzione della sequenza risultante

  6. Clausola FOR (1/3) • La clausola For lega una variabile ad ogni elemento restituito dall’espressione XPath. • La clausola For è una iterazione. • Possono esserci più clausole For all’interno di una FLWOR expression. Esempio: for $x in (1 to 5) return <test> {$x} </test> Risultato: <test>1</test> <test>2</test> <test>3</test> <test>4</test> <test>5</test>

  7. Clausola FOR (2/3) • per effettuare il conteggio sull’iterazione è possibile utilizzare la keyword atall’interno della clausola for. Esempio: (books.xml) for $x at $i in doc("books.xml")/bookstore/book/title return <book>{ $i }. { data($x) }</book> Risultato: <book>1. Everyday Italian</book> <book>2. Harry Potter</book> <book>3. XQuery Kick Start</book> <book>4. Learning XML</book>

  8. Clausola FOR (3/3) • E’ possibile utilizzare più espressioni all’interno di una clausola for. Esempio: for $x in (10,20), $y in (100,200) return <test> x={ $x } and y={ $y } </test> Risultato: <test>x=10 and y=100</test> <test>x=10 and y=200</test> <test>x=20 and y=100</test> <test>x=20 and y=200</test>

  9. Clausola LET • La clausola let associa ad una variabile il risultato di una espressione XPath. Esempio: let $x := (1 to 5) return <test> { $x } </test> Risultato: <test>1 2 3 4 5</test>

  10. Clausola WHERE • La clausola where è utilizzata per definire filtri sui risultati derivati dalle clausole precedenti (for e let). Esempio: where $x/price>30 and $x/price<100

  11. Clausola ORDER BY • La clausola order by definisce il tipo di ordinamento del risultato. Esempio: for $x in doc("books.xml")/bookstore/book order by $x/@category, $x/title return $x/title Risultato: <title lang="en">Harry Potter</title> <title lang="en">Everyday Italian</title> <title lang="en">Learning XML</title> <title lang="en">XQuery Kick Start</title>

  12. Clausola RETURN • La clausola return specifica cosa deve essere restituito in output. Esempio: for $x in doc("books.xml")/bookstore/book return $x/title Risultato: <title lang="en">Everyday Italian</title> <title lang="en">Harry Potter</title> <title lang="en">XQuery Kick Start</title> <title lang="en">Learning XML</title>

  13. Esempio 1 Dato il documento books.xml (http://www.w3schools.com/xquery/xquery_example.asp) “Titoli dei libri che costano più di 30€” For $x in doc("books.xml")/bookstore/bookwhere $x/price>30return $x/title Il path expression: doc("books.xml")/bookstore/book[price>30]/title Produce lo stesso risultato. Risultato: <title lang="en">XQuery Kick Start</title><title lang="en">Learning XML</title>

  14. Esempio 2 Dato il documento books.xml (http://www.w3schools.com/xquery/xquery_example.asp) “Titoli dei libri che costano più di 30€ ordinati per titolo” for $x in doc("books.xml")/bookstore/bookwhere $x/price>30order by $x/titlereturn $x/title Risultato: <title lang="en">Learning XML</title><title lang="en">XQuery Kick Start</title>

  15. Differenze tra FOR e LET (1/4) for $x in (1, 2, 3, 4) let $y := ("a", "b", "c") return ($x, $y) 1, a, b, c, 2, a, b, c, 3, a, b, c, 4, a, b, c

  16. Differenze tra FOR e LET (2/4) let $x in (1, 2, 3, 4) for $y := ("a", "b", "c") return ($x, $y) 1, 2, 3, 4, a, 1, 2, 3, 4, b, 1, 2, 3, 4, c

  17. Differenze tra FOR e LET (3/4) for $x in (1, 2, 3, 4) for $y in ("a", "b", "c") return ($x, $y) 1, a, 1, b, 1, c, 2, a, 2, b, 2, c, 3, a, 3, b, 3, c, 4, a, 4, b, 4, c

  18. Differenze tra FOR e LET (4/4) let $x := (1, 2, 3, 4) let $y := ("a", "b", "c") return ($x, $y) 1, 2, 3, 4, a, b, c Ogni clausola for e let può far riferimento a variabili legate in clausole precedenti: in questo modo si può effettuare il join tra documenti XML.

  19. Espressioni racchiuse Le espressioni XML possono essere calcolate dinamicamente mediante l’uso di “espressioni racchiuse”: { expr } Esempio: <foo>1 2 3 4 5</foo> <foo>{1, 2, 3, 4, 5}</foo> <foo>{1, "2", 3, 4, 5}</foo> <foo>{1 to 5}</foo> <foo>1 {1+1} {" "} {"3"} {" "} {4 to 5}</foo>

  20. Costruttori Le espressioni racchiuse possono essere utilizzate nei costruttori. Esempio: element card { namespace { "http://businesscard.org" }, element name { text { "John Doe" } }, element title { text { "CEO, Widget Inc." } } , element email { text { "john.doe@widget.com" } }, element phone { text { "(202) 555-1414" } }, element logo { attribute uri { "widget.gif" } } } <card xmlns="http://businesscard.org"> <name>John Doe</name> <title>CEO, Widget Inc.</title> <email>john.doe@widget.com</email> <phone>(202) 555-1414</phone> <logo uri="widget.gif"/> </card>

  21. Definizione di nuovi nodi (1/2) “Titoli dei libri che costano più di 30€ ordinati per titolo” <myQuery> { for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return <result> { $x/title } </result> } </myQuery>

  22. Definizione di nuovi nodi (2/2) • Produce come risultato un documento XML piuttosto che una lista di nodi: <myQuery> <result> <title lang="en">Learning XML</title> </result> <result> <title lang="en">XQuery Kick Start</title> </result> <myQuery>

  23. Esempi basex • Esempio “Ricette” del libro

  24. Espressioni Condizionali XQuery accetta le espressioni del tipo: If - Then - Else Esempio: for $x in doc("books.xml")/bookstore/bookreturn if ($x/@category="CHILDREN")then <child> {data($x/title)} </child>else <adult> {data($x/title)} </adult> Risultato: <adult>Everyday Italian</adult> <child>Harry Potter</child> <adult>Learning XML</adult> <adult>XQuery Kick Start</adult>

  25. Funzioni In XQuery vi sono due tipi di funzioni: • Funzioni built-in; • Funzioni definite dall’utente.

  26. Funzioni built-in • XQuery include più di 100 funzioni built-in. Vi sono funzioni per le stringhe, i valori numerici manipolazione di sequenze, ecc. • L’URI del namespace per le funzioni XQuery è: http://www.w3.org/2005/02/xpath-functions • Il prefisso di default del namespace delle funzioni è fn: • L’utilizzo del prefisso di default nelle chiamate a funzione non è obbligatorio.

  27. Funzioni definite dall’utente • La definizione delle funzioni deve essere dichiarata nel prologo con la sintassi: declare function nome ($x1, $x2, …, $xk) { espr } ; Le variabili $x1, $x2, …, $xk sono visibili all’interno dell’espressione espr. • La sintassi della chiamata è: nome (espr1, espr2, …, esprk)

  28. Tipi per funzioni e parametri • I tipi sequenza sono utilizzati nei parametri e nei risultati delle funzioni: declare function nome ($x1as item()*, …, $xkas item()*) as item()* { espr }; • Si possono utilizzare annotazioni più precise: declare function nome ($x as xs:string) as xs:decimal { espr };

  29. Esempi Basex • Esempio “studenti” del libro

  30. Database XML • Basi di dati relazionali sono potenti con tecnologie (ottimizzazione,concorrenza, indicizzazione, recupero dati) sviluppate da più di 40 anni. • XQuery è un progetto recente, ancora non adatto a grandi applicazioni commerciali. Le implementazioni sono leggere e facili da estendere con nuove caratteristiche XML.

  31. Viste XML di una tabella • Molti sistemi supportano la possibilità di esportare i dati relazionali in un formato XML. • Per esempio SQL/XML è un’estensione di SQL per costruire dati XML come risultato di query SQL. xmlelement(name, "Students", SELECT xmlelement(name, "record", xmlattributes(s.id, s.name, s.age)) FROM Students ) Students <Students> <record id="100026" name="Joe Average" age="21"/> <record id="100078" name="Jack Doe" age="18"/> </Students>

  32. Riferimenti • Libro • http://www.w3schools.com/xquery • http://www.w3.org/XML/Query/ • http://www.w3.org/TR/xquery/

More Related