XPath:生成从根节点到指定节点的相对表达式?
如何生成所需的 XPath 表达式以从给定的根节点遍历 xml 结构到指定的节点?
我将在运行时收到表格的 HTML 片段。我必须根据某些条件找到所需的节点,并形成从表根节点到该节点的 XPath 字符串并将其返回。
HTML 表格结构事先是未知的。 Java中是否有任何API可以返回给定根节点和子节点的XPath字符串?
How can I generate the required XPath expression to traverse from a given root node to a specified node down the xml structure?
I will receive HTML fragment of a table at runtime. I have to find the desired node based on some criteria and the form an XPath string from the table root node to that node and return that.
The HTML table structure is not known beforehand. Is there any API in Java that returns the XPath string given the root node and the child node?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议在 Groovy 中执行此操作,它提供 GPATH (本质上是 groovy 语言的 xpath 实现。)Groovy 语法非常简洁且强大,如我的 blog 并与 Java 语言无缝混合(groovy 被编译为 java 类文件)。
至于你想要实现的目标......以下应该遍历整个 HTML DOM 结构并搜索具有特定 id 属性(例如 unique_id_for_tag)的“标签”(例如 div),每个条目都由闭包处理。
I would recommend doing this in Groovy which provides GPATH (essentially an xpath implementation for the groovy language.) The Groovy syntax is very succint and powerful as described in my blog and mixes seamlessly with the he Java language (groovy is compiled down to java class files).
As for what you are trying to achieve...the following should traverse the entire HTML DOM structure and search for a "tag" (e.g. div) with a specific id attribute (e.g. unique_id_for_tag) with each entry found to be processed by the closure.
下面是(我知道的)实现此目的的一种方法
Below is one way (that I know) to achieve this
这不能(仅)在纯 XPath 1.0 中完成。
XPath 2.0 解决方案:
当根据以下 XML 文档计算此 XPath 2.0 表达式时:
如果两个参数定义为:
那么上面的 XPath 2.0 表达式的计算结果为:
This cannot be done (only) in pure XPath 1.0.
XPath 2.0 solution:
When this XPath 2.0 expression is evaluated against the following XML document:
and if the two parameters are defined as:
then the result of the evaluation of the XPath 2.0 expression above is:
如果您知道要选择的根元素和子元素的名称,并且只有一个具有该名称的子元素,则可以简单地使用“/root//child”。但也许我误解了你想要实现的目标。你能举个例子吗?
If you know the names of the root element and the child element you are trying to select, and if there is only one child element with that name, you could use simply "/root//child". But maybe I misunderstood what you were trying to achieve. Could you give an example ?