XMLStarlet
XMLStarlet is a set of command line utilities (toolkit) to query, transform, validate, and edit XML documents and files using a simple set of shell commands in a way similar to how it is done with UNIX grep, sed, awk, diff, patch, join, etc commands. This set of command line utilities can be used by those who want to test XPath query or execute commands on the fly as well as deal with many XML documents or for automated XML processing with shell scripts. FeaturesThe toolkit's feature set includes the following options:
The XMLStarlet command line utility is written in C and uses libxml2 and libxslt. Implementation of extensive choice of options for XMLStarlet utility was only possible because of rich feature set of both libraries: libxml2 and libxslt. XMLStarlet is linked statically to both libxml2 and libxslt, so generally all you need to process XML documents is one executable file. XMLStarlet is open source free software released under an MIT License which allows free use and distribution for both commercial and non-commercial projects. ExamplesConsider the following XML document 'xmlfile1.xml' example: <?xml version="1.0" encoding="utf-8"?>
<wikimedia>
<projects>
<project name="Wikipedia" launch="2001-01-05">
<editions>
<edition language="English">en.wikipedia.org</edition>
<edition language="German">de.wikipedia.org</edition>
<edition language="French">fr.wikipedia.org</edition>
<edition language="Polish">pl.wikipedia.org</edition>
<edition language="Spanish">es.wikipedia.org</edition>
</editions>
</project>
<project name="Wiktionary" launch="2002-12-12">
<editions>
<edition language="English">en.wiktionary.org</edition>
<edition language="French">fr.wiktionary.org</edition>
<edition language="Vietnamese">vi.wiktionary.org</edition>
<edition language="Turkish">tr.wiktionary.org</edition>
<edition language="Spanish">es.wiktionary.org</edition>
</editions>
</project>
<project name="Wikiversity" launch="2006-10-04">
<editions>
<edition language="English">en.wikiversity.org</edition>
</editions>
</project>
</projects>
</wikimedia>
On a command prompt the following five XPath queries are executed on the above XML file 'xmlfile1.xml'.
$ xmlstarlet sel -t -v "//wikimedia/projects/project/@name" xmlfile1.xml
Wikipedia
Wiktionary
Wikiversity
$ xmlstarlet sel -t -v "/wikimedia/projects/project[last()]/@*" xmlfile1.xml
Wikiversity
2006-10-04
$ xmlstarlet sel -t -v "/wikimedia/projects/project[@name='Wiktionary']/editions/edition" xmlfile1.xml
en.wiktionary.org
fr.wiktionary.org
vi.wiktionary.org
tr.wiktionary.org
es.wiktionary.org
$ xmlstarlet sel -t -v "/wikimedia/projects/project[@name='Wiktionary']/editions/edition[@language!='Turkish' and @language!='Spanish']" xmlfile1.xml
en.wiktionary.org
fr.wiktionary.org
vi.wiktionary.org
$ xmlstarlet sel -t -v "/wikimedia/projects/project/editions/edition[position() >= 3]/@*" xmlfile1.xml
French
Polish
Spanish
Vietnamese
Turkish
Spanish
An XML document can be validated against an XSD schema saved in file 'xsdfile.xsd' as follows: $ xmlstarlet val -e -s xsdfile.xsd xmlfile1.xml
xmlfile1.xml - valid
See also
NotesExternal links |