具有层次结构中可选元素的XPath [英] XPath with optional element in hierarchy

查看:106
本文介绍了具有层次结构中可选元素的XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如堆栈溢出答案想象你需要选择一个特定的表格,然后选择它的所有行。由于HTML的宽容性,以下三种都是合法标记:

 < table id =foo> < TR> ...< / TR>< /表> 
< table id =foo>< tbody>< tr> ...< / tr>< / tbody>< / table>
< table id =foo>< tr> ...< / tr>< tbody>< tr> ...< / tr>< / tbody>< /表>

您担心嵌套在表中的表,因此不想使用像< br>
表格[@ id =foo] // tr



将你想要的XPath指定为正则表达式,它可能看起来像这样:

table [@ id =foo](/ tbody)?/ tr



一般来说,如何指定允许选择器层次结构中可选元素的XPath表达式?



<为了说清楚,我并没有试图解决现实世界的问题,或者选择特定文档的特定元素。我正在寻求解决一类问题的技巧。

我不明白你为什么可以这样做。 't use this:

  // table [@ id ='foo'] / tr | // table [@ id = 'foo'] / tbody / tr 

如果您想要一个没有节点集合的表达式:

  // tr [(。| parent :: tbody)[1] / parent :: table [@ id ='foo']] 


As in this Stack Overflow answer imagine that you need to select a particular table and then all the rows of it. Due to the permissiveness of HTML, all three of the following are legal markup:

<table id="foo"><tr>...</tr></table>
<table id="foo"><tbody><tr>...</tr></tbody></table>
<table id="foo"><tr>...</tr><tbody><tr>...</tr></tbody></table>

You are worried about tables nested in tables, and so don't want to use an XPath like
table[@id="foo"]//tr.

If you could specify your desired XPath as a regex, it might look something like:
table[@id="foo"](/tbody)?/tr

In general, how can you specify an XPath expression that allows an optional element in the hierarchy of a selector?

To be clear, I'm not trying to solve a real-world problem or select a specific element of a specific document. I'm asking for techniques to solve a class of problems.

解决方案

I don't see why you can't use this:

//table[@id='foo']/tr|//table[@id='foo']/tbody/tr

If you want one expression without node set union:

//tr[(.|parent::tbody)[1]/parent::table[@id='foo']]

这篇关于具有层次结构中可选元素的XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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