bash 使用 xmllint 命令行的模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18390288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:22:30  来源:igfitidea点击:

pattern using xmllint command-line

xmlbashxmllint

提问by iamauser

]$ xmllint --version
xmllint: using libxml version 20626

My xml file looks something like this:

我的 xml 文件看起来像这样:

<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 StringB2given the condition name==ARCH2and state==StringC2. Is it possible using xmllintcommand line options, if yes, how ? Some examples will be beneficial.

例如,我想获得StringB2给定条件name==ARCH2和的值state==StringC2。是否可以使用xmllint命令行选项,如果是,如何?一些例子将是有益的。

I can extract these using sedor awkbut that may not be a good solution.

我可以使用sedor提取这些,awk但这可能不是一个好的解决方案。

回答by hek2mgl

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.

在测试此语句时,我意识到 xmllint (20708) 的 ubuntu (12.04) 版本在执行此命令时以段错误终止。我从https://git.gnome.org/browse/libxml2/refs/克隆了最新版本并编译。现在上面的命令有效。

回答by Mark O'Connor

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