« Programmation XML/XPath » : différence entre les versions

Contenu supprimé Contenu ajouté
Aucun résumé des modifications
Ligne 1 :
{{programmation XML}}
 
{{w|XPath}} est un aspect fondamental de XML. Vous devez le maîtriser pour aborder la suite. En effet, la rédaction de documents, XSLT, XQuery fait appel à XPath.
 
== Syntaxe ==
Le {{w|XPath}} est un langage de sélection d'éléments XML, appelés "nœuds".
 
Par exemple "/" sélectionne tout le fichier XML. "/nom du nœud" tout ce qui est compris dans l'élément nommé. Il existe un interpréteur en ligne pour réaliser les exemples décrits ci-dessous : http://www.xpathtester.com/.
 
Ces expressions sont appelées "chemin de localisation", composés d'un ou plusieurs "pas de localisation" (ou "étapes") séparés par des "/". Les pas de localisation ont chacun trois composants :
# un axe (parent, descendant...) ;
# un test de nœud (nom ou fonction désignant les nœuds) ;
# des prédicats (entre crochets).
 
=== Axes ===
Pour décrire les relations entre les nœuds, XPath utilise le vocabulaire suivant :
{|class="wikitable"
Ligne 43 ⟶ 51 :
|}
 
=== ExempleTests de nœuds ===
Node tests may consist of specific node names or more general expressions. In the case of an XML document in which the namespace prefix <code>gs</code> has been defined, <code>//gs:enquiry</code> will find all the <code>enquiry</code> elements in that namespace, and <code>//gs:*</code> will find all elements, regardless of local name, in that namespace.
 
Other node test formats are:
;comment() :finds an XML comment node, e.g. <code><nowiki><!-- Comment --></nowiki></code>
;text() :finds a node of type text, e.g. the <code>hello</code> in <code><k>hello<m> all</m></k></code>
;processing-instruction() :finds XML [[processing instruction]]s such as <code><?php echo $a; ?></code>. In this case, <code>processing-instruction('php')</code> would match.
;node() :finds any node at all.
 
 
=== Prédicats ===
Les prédicats sont des fonctions filtrant les nœuds évalués à "false", qui se placent à la fin des sélections<ref>http://www.w3.org/TR/xpath#corelib</ref> :
 
//p[@classe="alerte"]
 
{|class="wikitable"
! Prédicats !! Notes
|-
|<code>number '''last'''()</code> || last node in the current node set
|-
|number '''position'''() || position of the context node being tested
|-
|number '''count'''(node-set) || the number of nodes in a node-set
|-
|boolean '''starts-with'''(string, string) || returns true if the first argument starts with the second
|-
|boolean '''contains'''(string, string) || returns true if the first argument contains the second
|-
|number '''sum'''(node-set) || the sum of the numeric values of the nodes in the node-set
|-
|number '''floor'''(number) || the number, rounded down to the nearest integer
|-
|number '''ceiling'''(number) || the number, rounded up to the nearest integer
|-
|number '''round'''(number) || the number, rounded to the nearest integer
|}
 
== Exemples ==
Soit l'arborescence suivante :
<source lang="xml">
Ligne 84 ⟶ 129 :
*# Non abrégé : parent::node()
 
==Références==
Ces expressions sont appelées "chemin de localisation", composés d'un ou plusieurs "pas de localisation" (ou "étapes") séparés par des "/". Les pas de localisation ont chacun trois composants :
<references/>
# un axe (parent, descendant...) ;
# un test de nœud (nom ou fonction désignant les nœuds) ;
# des prédicats (entre crochets).
 
[[en:XML - Managing Data Exchange/XPath]]