Skip to content

Instantly share code, notes, and snippets.

@kerphi
Created August 19, 2014 15:10
Show Gist options
  • Save kerphi/e6975df924bed2d774b6 to your computer and use it in GitHub Desktop.
Save kerphi/e6975df924bed2d774b6 to your computer and use it in GitHub Desktop.
Utilisation d'xmlstarlet pour supprimer d'un document XML des éléments ciblés par une liste de XPATH
echo '<?xml version="1.0"?>
<xml>
<table>
<rec id="1">
<numField>123</numField>
<stringField>String Value</stringField>
</rec>
<rec id="2">
<numField>346</numField>
<stringField>Text Value</stringField>
</rec>
<rec id="3">
<numField>-23</numField>
<stringField>stringValue</stringField>
</rec>
</table>
</xml>' | xmlstarlet ed -d "/xml/table/rec[@id='2']" -d "/xml/table/rec[@id='1']"
@kerphi
Copy link
Author

kerphi commented Aug 19, 2014

retourne ceci (id 1 et id 2 ont été retirés) :

<?xml version="1.0"?>
<xml>
  <table>
    <rec id="3">
      <numField>-23</numField>
      <stringField>stringValue</stringField>
    </rec>
  </table>
</xml>

@kerphi
Copy link
Author

kerphi commented Aug 19, 2014

Exemple de filtrage XPATH directement sur un fichier ECCO :

curl -s http://api.istex.fr/document/acea79e0d184a3929b5618a2f55fbaae4c4542db/metadata/xml | \
  xmlstarlet ed -d "/book/text/page"

La commande retourne le document sans le contenu des pages du livre.

@kerphi
Copy link
Author

kerphi commented Aug 19, 2014

En terme de perf, sur ma machine locale, l'exécution de 100 nettoyage à coup de XPATH sur le fichier témoin d'ECCO (qui pèse 5Mo) prend 14 secondes.

Cf la ligne de commande utilisée :

curl -s http://api.istex.fr/document/acea79e0d184a3929b5618a2f55fbaae4c4542db/metadata/xml > ./ecco-test-file.xml
time for i in $(seq 1 100); do   xmlstarlet ed -d "/book/text/page" ./ecco-test-file.xml >/dev/null; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment