23

I've seen this syntax several places (eg, here):

xmllint --xpath '/plist/array/string/text()' tmp.xml, used to query a particular XML node using an xpath selector. However, I'm getting the error Unknown option --xpath when I try to execute this on my machine (mac os x snow leopard).

Looking more closely at the man page for xmllint, I don't see the --xpath option documented...

Am I just totally missing something here?

Neal Pisenti
  • 526
  • 1
  • 3
  • 7
  • 2
    I also can't find `--xpath` option (on my Linux box) but in my case `xmllint` knows about this option: `xmllint | grep xpath` return `--xpath expr: evaluate the XPath expression, inply --noout`. Show to us output of `xmllint --version` command. Maybe this program should be compiled with/or without XPath support?.. – Slava Semushin Aug 15 '12 at 19:36
  • See @steve.sims answer to [this question](http://stackoverflow.com/questions/11611385/get-value-from-an-attribute-using-xmllint) for using older versions of xmllint that do not support the --xpath argument. – mmigdol Jul 17 '14 at 00:51

4 Answers4

8
xmllint --shell tmp.xml <<<'xpath /plist/array/string/text()'

If you need to make xmllint to read stdin:

cat /tmp/tmp.xml | xmllint --shell <(cat) <<<'xpath /plist/array/string/text()'
Alexey
  • 81
  • 1
  • 4
  • Alexey, I used the exact same command and I am getting - 'Missing name for redirect.'. Can you please help – Dileep Jun 06 '18 at 11:06
  • I do not know exactly but sometimes or on the different shell version construction <(cat) did not work. Try to use file instead or - as file. – Alexey Jun 07 '18 at 18:36
6

For some people updating is not an option. You have to work with the given version, that is installed by some other team and you go with it.

You can try through --shell:

xmllint --shell tmp.xml << EOF
'/plist/array/string/text()'
EOF
trompa
  • 1,967
  • 1
  • 18
  • 26
  • 2
    Thankyou @Trompa, this demonstrated that it is possible with an old xmllint; I used the xmllint --shell command 'cat' to extract a tomcat HTTP listener port in 1-line thus: `echo "cat //Connector[@protocol='org.apache.coyote.http11.Http11NioProtocol']/@port" | xmllint --shell /apache-tomcat/conf/server.xml | awk -F\" '/=/ { print $2; }'` – Ed Randall Oct 08 '15 at 16:39
5

Ah, yep, must've been an issue with an outdated version of libxml2.

Updating libxml2 (to v2.7.8) via macports seems to have fixed the problem.

Neal Pisenti
  • 526
  • 1
  • 3
  • 7
1

If you want to manipulate plist files from the command line on a Mac, use PlistBuddy.

For example, you can do this kind of thing in a shell script:

BUNDLE_ID=`/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' Info.plist`
echo $BUNDLE_ID

It's very powerful, you can add/delete items in arrays and dictionaries, look up keyed or indexed values, copy/merge/import other files, etc. See /usr/libexec/PlistBuddy -h for full info.

jhabbott
  • 18,461
  • 9
  • 58
  • 95