You are viewing illiterat

James Antill - XML grep

Sep. 26th, 2011

03:05 pm - XML grep

Previous Entry Share

For a long time I've wanted to be able to do an "XML grep" on XML data, where I see just the nodes I care about in an XML file. After recently hitting this problem again, I found out about xmlstarlet and after a _lot_ of work I managed to get what I wanted. So I figured I'd write it down, for both of us:

xmlstarlet sel -I -t \
  -m '/updates/update' \
  -i 'pkglist/collection/package[@name="raydium"]' \
  -c . \
  /var/cache/yum/x86_64/15/updates/gen/updateinfo.xml


...I'll explain each line of the above:


  • {xmlstarlet sel -I -t} -- Runs the command, in "select" mode and does automatic indentation of the output.


  • {-m '/updates/update'} -- Where we are matching "from", comparing this to grep we are kind of saying that each node at <updates><update> is a line. This is an XPATH expression.

  • {-i 'pkglist/collection/package[@name="raydium"]'} -- The condition we want to match on, here we are saying that we want to match on anything of the form <updates><update><pkglist><collection><package name="raydium">. This is an XPATH expression (it's worth repeating the @ changes the match from the node to the attribute).

  • {-c .} -- This says display all the nodes from the start of our match #2, that pass our condition.

  • { /var/cache/yum/x86_64/15/updates/gen/updateinfo.xml } -- The XML file to operate on (if it's missing it default to stdin.

Tags: ,

Comments:

[User Picture]
From:pfrields.id.fedoraproject.org
Date:September 26th, 2011 10:50 pm (UTC)

Yay!

(Link)
Very happy that someone is using this Fedora package which I've maintained for a long time. It's an interesting little toolkit though I wouldn't go so far as to call it user friendly. Glad it was of use for you.