UP PREVIOUS NEXT   Technologies Internet et Education, © TECFA
  3. XQuery de base

3. XQuery de base

3.1 La notion d'expression de recherche

XQuery est bâti sur des éléments XPath, par exemple

  1. position dans un arbre
    • ex: retourne tous les noeuds <card-propvalue> sous <c3mssoft-desc> ...
//c3msbrick//c3mssoft-desc//card-propvalue
  1. opérations de comparaison
    • ex: retourne seulement le fragment pour une id particulière
//c3msbrick//c3mssoft[@id="epbl"]
  1. functions
    • ex: qui retourne juste le contenu d'un noeud
//c3msbrick/title/text()

3.2 Les expressions FLWOR

Voici un survol:

  1. 1. for = iteration sur une liste de fragments xml et
  2. 2. let = association du résultat d'une expression à une variable
  3. 3. where = condition de sélection
  4. 4. order = tri des résultats
  5. 5. return = expression à retourner

A. "for"

for $variable in expression_recherche
RETURN

"for" associe à chaque $variable une valeur (fragment XML) trouvé pour l'expression XQuery (voir page 7)

Exemple:

for $t in //topic/title

B. let

for $t in document("catalog09.xml")//c3msbrick//c3mssoft-desc//card-propvalue
return $t
for $t in document("catalog09.xml")//c3msbrick
let $desc := $t//c3mssoft-desc//card-propvalue
return $desc
for $t in document("catalog09.xml")//c3msbrick
let $n := count($t//c3mssoft)
return <result> {$t/title/text()} possède {$n} briques </result>

C. where

for $t in document("catalog09.xml")//c3msbrick
let $n := count($t//c3mssoft)
where ($n > 1)
return <result> {$t/title/text()} possède {$n} briques </result>

D. order

for $t in //topic/title order by $t return $t
for $t in document("catalog09.xml")//c3msbrick
let $n := count($t//c3mssoft)
where ($n > 1)
order by $n
return <result> {$t/title/text()} possède {$n} briques </result>
for $t in document("catalog09.xml")//c3msbrick
let $brick_softs := $t//c3mssoft
let $n := count($brick_softs)
where ($n > 0)
order by $n
return <result> 
      For {$t/title/text()} we found {$n} softwares: 
         {$brick_softs//title} 
      </result>
 

E. return

Juste:

for $t in document("catalog09.xml")//c3msbrick
let $n := count($t//c3mssoft)
return <result> 
       {$t/title/text()} possède {$n} briques 
       </result>

Faux:

for $t in document("catalog09.xml")//c3msbrick
let $n := count($t//c3mssoft)
return $t/title/text() possède $n briques 

3.3 Construction de fragments XML

A. Version multi-fragment (collection)

L'expression suivante de base

for $t in //c3msbrick/title
   return $t

retourne une liste (une collection)

1	<title class ="- topic/title " > TECFA Seed Catalog </ title >
2	<title class ="- topic/title " > Introduction </ title >
3	<title class ="- topic/title " > Conceptual and technical framework </ title >
4	<title class ="- topic/title " > The socio-constructivist approach </ title >
....

B. Version un seul fragment

L'expression suivante:

<result> 
 { for $t in //topic/title/text()
 return <titre>{$t}</titre> }
</result>

donne:

<result  >
<titre > TECFA Seed Catalog </ titre >
<titre > Introduction </ titre >
<titre > Conceptual and technical framework </ titre >
<titre > The socio-constructivist approach </ titre > 
....
</result>

3.4 For dans for

 
<result>
<title>List of C3MSBricks and associated Software</title>
{ for $t in document("catalog09.xml")//c3msbrick
  let $brick_softs := $t//c3mssoft
  let $n := count($brick_softs)
  where ($n > 0)
  order by $n descending
  return 
    <brick> Pour {$t/title/text()} on a les {$n} modules suivants:
       { for $soft in $brick_softs
         return <soft> {$soft//title/text()}</soft>
       }
    </brick>
}
</result>
 

Voici le résultat:

<result  >
<title > List of C3MSBricks and associated Software </ title >
<brick > Gallery possède les 5 briques suivants:
<soft > PhotoShare </ soft >
<soft > Photoshare combined with PageSetter </ soft >
<soft > My_eGallery </ soft >
<soft > Coppermine </ soft >
<soft > Gallery </ soft >
</ brick >
<brick > User statistics possède les 5 briques suivants:
<soft > commArt </ soft >
<soft > pncUserPoints </ soft >
<soft > pncSimpleStats </ soft >
<soft > Statistics module </ soft >
<soft > NS-User_Points </ soft >
</ brick > 
.....
</result>

UP PREVIOUS NEXT -- TIE