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

## Pré-requis
  * NodeJS
  * xmlstarlet
  * libxml2
  * expat
  * perl
  * 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

# test sur un gros fichier (24Mo)
./parse-doctype node_modules/get-doctype/test/dataset/big.xml

# utilisation en passant par stdin
cat node_modules/get-doctype/test/dataset/local.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))


```xml
<?xml version="1.0" encoding="UTF-8"?>
<component xmlns="http://www.wiley.com/namespaces/wiley" version="2.0" type="serialArticle" xml:lang="en">
	<header>
		<publicationMeta level="product">
			<publisherInfo>
				<publisherName>Wiley Subscription Services, Inc., A Wiley Company</publisherName>
				<publisherLoc>Hoboken</publisherLoc>
			</publisherInfo>
      		...
			<issn type="print">1532-2882</issn>
			<issn type="electronic">1532-2890</issn>
      		<selfCitationGroup>
        		<citation type="ancestor" xml:id="cit1">
					...
        			<pubYear year="2000">2000</pubYear>
        		</citation>
			</selfCitationGroup>
		</publicationMeta>
		<publicationMeta level="part" position="90">
			...
			<coverDate startDate="2011-09">September 2011</coverDate>
		</publicationMeta>
	</header>
	<body sectionsNumbered="no">
		<bibliography style="nameDate">
			<title type="main">References</title>
			<bib xml:id="bib1">
				<citation type="other" xml:id="cit2">
					...
          			<pubYear year="2008">2008</pubYear>
        		</citation>
      		</bib>
      		<bib xml:id="bib2">
        		<citation type="book" xml:id="cit3">
					...
          			<pubYear year="2007">2007</pubYear>
        		</citation>
      		</bib>
      		<bib xml:id="bib3">
        		<citation type="book" xml:id="cit4">
					...
          			<pubYear year="2010">2010</pubYear>
        		</citation>
      		</bib>
      	...
    	</bibliography>
	</body>
</component>
```


```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
xmlstarlet 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

# Commande équivalente utilisant xsltproc
xsltproc --stringparam idistex MON_ID_AVEC_XSLTPROC --stringparam datecreation 08/01/2016  --output tmp/wiley.mods.xml xslt/Wileyml3g.xsl xml/wiley-sample.xml
```

## Correction d'espaces de nommage
Outil en ligne de commande écrit en perl par Romain Loth : [add_els_ns](https://git.istex.fr/istex/singleUse/tree/master/elsevier-ns-modify)

```bash
# Affichage du fichier XML utilisant des namespaces non déclarés
firefox xml/elsevier-sample.xml

# Correction du fichier
perl tools/add_els_ns.pl xml/elsevier-sample.xml
```

## Modules NPM

### xml-mapping

Module NPM de transformation XML <-> JSON
([README du module](https://github.com/Inist-CNRS/node-xml-mapping))

### xpath

Module NPM de recherche dans un document XML parsé en DOM
([README du module](https://github.com/goto100/xpath))

### node-expat

Module NPM permettant de parcourir un document via une API SAX.

Documentation : http://node-xmpp.org/doc/expat.html

Ci-après un exemple un peu plus complexe qui transforme un fichier XML vers un syntaxe "JSON-like".

XML à traiter :

```xml
<?xml version="1.0" encoding="utf-8"?>
<atelier numero="3">
  <nom>Outils XML ISTEX</nom>
  <date jour="08" mois="01" annee="2016"/>
  <auteurs>
    <!-- just the best ! -->
    <auteur>
      <nom>Niederlender</nom>
      <prenom>Claude</prenom>
    </auteur>
    <auteur>
      <nom>Caron</nom>
      <prenom>Étienne</prenom>
    </auteur>
  </auteurs>
</atelier>
```

appel du [accès au script lui-même](https://git.istex.fr/istex/ade-outils-xml/blob/master/tools/sax-parser-sample.js) :

```bash
tools/sax-parser-sample.js xml/minimal.xml
```