Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


hpr3394 :: Be an XML star with xmlstarlet

Parse XML from the terminal

<< First, < Previous, Latest >>

Hosted by Klaatu on Thursday 2021-08-05 is flagged as Clean and is released under a CC-BY-SA license.
Tags: xml,data,markup,markdown.

Listen in ogg, spx, or mp3 format. | Comments (2)

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/).

Show Transcript

Automatically generated using whisper

whisper --model tiny --language en hpr3394.wav

<< First, < Previous, Latest >>


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2021-08-05T14:54:19Z by norrist

My one cool xmlstarlet trick

I had to install an xml config file on a bunch of servers.
The xml was the same for every server except the hostname had to be added to a specific field.

My first thought was to use sed, but anyone who has tried parsing xml with regex knows just how far I got.

Searching for something like sed that understood xml tags led me to xmlstarlet.
Here is the command I used to add the hostname of the server to the xml path "info/host-id":

xmlstarlet ed --inplace -u info/host-id -v `hostname -f` /path/to/info.xml

Comment #2 posted on 2021-12-22T18:54:26Z by dnt

I consulted this episode this week

When I listened to "We need to talk about XML", I nodded in agreement. Working in localization there's a lot of XLIFF, so I have learned to appreciate it. This week I had a chance to use xmlstarlet at work, so I came back and had another listen to this. There was some trouble figuring out the deal with XML namespaces, I found that in xmlstarlet you can use //_:node where the underscore stands for the default namespace. For now, this just worked, but I do need to learn more about namespaces. Thanks again!

<< First, < Previous, Latest >>

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the P in HPR stand for ?
Are you a spammer →
Who hosted this show →
What does HPR mean to you ?