如何从 shell 执行 XPath one-liners? [英] How to execute XPath one-liners from shell?
问题描述
是否有适用于 Ubuntu 和/或 CentOS 的软件包,其具有可以执行 XPath 单行程序的命令行工具,例如 foo//element@attribute filename.xml
或foo//element@attribute <filename.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?
我正在寻找可以让我apt-get install foo
或 yum install foo
然后开箱即用的东西,无需包装器或其他改编.
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.如果我编写这个包装器,我可以按照上述方式调用包装器:
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.可以使用这个包装器:
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, "
");
}
xpath
来自 XML::XPath 返回太多噪音,-- NODE --
和 attribute = "value"
.
xpath
from XML::XPath returns too much noise, -- NODE --
and attribute = "value"
.
xml_grep
无法处理不返回元素的表达式,因此无法在不进一步处理的情况下用于提取属性值.
xml_grep
from XML::Twig cannot handle expressions that do not return elements, so cannot be used to extract attribute values without further processing.
echo cat//element/@attribute |xmllint --shell filename.xml
返回类似于 xpath
的噪音.
echo cat //element/@attribute | xmllint --shell filename.xml
returns noise similar to xpath
.
xmllint --xpath//element/@attribute filename.xml
返回attribute = "value"
.
xmllint --xpath 'string(//element/@attribute)' filename.xml
返回我想要的,但只针对第一场比赛.
xmllint --xpath 'string(//element/@attribute)' filename.xml
returns what I want, but only for the first match.
对于另一个几乎满足该问题的解决方案,这里有一个 XSLT,可用于计算任意 XPath 表达式(需要 XSLT 处理器中的 dyn:evaluate 支持):
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="' '"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
使用 xsltproc --stringparam pattern//element/@attribute --stringparam value 运行.任意-xpath.xslt 文件名.xml
.
推荐答案
你应该试试这些工具:
xmlstarlet
: 可以编辑、选择、转换...默认没有安装,xpath1xmllint
:通常默认安装libxml2-utils
、xpath1(检查我的 wrapper 让--xpath
在非常旧的版本和换行符分隔的输出上切换 (v <2.9.9)xpath
: 通过 perl 的模块XML::XPath
, xpath1 安装xml_grep
:通过 perl 的模块XML::Twig
、xpath1(有限的 xpath 使用)安装xidel
: xpath3saxon-lint
:我自己的项目,封装了@Michael Kay 的 Saxon-HE Java 库 xpath3
xmlstarlet
: can edit, select, transform... Not installed by default, xpath1xmllint
: often installed by default withlibxml2-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 moduleXML::XPath
, xpath1xml_grep
: installed via perl's moduleXML::Twig
, xpath1 (limited xpath usage)xidel
: xpath3saxon-lint
: my own project, wrapper over @Michael Kay's Saxon-HE Java library, xpath3
xmllint
带有 libxml2-utils
(可用作带有 --shell
开关的交互式 shell)
xmllint
comes with libxml2-utils
(can be used as interactive shell with the --shell
switch)
xmlstarlet
是 xmlstarlet
.
xpath
带有 perl 的模块 XML::Xpath代码>
xpath
comes with perl's module XML::Xpath
xml_grep
带有 perl 的模块 XML::Twig代码>
xml_grep
comes with perl's module XML::Twig
xidel
是 xidel
saxon-lint
使用 SaxonHE 9.6 ,XPath 3.x(+复古兼容性)
saxon-lint
using SaxonHE 9.6 ,XPath 3.x (+retro compatibility)
例如:
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
- xmlstarlet 页面
- 男人 xmllint
- xpath 页面
- xml_grep
- xidel
- saxon-lint
.
这篇关于如何从 shell 执行 XPath one-liners?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!