![Hacker Public Radio artwork](https://is2-ssl.mzstatic.com/image/thumb/Podcasts113/v4/6e/8f/a8/6e8fa80a-9edd-225f-394b-2a6c8c473614/mza_6915183156681029220.png/100x100bb.jpg)
HPR3394: Be an XML star with xmlstarlet
Hacker Public Radio
English - August 05, 2021 00:00 - 12.4 MB - ★★★★ - 34 ratingsTechnology News Tech News community radio tech interviews linux open hobby software freedom Homepage Download IPFS Apple Podcasts Google Podcasts Overcast Castro Pocket Casts RSS feed
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 xmlstarlet.sourceforge.net.