How to suppress over-verbose xpath-output?

Viewed 2202

I'm using xpath (as supplied in Mac OS X 10.9 usr/bin/xpath5.16) in a shell script to parse some values from an XML file which works really well. However it gives me some verbose output which I don't want to see in my script. I actually only want to store the result (the content of the attribute) in a variable.

content=$(xpath ../../AndroidManifest.xml /manifest/@android:versionCode)
echo "$content"

After execution the variable content indeed contains the content of the attribute, however there is also some verbose output I want to get rid of. Here it is:

Found 1 nodes:
-- NODE --

 android:versionCode="38"

Note: the "38" at the end of the output originates from the echo "$content" line the rest is the output of xpath.

4 Answers

-q did not work for me. Since xpath utulity prints extra output on std-err, just redirect it to /dev/null

xpath file.xml  '/foo/bar/text()' 2>/dev/null

Sorry Folks to stretch your enthusiasm but look at this output instance of Open Directory Keywords attribute, it's made of text strings which each one is a node in dscl plist output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>dsAttrTypeStandard:Keywords</key>
        <array>
                <string>nelve0jfO</string>
                <string>qPknu6</string>
                <string>cic3-bubcd</string>
        </array>
</dict>
</plist>


 /usr/bin/xpath "//string[.]/text()"

Output (console print is 4 lines):

Found 3 nodes: -- NODE --

nelve0jfO-- NODE --

qPknu6-- NODE --

cic3-bubcd

/usr/bin/xpath "//string[.]/text()" 2>/dev/null

Output:

nelve0jfOqPknu6cic3-bubcd

Whithout linefeed...

Who knows if its possible to append field delimiter in the query syntax?

Related