ROXI vs XQuery

20 March, 2007 (23:01) | programming, roxi, ruby, xml

L’intento di ROXI è quello di raggiungere un’espressività tale da rendere il meno doloroso possibile l’avere a che fare con dei documenti XML. Quale linguaggio migliore di XQuery per interrogare tali documenti? ROXI riesce a raggiungere la sua espressività? All’epoca mi sfidai a tradurre alcuni degli use case ufficiali del W3C in ROXI. Quello che segue è un esempio del risultato.

Supponiamo di avere un file XML bib.xml

01: <?xml version="1.0" encoding="utf-8"?>
02: <bib>
03:     <book year="1994">
04:         <title>TCP/IP Illustrated</title>
05:         <author><last>Stevens</last><first>W.</first></author>
06:         <publisher>Addison-Wesley</publisher>
07:         <price>65.95</price>
08:     </book>
09:
10:     <book year="1992">
11:         <title>Advanced Programming in the Unix environment</title>
12:         <author><last>Stevens</last><first>W.</first></author>
13:         <publisher>Addison-Wesley</publisher>
14:         <price>65.95</price>
15:     </book>
16:
17:     <book year="2000">
18:         <title>Data on the Web</title>
19:         <author><last>Abiteboul</last><first>Serge</first></author>
20:         <author><last>Buneman</last><first>Peter</first></author>
21:         <author><last>Suciu</last><first>Dan</first></author>
22:         <publisher>Morgan Kaufmann Publishers</publisher>
23:         <price>39.95</price>
24:     </book>
25:
26:     <book year="1999">
27:         <title>The Economics of Technology and Content for Digital TV</title>
28:         <editor>
29:                <last>Gerbarg</last><first>Darcy</first>
30:                 <affiliation>CITI</affiliation>
31:         </editor>
32:             <publisher>Kluwer Academic Publishers</publisher>
33:         <price>129.95</price>
34:     </book>
35:
36: </bib>

Lo use case 1.1.9.6 Q6 “per ogni libro che possiede almeno un autore, estrarre come elementi il titolo e i primi due autori, ed eventualmente un elemento “et-al” vuoto se ci sono ulteriori autori” in XQuery viene implementato così

01: <bib>
02:   {
03:     for $b in doc("bib.xml")//book
04:     where count($b/author) > 0
05:     return
06:         <book>
07:             { $b/title }
08:             {
09:                 for $a in $b/author[position()<=2]
10:                 return $a
11:             }
12:             {
13:                 if (count($b/author) > 2)
14:                  then <et-al/>
15:                  else ()
16:             }
17:         </book>
18:   }
19: </bib>

In ROXI invece può essere raggiunto lo stesso risultato in questo modo

01: require 'roxi/xquery'
02: include ROXI
03:
04: element = XElement.new('bib',
05:   XQuery.from(XDocument.open('bib.xml').xpath('//book')) do
06:     where { | book | book.children('author').size > 0 }
07:     select { | book |
08:       XElement.new('book',
09:         book.child('title'),
10:         book.xpath('author[position()<3]'),
11:         if (book.children('author').size > 2)
12:           XElement.new('et-al')
13:         end
14:       )
15:     }
16:   end
17: )

Considerando che è puro codice Ruby non è male vero? :-)

Entrambi producono lo stesso risultato

01: <bib>
02:     <book>
03:         <title>TCP/IP Illustrated</title>
04:         <author>
05:             <last>Stevens</last>
06:             <first>W.</first>
07:         </author>
08:     </book>
09:     <book>
10:         <title>Advanced Programming in the Unix environment</title>
11:         <author>
12:             <last>Stevens</last>
13:             <first>W.</first>
14:         </author>
15:     </book>
16:     <book>
17:         <title>Data on the Web</title>
18:         <author>
19:             <last>Abiteboul</last>
20:             <first>Serge</first>
21:         </author>
22:         <author>
23:             <last>Buneman</last>
24:             <first>Peter</first>
25:         </author>
26:         <et-al/>
27:     </book>
28: </bib>

Nota: se volete utilizzare ROXI, fatelo, tenendo a mente che non è ancora stata rilasciata ufficialmente e quindi aspettatevi qualche problemuccio :-)

Write a comment