Il y a 389 articles et 19 utilisateurs inscrits (ce bandeau est défini dans MediaWiki:Sitenotice)

« Xml Tester des XPath » : différence entre les versions

De AJLSM
Aller à la navigation Aller à la recherche
→XMLStarlet : Exemple avancé
→Exemple avancé : Valeur de tous les nœuds
Ligne 25 : Ligne 25 :
** l'option {{cmd|-c}} ou {{cmd|--copy-of}} équivaut à un {{tag|xsl:copy-of}} ;
** l'option {{cmd|-c}} ou {{cmd|--copy-of}} équivaut à un {{tag|xsl:copy-of}} ;
** l'option {{cmd|-v}} ou {{cmd|--value-of}} équivaut à un {{tag|xsl:value-of}}.
** l'option {{cmd|-v}} ou {{cmd|--value-of}} équivaut à un {{tag|xsl:value-of}}.
=== Valeur de '''tous''' les nœuds ===
<syntaxhighlight lang="bash">
xmlstarlet sel -N csv="http://apache.org/cocoon/csv/1.0" \
-t -m "//csv:record[csv:field[@column = 'type' and text()='almanach']]" -v "csv:field[@column='type']" -n \
./csv-almanachs.xml
</syntaxhighlight>
Explication :
<syntaxhighlight lang="bash">
xmlstarlet sel \
-N csv="http://apache.org/cocoon/csv/1.0" \
-t # template à appliquer \
  -m "//csv:record[csv:field[@column = 'type' and text()='almanach']]" # on match ce genre de nœud \
  -v "csv:field[@column='type']" # une fois matcher, on affiche cette valeur \
  -n # saut de ligne entre chaque match \
./csv-almanachs.xml # document d'entrée
</syntaxhighlight>


== Références ==  
== Références ==  

Version du 7 août 2012 à 16:26

GUI

Eclipse

On installera les plugins suivants qui sont disponible dans Eclipse Web Tools Platform :

  • XSL Tools SDK
    • PscyhoPath XPath 2.0 Processor Feature
    • XSL Tools Developer Resources

Remarques : On fera attention au processeur que l'on utilise : XPath 1.0 ou XPath 2.0.

Terminal

Utilitaire xpath

Cela correspond au paquet linux : libxml-xpath-perl disponible sur [[1]]

xpath -e '//archref/@href'

Pour n'avoir que les noeud, on rajoutera une redirection des erreurs à la fin de la commande : 2&>1

XMLStarlet

On peut également utiliser XMLStarlet, qui est open source, gratuit. Et disponible sur Windows et Linux.

xmlstarlet sel -t -v "count(/xml/table/rec/numField)" table.xml

Exemple avancé

xmlstarlet sel -N xhtml="http://www.w3.org/1999/xhtml" -t -c "/xhtml:html/xhtml:head/xhtml:link" /tmp/bmo/bmoxml-basket.xml
  • sel : sélection de noeud dans le document XML ;
  • -N xhtml="http://www.w3.org/1999/xhtml" : définition d'un espace de nom<ref>Namespaces and default namespace</ref> ;
  • -t -c "/xhtml:html/xhtml:head/xhtml:link" : le template XPath que l'on applique pour obtenir nos résultats<ref>Querying XML documents</ref>.
    • l'option -c ou --copy-of équivaut à un <xsl:copy-of> ;
    • l'option -v ou --value-of équivaut à un <xsl:value-of>.

Valeur de tous les nœuds

xmlstarlet sel -N csv="http://apache.org/cocoon/csv/1.0" \
-t -m "//csv:record[csv:field[@column = 'type' and text()='almanach']]" -v "csv:field[@column='type']" -n \
./csv-almanachs.xml

Explication :

xmlstarlet sel \
 -N csv="http://apache.org/cocoon/csv/1.0" \
 -t # template à appliquer \
  -m "//csv:record[csv:field[@column = 'type' and text()='almanach']]" # on match ce genre de nœud \
  -v "csv:field[@column='type']" # une fois matcher, on affiche cette valeur \
  -n # saut de ligne entre chaque match \
./csv-almanachs.xml # document d'entrée

Références

<references/>