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

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

问题描述

是否有适用于 Ubuntu 和/或 CentOS 的软件包,其具有可以执行 XPath 单行程序的命令行工具,例如 foo//element@attribute filename.xmlfoo//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 fooyum 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="'&#10;'"/>
    </xsl:for-each> 
  </xsl:template>
</xsl:stylesheet>

使用 xsltproc --stringparam pattern//element/@attribute --stringparam value 运行.任意-xpath.xslt 文件名.xml.

推荐答案

你应该试试这些工具:

  • xmlstarlet : 可以编辑、选择、转换...默认没有安装,xpath1
  • xmllint :通常默认安装 libxml2-utils、xpath1(检查我的 wrapper--xpath 在非常旧的版本和换行符分隔的输出上切换 (v <2.9.9)
  • xpath : 通过 perl 的模块 XML::XPath, xpath1
  • 安装
  • xml_grep :通过 perl 的模块 XML::Twig、xpath1(有限的 xpath 使用)安装
  • xidel: xpath3
  • saxon-lint :我自己的项目,封装了@Michael Kay 的 Saxon-HE Java 库 xpath3
  • 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 带有 libxml2-utils(可用作带有 --shell 开关的交互式 shell)

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

xmlstarletxmlstarlet.

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

xidelxidel

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

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