Utilisé par beaucoup de dérivés de XML, en modélisant le document XML sous forme d'un arbre, il permet d'associer à tout objet XML une chaîne permettant d'y accéder à l'aide de :
Exemple:
<book title="Starship Titanic" price="14"> <author name="Douglas Adams"/> <author name="Terry Jones"/> "Where is Leovinus?" demanded the Gat of Blerontis </book>
XPath | Correspondance |
/book/author[2] | <author name="Terry Jones"/> |
/book/author/@name | Douglas Adams Terry Jones |
/book/author | <author name="Douglas Admas"/> |
<author name="Terry Jones"/> | |
/book/text() | "Where is Leovinus?" demanded the Gat of Blerontis |
Accès aux noeuds :
<x><y><z/></y><t><y><z>foo</z></y></t><z>1</z></x> "//y/z" --> <z/> <z>foo</z> "//z" --> <z/> <z>foo</z> <z>1</z>
<x><y><z/></y><t><y><z>foo</z></y></t><z>1</z></x> "/*/*/*/*" --> <z>foo</z>
<x><y><z/></y><t><y><z>foo</z></y></t><z>1</z></x> "//y/z/*" --> <z/> <z>foo</z>
<x><y><z/></y><t><y><z>foo</z></y></t><z>1</z></x> "//y/z/.." --> <y><z/></y> <y><z>foo</z></y>
Accès aux attributs : Le principe est le même, il suffit de préfixé le champ par le caractère @.
<circle col="red"><disc><rect col="blue"/></disc></circle> "//rect/@col"-> blue
<circle col="red"><disc><rect col="blue"/></disc></circle> "//@*" -> red blue