user2379020
user2379020

Reputation: 322

shell command to parse xml is not behaving properly

I want to parse below xml

<?xml version="1.0" encoding="UTF-8"?>
  <widget xmlns="http://www.w3.org/ns/widgets" xmlns:tzn="http://tzn.org/ns/widgets" id="http://yourdomain/TznTtApp" version="1.0.0" viewmodes="maximized">
  <acs orgn="*" sbdomains="true"/>
  <tzn:application id="ca9i.TznTtApp" package="ca9i" required_version="1.0"/>
  <apptype>14</apptype>
  <content src="index.html"/>
  <feature name="http://tzn.org/fate/sn.se.al"/>
  <icon src="icon.png"/>
  <name>TznTtApp</name>
</widget>

so i am using below shell script

echo -e 'cat //*[local-name()="apptype"]/text()' | xmllint --shell  /data/2211334455/894949890051_1.0.4/config.xml | grep -v "^/ >"

But i am getting the output like

-------
14

And expected output is

14

so can anyone tell me what mistake am i doing ?

Upvotes: 2

Views: 48

Answers (2)

Tom Fenech
Tom Fenech

Reputation: 74685

It looks like this is just part of the output when you use the --shell option. One alternative would be to just use --xpath:

$ xmllint --xpath '//*[local-name()="apptype"]/text()' config.xml
14

The --shell option launches an interactive shell, which you're currently passing commands to using echo. --xpath is used to return the results that match an XPath. It looks like this is exactly what you want in this case.

If there's a good reason that you're using --shell, I guess you could add to your grep filter:

$ echo 'cat //*[local-name()="apptype"]/text()' | xmllint.exe --shell config.xml | grep -v '^\(/ >\| -\)'
14

This filters out any lines starting with / > (as in your attempt) or -, so the other undesired line is removed too.

Upvotes: 3

Kent
Kent

Reputation: 195199

xmllint has an option --xpath to evaluate xpath expression.

For your example, you can just do:

 xmllint --xpath '//*[local-name()="apptype"]/text()' your.xml

to get only 14

Upvotes: 1

Related Questions