Newer
Older
ade-outils-xml / README.md
# ADE Outils XML ISTEX
08 janvier 2016

## Pré-requis
  * NodeJS
  * xmlstarlet
  * libxml2
  * expat
  * j'en oublie sûrement !!! ;-)

## Installation

```bash
git clone ssh://vsistex.intra.inist.fr:22222/istex/ade-outils-xml.git
cd ade-outils-xml
npm install
```

## XMLWF
Mon fichier XML est-il bien formé ?
  * balises ouvrantes bien fermées
  * balises qui "ne se chevauchent pas"
  * pas de caractères `&<>;,` dans les attributs ou noeuds texte
  * ...

```bash
# test sur un XML bien formé
xmlwf  xml/oup-sample.xml
echo $?

# test sur un XML mal formé
xmlwf  xml/bad-formed.xml
echo $?

# le même test en passant par stdin
cat xml/bad-formed.xml | xmlwf

# test d'un répertoire entier
xmlwf  xml/*
```

NB : Valeur de retour toujours 0, même si bien formé. Obligation de vérifier stdout si on souhaite automatiser

## get-doctype
Récupération des informations contenues dans la ligne doctype.

```bash
# récupération d'un doctype "classique"
./parse-doctype xml/oup-sample.xml

# cas d'un XML sans doctype
./parse-doctype xml/wiley-sample.xml

# cas d'un XML mal formé
./parse-doctype xml/parsing-problem.xml

# utilisation en passant par stdin
cat node_modules/get-doctype/test/dataset/big.xml | ./parse-doctype
```

Plus d'infos sur https://github.com/Inist-CNRS/get-doctype


## xmlstarlet
Site officiel : http://xmlstar.sourceforge.net/

### xmlstarlet sel (select)
Interrogation de documents XML en ligne de commande. Possibilité de faire des requêtes complexes via XPath.
([documentation officielle](http://xmlstar.sourceforge.net/doc/UG/ch04.html))

```bash
# sélection d'une valeur dans un noeud texte
xmlstarlet sel -t -v "//_:publisherInfo/_:publisherName" -n xml/wiley-sample.xml

# sélection d'une valeur dans un attribut
xmlstarlet sel -t -v "//_:coverDate/@startDate" -n xml/wiley-sample.xml

# sélection d'une valeur avec conditionnelle
xmlstarlet sel -t -v "/_:component/_:header//_:issn[@type='print']" -n xml/wiley-sample.xml

# Utilisation du template -m <=> xsl:for-each
xmlstarlet sel -t -m "//_:citation" -v "_:pubYear/@year" -n  xml/wiley-sample.xml

```

### xmlstarlet val (validate)
Validation de documents selon DTD, Schéma XML ou RelaxNG.
([documentation officielle](http://xmlstar.sourceforge.net/doc/UG/ch04s04.html))

```bash
# Validation contre DTD
xmlstarlet val -e -d dtd/Wileyml3gv20-flat/Wileyml3gv20-flat.dtd xml/wiley-sample.xml

# Validation contre RelaxNG
xmlstarlet val -e -r dtd/Wileyml3gv20-rng/Wileyml3g.rng xml/wiley-sample.xml

# Echec de validation
val -e -r dtd/Wileyml3gv20-rng/Wileyml3g.rng xml/not-valid.xml
```

### xmlstarlet tr (transform)
Transformation XSLT de documents XML.
([documentation officielle](http://xmlstar.sourceforge.net/doc/UG/ch04s02.html))

```bash
# Transformation vers le format MODS / résultat sur stdout
xmlstarlet tr xslt/Wileyml3g.xsl xml/wiley-sample.xml

# Transformation vers le format MODS / résultat indenté sur stdout
xmlstarlet tr xslt/Wileyml3g.xsl xml/wiley-sample.xml | xmllint --format -

# Transformation vers le format MODS / résultat dans un fichier
xmlstarlet tr xslt/Wileyml3g.xsl xml/wiley-sample.xml > tmp/wiley.mods.xml

# Transformation avec passage de paramètre xslt
xmlstarlet tr xslt/Wileyml3g.xsl -s idistex=MON_ID_A_MOI -s datecreation="08/01/2016" xml/wiley-sample.xml | xmllint --format - >  tmp/wiley.mods.xml

```

##