Home > Mobile >  XML query with xmlstarlet
XML query with xmlstarlet

Time:11-24

I have an xml document (it's a config file for a game) that looks something like:

<properties>
  ...
  <settings>
    <fooBar value="abc"/>
    <uffDa value="1.23"/>
    ...
  </settings>
  ...
</properties>

I'm trying to get all of the settings printed in a format like

fooBar abc
uffDa 1.23
...

I've used xmlstarlet to query a few attributes but that's it. I don't have any experience working with xml and even with the help docs I had some difficulty just figuring out that much.

The best I've been able to do is narrow it down to the settings with

xml sel -t -c "/properties/settings" <xml file>

but that includes the parent settings tags in the output. I also tried

xml sel -t -c "/properties/settings/*" <xml file>

which didn't include the settings tags in the output, but it didn't include any line-breaks either.

Technically, either output could be used for what I'm trying to do (compare the settings from two config files) but I'm hoping to get something more concise.

CodePudding user response:

Try something like this

xml sel -T -t -m "//properties/settings//*"  -v "name()" -o " " -v "@value"  -nl yourfile.xml

and see if it works.

  • Related