- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
使用 nokogiri,只需调用 node.path
即可轻松获得从任何节点返回到根的绝对路径。举个例子:
<bookstore>
<department category="COOKING">
<book>
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book>
<title lang="en">Nice meals</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</department>
<department category="WEB">
<book>
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</department>
</bookstore>
如果我这样做
tree.search("//title[text() = 'Learning XML']").first.path
我会得到类似bookstore/department[2]/book[1]/title[1]
现在,如果我想获取此节点的路径,而不是从根节点获取,我想从例如//department[@category='WEB']
一直到同一个标题节点?
换句话说。我一般如何获取/生成两个已知节点之间的路径,例如 //department[@category='WEB']
到 bookstore/department[2]/book[1]/title[ 1]
?
编辑
我一直在考虑将 //department[@category='WEB']
变成一种新的“根”的方法,例如通过删除一些东西,然后再次在标题节点上使用 .path
方法。这似乎不是很“简单”...
最佳答案
我对最后基于字符串的 hack 并不感兴趣,但现在这会产生干净的 XPaths:
require 'nokogiri'
class Nokogiri::XML::Node
def path_to( node )
self_ancestors = [self].concat(self.ancestors)
shared = (self_ancestors & [node].concat(node.ancestors)).first
[ "../"*self_ancestors.index(shared),
".", node.path[shared.path.length..-1] ]
.join
.sub( %r{\A\./|/\.(?=/|\z)}, '' ) # remove superfluous "."
end
end
doc = Nokogiri.XML(IO.read('tmp.rxml'))
n1 = doc.at("//department[@category='WEB']")
n2 = doc.at("//title[.='Learning XML']")
n3 = doc.at("//year[.='2003']")
n4 = doc.at("//@lang")
p n1.path_to(n2) #=> "book/title"
p n2.path_to(n3) #=> "../year"
p n3.path_to(n2) #=> "../title"
p n2.path_to(n1) #=> "../.."
p n1.path_to(n1) #=> "."
p n4.path_to(n2) #=> "../../../../department[2]/book/title"
p n2.path_to(n4) #=> "../../../department[1]/book[1]/title/@lang"
p n2.at( n2.path_to(n4) )==n4 #=> true
关于ruby - 在两个节点之间获取 xpath 的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21768487/
我制作手机游戏,但我想为我的社交游戏制作一些简单的基于浏览器的客户端,以便我们可以更轻松地调试。最简单的引擎是什么? 我研究了一些 HTML5 引擎和 GWT,但我想听听社区的意见。我正在寻找一种能够
有一个有趣的例子,有人设法为一个只占用 1kb 的论坛创建了一个 PHP 脚本:http://www.nerdparadise.com/blogs/blake/6034/ 我想知道是否有类似的小脚本可
首先我要吐槽一下,看程序的过程中遇见了yield这个关键字,然后百度的时候,发现没有一个能简单的让我懂的,讲起来真tm的都是头头是道,什么参数,什么传递的,还口口声声说自己的教程是最简单的,最浅显易
我的proguard配置太糟糕了,我的游戏崩溃了,而且似乎不正常。在我弄清楚到底哪里出了问题之前,最简单,最安全的配置用于全部安装是什么?有没有办法使它仅更改变量名?或者只是混淆代码以使其更难阅读而不
我正在重构一些 C# 代码,其中一部分是重做一些引用,因为我们正在完全重做文件夹结构。我想做的就是进入 .csproj 或 .sln 文件并修改路径。 然而,一些引用文献有类似的路径 "../../.
免责声明:这是一个理论问题,目的是增加我的理解。我知道我总是可以使用像 JSON 库这样的工具来解决问题。 假设我想创建一个逗号分隔的值列表,这些值本身可能包含逗号。这些逗号需要先转义。假设我使用 .
我对编程完全陌生,我选择 Delphi 作为我想学习的编程语言。 我基本上想构建使用套接字填写和提交 Web 表单的工具,并且我希望它们也是多线程的。 我希望它们功能丰富且性能正确。 我并不急于这样做
我正在构建一个协作创作工具,该工具允许用户共同编辑信息空间,该信息空间是节点和链接的可视化。一个客户端应用程序中所做的更改需要反映到所有其他客户端中。由于它是可视化的,因此可能需要经常更新潜在的大数据
作为一家专门开发自定义 CMS 的公司,我们被要求在下一个项目中部署开源 CMS。 我们可以自由选择系统。对于熟悉 PHP5 中的 MVC 模型和 OOP 的团队,您会推荐什么? 有人告诉我Drupa
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我完全是 Ember.js 菜鸟,需要了解后端注意事项,目前似乎很少有教程涵盖。对于快速原型(prototype)设计,最简单/最简单的后端设置是什么?我看到了一些 ember-rails 教程,但是
我正在寻找这种最简单、最简单的方法来启动 Java Web 服务。我曾经使用 Axis-1-on-Tomcat,但是对于 Axis 2,它变得太厚了。我正在寻找的一些偏好: 低内存占用 - 一个包含最
我多年来一直使用旧版 openGL 和 cocoa,但现在我正在努力过渡到 openGL 3.2。互联网上有几个例子,但它们都太复杂了(许多甚至在 XCode 5.1 下不再编译)。有人可以编写一个最
我正在构建一个简单的应用程序,它应该将开关/支票簿的状态存储 7 天。我遇到的唯一问题是我用来构建所述应用程序的网站不适合手机上的本地存储。没有通过本地存储或链接到在线数据库来存储开关/支票簿状态的良
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 7 个月前。 Improv
有时我需要 1 个用户类型元素的集合(或任何其他容器)并以这种方式创建它们: boost::assign::list_of(typeVariable).convert_to_container >()
我的页面上有一个 DropDownList 和一个 TextBox。当用户在 DropDownList 中选择“其他”选项时,我想在其右侧显示一个文本框。我不想使用传统的回发技术。我希望这种交互是在客
说到编码,我还很年轻,而且我听说过很多关于组织的事情。有些使用部分,有些使用 div,有些使用 div 作为按钮,其他使用 css 中的输入来更改它。作为一个喜欢让他的代码干净、简单易懂但又正确的人,
我需要将带有变量项的 ContentValues 转换为 JSON 字符串,我可以将其保存到数据库中,并在以后用作 HTTP 请求的正文。网络上的所有内容都只会以相反的方向进行转换。 最佳答案 每当您
我的电脑上安装了 Python 3.7。想用tensorflow,发现基本不支持3.7,所以想(也)安装Python 3.6。 关于如何做到这一点有什么建议吗?我是否必须卸载 3.7 并将其替换为 3
我是一名优秀的程序员,十分优秀!