iamauser
iamauser

Reputation: 11489

pattern using xmllint command-line

]$ 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.

Upvotes: 3

Views: 11217

Answers (2)

hek2mgl
hek2mgl

Reputation: 158220

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.

Upvotes: 4

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 78011

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 

Upvotes: 4

Related Questions