Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use the --pattern option of xmllint?

Tags:

xpath

libxml2

I'm trying to see how libxml implements XPath support, so it made sense to me to test using xmllint. However, the obvious option, --pattern, is somewhat obscure, and I ended up using something like the following:

test.xml: <foo><bar/><bar/></foo>

> xmllint --shell test.xml
/  > dir /foo
ELEMENT foo
/  > dir /foo/*
ELEMENT bar
ELEMENT bar

This seems to work, and that's great, but I'm still curious. What is xmllint's --pattern option for, and how does it work?

Provide an example for full credit. =)

like image 679
Matthew Lowe Avatar asked Feb 08 '10 20:02

Matthew Lowe


2 Answers

The seemingly undocumented option --xpath seems to be more useful.

% cat data.xml
<project>
  <name>
    bob
  </name>
  <version>
    1.1.1
  </version>
</project>
% xmllint --xpath '/project/version/text()' data.xml | xargs -i echo -n "{}"
1.1.1
% xmllint --xpath '/project/name/text()' data.xml | xargs -i echo -n "{}"
bob
like image 66
l0st3d Avatar answered Oct 21 '22 20:10

l0st3d


The hint is in the words "which can be used with the reader interface to the parser": xmllint only uses the reader interface when passed the --stream option:

$ xmllint --stream --pattern /foo/bar test.xml
Node /foo/bar[1] matches pattern /foo/bar
Node /foo/bar matches pattern /foo/bar
like image 36
npostavs Avatar answered Oct 21 '22 19:10

npostavs