pattern using xmllint command-line

11k views Asked by At
]$ xmllint --version
xmllint: using libxml version 20626

My xml file looks something like this:

<projects>
 <architecture name="ARCH1">
  <project label="StringA1" type="StringB1" state="StringC1"/>
   ......
 </architecture>
 <architecture name="ARCH2">
  <project label="StringA2" type="StringB2" state="StringC2"/>
  ......
 </architecture>
</projects>

For example, I would like to obtain the value StringB2 given the condition name==ARCH2 and state==StringC2. Is it possible using xmllint command line options, if yes, how ? Some examples will be beneficial.

I can extract these using sed or awk but that may not be a good solution.

2

There are 2 answers

4
hek2mgl On

Use:

xmllint --xpath '//architecture[@name="ARCH2"]/project/@type'

or if you just want the string and only the string:

xmllint --xpath 'string(//architecture[@name="ARCH2"]/project/@type)'

While testing this statement I realized, that the ubuntu (12.04) version of xmllint (20708) terminates with a segfault when executing this command. I cloned the most recent version from https://git.gnome.org/browse/libxml2/refs/ and compiled. Now the command above works.

0
Mark O'Connor On

Try the following

xmllint --xpath 'string(///project[../@name="ARCH1" and @state="StringC1"]/@type)' data.xml

Version:

$ xmllint --version
xmllint: using libxml version 20900
   compiled with: Threads Tree Output Push Reader Patterns Writer SAXv1 FTP HTTP DTDValid HTML Legacy C14N Catalog XPath XPointer XInclude Iconv ISO8859X Unicode Regexps Automata Expr Schemas Schematron Modules Debug Zlib Lzma