如何从Shell执行XPath单行? [英] How to execute XPath one-liners from shell?

查看:153
本文介绍了如何从Shell执行XPath单行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有针对Ubuntu和/或CentOS的软件包,该软件包具有可以执行XPath单一代码的命令行工具,如 foo // element @ attribute filename.xml foo // element @ attribute< filename.xml 并逐行返回结果?



我正在寻找可以让我 apt-get install foo yum install foo ,即可直接使用,无需包装或其他修改。 / p>

以下是一些接近的例子:



Nokogiri。如果我编写此包装器,则可以按上述方式调用包装器:

 #!/ usr / bin / ruby​​ 

需要'nokogiri'

Nokogiri :: XML(STDIN).xpath(ARGV [0])。each | row |
放置行
结束

XML :: XPath。可以与此包装一起使用:

 #!/ usr / bin / perl 

use strict;
使用警告;
使用XML :: XPath;

my $ root = XML :: XPath-> new(ioref =>'STDIN');我的$ node的
($ root-> find($ ARGV [0])-> get_nodelist){
print($ node-> getData, \n);
}

xpath :: XPath返回太多噪音,-NODE- attribute = value

来自XML :: Twig的

xml_grep 无法处理不返回元素的表达式,因此不能用于未经进一步处理而提取属性值。



编辑:



echo cat // element / @ attribute | xmllint --shell filename.xml 返回类似于 xpath 的噪声。



xmllint --xpath // element / @ attribute filename.xml 返回 attribute = value



xmllint --xpath'string(// element / @ attribute)'filename.xml 返回我想要的内容,但仅针对第一个



对于另一个几乎可以解决该问题的解决方案,这是一个XSLT,可用于评估任意XPath表达式(需要在XSLT处理器中使用dyn:evaluate支持):

 <?xml version = 1.0?> 
< xsl:stylesheet xmlns:xsl = http://www.w3.org/1999/XSL/Transform version = 1.0
xmlns:dyn = http:// exslt。 org / dynamic extension-element-prefixes = dyn>
< xsl:output omit-xml-declaration = yes indent = no method = text />
< xsl:template match = />
< xsl:for-each select = dyn:evaluate($ pattern)>
< xsl:value-of select = dyn:evaluate($ value) />
< xsl:value-of select =’&#10;’ />
< / xsl:for-each>
< / xsl:template>
< / xsl:stylesheet>

运行 xsltproc --stringparam pattern // element / @ attribute- stringparam值。任意-xpath.xslt filename.xml

解决方案

您应该尝试以下工具:



  • xmlstarlet :可以编辑,选择,转换...默认未安装,xpath1

  • xmllint :通常默认情况下与 libxml2-utils ,xpath1一起安装(检查我的包装,使-xpath 可以在非常老的发行版上启用和换行符分隔输出(v <2.9.9)

  • xpath :通过perl的模块 XML安装: :XPath ,xpath1

  • xml_grep :通过perl的模块 XML ::安装嫩枝,xpath1(有限的xpath使用)

  • xidel :xpath3

  • saxon-lint :我自己的项目,@Michael Kay的Saxon-HE Java库的包装,xpath3


xmllint 随附 libxml2-utils (可以用作interacti带有-shell 开关的ve shell)


xmlstarlet xmlstarlet


xpath 随附perl的模块 XML :: Xpath


xml_grep 随perl的模块 一起提供XML :: Twig


xidel xidel


saxon-lint 使用 SaxonHE 9.6 XPath 3.x (+复古兼容性)


Ex:

  xmllint --xpath'// element / @ attribute 'file.xml 
xmlstarlet sel -t -v // element / @ attribute; file.xml
xpath -q -e'// element / @ attribute'file.xml
xidel -se'// element / @ attribute'file.xml
saxon-lint- xpath'// element / @ attribute'file.xml




Is there a package out there, for Ubuntu and/or CentOS, that has a command-line tool that can execute an XPath one-liner like foo //element@attribute filename.xml or foo //element@attribute < filename.xml and return the results line by line?

I'm looking for something that would allow me to just apt-get install foo or yum install foo and then just works out-of-the-box, no wrappers or other adaptation necessary.

Here are some examples of things that come close:

Nokogiri. If I write this wrapper I could call the wrapper in the way described above:

#!/usr/bin/ruby

require 'nokogiri'

Nokogiri::XML(STDIN).xpath(ARGV[0]).each do |row|
  puts row
end

XML::XPath. Would work with this wrapper:

#!/usr/bin/perl

use strict;
use warnings;
use XML::XPath;

my $root = XML::XPath->new(ioref => 'STDIN');
for my $node ($root->find($ARGV[0])->get_nodelist) {
  print($node->getData, "\n");
}

xpath from XML::XPath returns too much noise, -- NODE -- and attribute = "value".

xml_grep from XML::Twig cannot handle expressions that do not return elements, so cannot be used to extract attribute values without further processing.

EDIT:

echo cat //element/@attribute | xmllint --shell filename.xml returns noise similar to xpath.

xmllint --xpath //element/@attribute filename.xml returns attribute = "value".

xmllint --xpath 'string(//element/@attribute)' filename.xml returns what I want, but only for the first match.

For another solution almost satisfying the question, here is an XSLT that can be used to evaluate arbitrary XPath expressions (requires dyn:evaluate support in the XSLT processor):

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:dyn="http://exslt.org/dynamic" extension-element-prefixes="dyn">
  <xsl:output omit-xml-declaration="yes" indent="no" method="text"/>
  <xsl:template match="/">
    <xsl:for-each select="dyn:evaluate($pattern)">
      <xsl:value-of select="dyn:evaluate($value)"/>
      <xsl:value-of select="'&#10;'"/>
    </xsl:for-each> 
  </xsl:template>
</xsl:stylesheet>

Run with xsltproc --stringparam pattern //element/@attribute --stringparam value . arbitrary-xpath.xslt filename.xml.

解决方案

You should try these tools :

  • xmlstarlet : can edit, select, transform... Not installed by default, xpath1
  • xmllint : often installed by default with libxml2-utils, xpath1 (check my wrapper to have --xpath switch on very old releases and newlines delimited output (v < 2.9.9)
  • xpath : installed via perl's module XML::XPath, xpath1
  • xml_grep : installed via perl's module XML::Twig, xpath1 (limited xpath usage)
  • xidel: xpath3
  • saxon-lint : my own project, wrapper over @Michael Kay's Saxon-HE Java library, xpath3

xmllint comes with libxml2-utils (can be used as interactive shell with the --shell switch)

xmlstarlet is xmlstarlet.

xpath comes with perl's module XML::Xpath

xml_grep comes with perl's module XML::Twig

xidel is xidel

saxon-lint using SaxonHE 9.6 ,XPath 3.x (+retro compatibility)

Ex :

xmllint --xpath '//element/@attribute' file.xml
xmlstarlet sel -t -v "//element/@attribute" file.xml
xpath -q -e '//element/@attribute' file.xml
xidel -se '//element/@attribute' file.xml
saxon-lint --xpath '//element/@attribute' file.xml

.

这篇关于如何从Shell执行XPath单行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆