0

In a shell script (Linux, bash, #!/bin/sh) I have a variable containing XML data like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<interface>
    <objects retrieved="0" total="0"/>
</interface>

Some error handling is already done when getting this data, so focus on the actual question please. And the question is: easy/effective way of reading the "objects" element's attribute data (named "retrieved"). It can be 0 or 1. (and nothing else). I guess I have two options: regular expressions and XML-parsing with some external program. But which one should I choose? Thanks in advance.

gyorgyabraham
  • 2,550
  • 1
  • 28
  • 46

2 Answers2

1

You can use xmlstarlet to get the attr:

$ xmlstarlet sel -t -m //objects -v @retrieved input.xml
0

Or

$ xmlstarlet sel -t -m //objects/@retrieved -v . input.xml

-m or --match <xpath>     - match XPATH expression
-v or --value-of <xpath>  - print value of XPATH expression
kev
  • 155,172
  • 47
  • 273
  • 272
1

Both are good choices, If you want strict choice that your script should be independent of external programs, I think Using regular expressions is better. Using regular expression may make the program function more faster than using an external program.

But using regular expression is not always a feasible option, especially when you have less time, and the regular expression you are making is complex. External program is an option then only. If you plan to move this script to another computer, this external program may cause dependecy or an overhead.

I will suggest Regular expressions. These may help you. Take a look.

How to parse XML using shellscript?

  • This is really helpful

And this have some idea in the content.

http://silveiraneto.net/2010/05/13/substitution-on-a-xml-file-shell-script-snippet/

Community
  • 1
  • 1
Xander
  • 1,652
  • 2
  • 15
  • 20