Technologies internet


Introduction au langage XQuery


Interrogation de documents XML

Comment interroger des documents XML ?

Langages de requêtes pour XML

Expressions XQuery

XQuery est un langage fonctionnel (typé) :

Forme d’une requête

Expressions simples

Expressions complexes

Fonctions

Opérateurs

Exemples

Nous utiliserons le fichier books.xml dans les exemples ci-dessous.

Sélection de noeuds avec FLWOR

L'expression XPath

doc('books.xml')/bookstore/book[price>30]/title

est équivalente à l'expression FLWOR suivante

for $x in doc("books.xml")/bookstore/book
  where $x/price>30
  return $x/title

Le résultat :

<title lang="en">XQuery Kick Start</title>
<title lang="en">Learning XML</title>

Il est possible de trier les éléments avec FLWOR

for $x in doc("books.xml")/bookstore/book
  where $x/price>30
  order by $x/title
  return $x/title

Création de noeuds

<ul>
{
for $x in doc("books.xml")/bookstore/book/title
  order by $x
  return <li>{$x}</li>
}
</ul>

Le résultat :

<ul>
<li><title lang="en">Everyday Italian</title></li>
<li><title lang="en">Harry Potter</title></li>
<li><title lang="en">Learning XML</title></li>
<li><title lang="en">XQuery Kick Start</title></li>
</ul>

Expressions conditionnelles

for $x in doc("books.xml")/bookstore/book
return if ($x/@category="CHILDREN")
  then <child>{data($x/title)}</child>
  else <adult>{data($x/title)}</adult>

Le résultat :

<adult>Everyday Italian</adult>
<child>Harry Potter</child>
<adult>Learning XML</adult>
<adult>XQuery Kick Start</adult>