Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


HPR3394: Be an XML star with xmlstarlet

Hosted by Klaatu on 2021-08-05 00:00:00
Download or Listen

See the layout of an XML document


$ xmlstarlet elements planets.xml
xml
xml/sol
xml/sol/planet
xml/sol/planet/name
xml/sol/planet/albedo
xml/sol/planet
xml/sol/planet/name
xml/sol/planet/albedo
xml/sol/planet
xml/sol/planet/name
xml/sol/planet/albedo

See content of the planet node


$ xmlstarlet select -t --value-of '/xml/sol/planet' planets.xml

        Mercury
        0.11

        Venus
        0.7

        Terra
        0.39

Get the third instance of the planet node


$ xmlstarlet select -t --value-of '/xml/sol/planet[3]' planets.xml
Terra
0.39

Get only the planets with an albedo greater than 0.25


$ xmlstarlet select -t --value-of '/xml/sol/planet[albedo > 0.25]' planets.xml

Venus
0.7

Terra
0.39

Get only the planets closer to Sol than the third planet


$ xmlstarlet select -t --value-of '/xml/sol/planet[position() < 3]' planets.xml

Mercury
0.11

Venus
0.7

Learn more XPath functions at Mozilla Developer Network.

Download xmlstarlet from xmlstar.sourceforge.net (https://sourceforge.net/projects/xmlstar/).

Comments



More Information...


Copyright Information

Unless otherwise stated, our shows are released under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.

The HPR Website Design is released to the Public Domain.