Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pattern using xmllint command-line

Tags:

bash

xml

xmllint

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

like image 942
iamauser Avatar asked Jul 23 '26 14:07

iamauser


1 Answers

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 
like image 52
Mark O'Connor Avatar answered Jul 26 '26 09:07

Mark O'Connor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!