« XPath » : différence entre les versions
De bricosoft
m Nettoyage : gabarit 2012 (widgets Twitter/Google/DISQUS morts et fichiers fantômes) |
m Remplacement des balises <source> obsolètes par <syntaxhighlight> (bot) |
||
| Ligne 38 : | Ligne 38 : | ||
===tests=== | ===tests=== | ||
$ curl -s "http://fr.wikipedia.org/w/index.php?title=Spécial:Modifications_récentes&feed=atom&limit=7" | xpath -q -e "//entry/title" | $ curl -s "http://fr.wikipedia.org/w/index.php?title=Spécial:Modifications_récentes&feed=atom&limit=7" | xpath -q -e "//entry/title" | ||
< | <syntaxhighlight lang="xml"> | ||
<title>Universit� nationale d'art (Bucarest)</title> | <title>Universit� nationale d'art (Bucarest)</title> | ||
<title>Ch�rten</title> | <title>Ch�rten</title> | ||
| Ligne 46 : | Ligne 46 : | ||
<title>Ludivine Aubourg</title> | <title>Ludivine Aubourg</title> | ||
<title>Django Unchained</title> | <title>Django Unchained</title> | ||
</ | </syntaxhighlight> | ||
==Les axes== | ==Les axes== | ||
Dernière version du 8 novembre 2026 à 17:46
XPath est un langage de requête pour documents xml. Il permet de parcourir le DOM afin de sélectionner des noeuds(enfants et/ou parents) xml avec ou sans critères.
exemples de syntaxe
/: recupère le noeud racine et tous les sous-noeuds, càd le premier noeud juste après</xml>//fruit: recupère tous les noeuds</fruit>ou qu'ils se trouvent dans le document.//fruit/pomme: recupère tous les noeuds</pomme>fils de</fruit>.//fruit/pomme[@cultivar='granny smith']: recupère tous les noeuds</pomme>avec comme attribut cultivar valant granny smith fils de</fruit>.
...
mumuse en CLI
Le CLI t'autorise (<--- hahaha) de faire mumuse avec xpath et des pages ouaibes.
install
$ xpath bash: /usr/bin/xpath: Aucun fichier ou dossier de ce type
qlb ?
$ sudo aptitude install libxml-xpath-perl
usage
$ xpath
Usage:
/usr/bin/xpath [options] -e query [-e query...] [filename...]
If no filenams are given, supply XML on STDIN.
You must provide at least one query. Each supplementary
query is done in order, the previous query giving the
context of the next one.
Options:
-q quiet. Only output the resulting PATH
-s suffix use suffix instead of linefeed.
-p postfix use prefix instead of nothing.
tests
$ curl -s "http://fr.wikipedia.org/w/index.php?title=Spécial:Modifications_récentes&feed=atom&limit=7" | xpath -q -e "//entry/title"
<title>Universit� nationale d'art (Bucarest)</title>
<title>Ch�rten</title>
<title>Antoine Charma</title>
<title>Tour de France (cyclisme)</title>
<title>Amphenol socapex</title>
<title>Ludivine Aubourg</title>
<title>Django Unchained</title>
Les axes
| syntaxe | syntaxe abbrégée | Notes |
|---|---|---|
ancestor |
||
ancestor-or-self |
||
attribute
|
@
|
@abc est l'abbrégé de attribute::abc
|
child |
xyz est l'abbrégé de child::xyz
| |
descendant |
||
descendant-or-self
|
//
|
// est l'abbrégé de /descendant-or-self::node()/
|
following |
||
following-sibling |
||
namespace |
||
parent
|
..
|
.. est l'abbrégé de parent::node()
|
preceding |
||
preceding-sibling |
||
self
|
.
|
. est l'abbrégé de self::node()
|
xpath vs xquery vs xpointer vs xtireur
- XPath : déplacement dans le DOM à la recherche de noeuds particuliers
- XQuery : propose une syntaxe proche de SQL : FLWOR (FOR, LET, WHERE, ORDER BY et RETURN)
- XPointer : c'est XPath + extensions pour retrouver une partie de document
- XTireur : n'existe pas.
et pour l'HTML ?
A moins de visiter des pages webs en xml, xpath ne vous sera d'aucune utilité [1] :
$ curl -s "http://www.666.wf" | xpath -q -e "//header/title"
error in processing external entity reference at line 1, column 89, byte 89: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> ========================================================================================^ <html> <head> at /usr/lib/perl5/XML/Parser.pm line 187
$ curl -s "http://w3c.com" | xpath -q -e "//header/title"
not well-formed (invalid token) at line 5, column 90, byte 214:
<title>w3c.com: The Best Search Links on the Net</title>
<script type="text/javascript">;
if(self != top) top.location.href = 'http://'+location.hostname+'/?redir=frame&uid=w3c512b7b2a10ccf6.48689816';
=========================================================================================^
</script>
<script type="text/javascript" src="http://return.bs.domainnamesales.com/return_js.php?d=w3c.com&s=1361804074"></script>
at /usr/lib/perl5/XML/Parser.pm line 187
La soluce est d'utiliser un autre programme comme xmllint.