14 juin 2005

xmllint, un couteau suisse pour les fichiers XML

L'utilitaire xmllint fourni avec la librairie multi-platorme libxml2 permet de manipuler, valider, formater des fichiers XML. Visite guidée :

Indenter un fichier

% xmllint data.xml --format

Valider un fichier XML contenant une DTD.

% xmllint data.xml --valid

Valider un fichier XML

avec une DTD externe :

% xmllint data.xml --dtdvalid data.dtd  --noout

avec un schéma XML

% xmllint data.xml --schema schema.xsd  --noout 

avec du RelaxNG

% xmllint data.xml --relaxng RNGnotice.rng --noout 

L'option --noout permet d'afficher uniquement les informations de validation.

Naviguer dans un fichier XML

Vous allez pouvoir à l'aide d'expression xpath naviguer dynamiquement dans votre fichier xml. Démonstration :

Toutes les options :

thouveni@localhost% xmllint --shell stow.xml 
/ > help
        base         display XML base of the node
        setbase URI  change the XML base of the node
        bye          leave shell
        cat [node]   display node or current node
        cd [path]    change directory to path or to root
        dir [path]   dumps informations about the node (namespace, attributes, content)
        du [path]    show the structure of the subtree under path or the current node
        exit         leave shell
        help         display this help
        free         display memory usage
        load [name]  load a new document with name
        ls [path]    list contents of path or the current directory
        xpath expr   evaluate the XPath expression in that context and print the result
        pwd          display current working directory
        quit         leave shell
        save [name]  save this document to name or the original name
        write [name] write the current node to the filename
        validate     check the document for errors
        relaxng rng  validate the document agaisnt the Relax-NG schemas
        grep string  search for a string in the subtree
/ > 

Naviguer avec xpath

thouveni@localhost% xmllint --shell stow.xml
/ > cd /slides/foil[2]/title
title > ls
t--       16 Principe de base
title > cd ../..
slides > ls
t--        5      
---        9 slidesinfo
t--        6       
---       13 foil
t--        5      
---       13 foil
t--        5      
---       13 foil
t--        5      
---       13 foil
t--        5      
---        7 foil
t--        1  
slides > dir
ELEMENT slides
slides > cd ..
/ > cat //foil[2]/title
 -------
<title>Principe de base</title>
/ > 

3 commentaires:

  1. Merci pour l'info
    C'est génial. J'ai cherche des informations sur comment valider des documents xml a partir d'un schéma et je trouve en plus la facon de parcourir des fichiers avec xpath.

    RépondreSupprimer
  2. Ce commentaire a été supprimé par l'auteur.

    RépondreSupprimer