如何使normalize-space()xpath函数起作用? [英] how to get the normalize-space() xpath function to work?
问题描述
我目前正在尝试以下xpath
I am currently trying the following xpath
//tr[normalize-space(td/text())='User Name']
以获得包含td且包含的所有tr。 '用户名'
或'用户名'
或'用户名'
工作,我不知道查询出了什么问题:(
我要查找的数据采用以下格式
to get all the tr that contains a td that contain 'User Name'
or 'User Name'
or ' User Name '
but its not working and I don't know what is wrong with the query :(
the data i want to find is in the following format
<tr><td>User Name</td></tr>
<tr><td>User Name</td></tr>
<tr><td> User Name </td></tr>
那么编写此xpath查询的正确格式是什么?
So what is the right format to write this xpath query?
编辑:
如果数据采用以下格式,则似乎不起作用
it seem not work if the data is in the following format
<tr><td>x</td><td>User Name</td></tr>
<tr><td>x</td><td>y</td><td>User Name</td></tr>
<tr><td>x</td><td>y</td><td>z</td><td> User Name </td></tr>
所以现在我该如何编写xpath查询?
注意: / / tr [normalize-space(td / text())='用户名']将不起作用
但 // tr / td [normalize-space(text())='用户名' ](但我想获取tr而不是td元素)
so now how can i write the xpath query?
note: "//tr[normalize-space(td/text()) = 'User Name']" wont work
but "//tr/td[normalize-space(text()) = 'User Name']" will work (but i want to get the tr and not the td element)
推荐答案
现在,您已经编辑了问题,这是有道理的。让我们考虑一下此输入:
Now that you've edited the question, it makes sense. Let's consider this input:
<tr><td>x</td><td>User Name</td></tr>
和非工作查询:
//tr[normalize-space(td/text()) = 'User Name']
现在, td / text()
的意思是选择所有子 td $ c的所有子文本节点$ c>当前节点的节点。在这种情况下,这将产生一个包含两个文本节点的节点集,分别为
x
和 User Name
。
Now, td/text()
means "select all child text nodes of all child td
nodes of current node". In this case this will yield a node-set consisting two text nodes, x
and User Name
.
现在您在该节点集上调用 normalize-space()
。 normalize-space()
的唯一参数的类型为 string?
。由于节点集不是字符串,因此按照第3.2节进行转换 XPath 1.0建议的a>:
Now you call normalize-space()
on that node-set. The type of the sole argument of normalize-space()
is string?
. Since a node-set is not a string, conversions kick in, per section 3.2 of XPath 1.0 recommendation:
将参数转换为string类型,就像通过调用string()函数一样。
An argument is converted to type string as if by calling the string() function.
现在让我们看一下定义:
节点集被转换为通过返回节点集中按文档顺序排在首位的节点的字符串值来获得字符串。如果节点集为空,则返回一个空字符串。
A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.
在我们的示例中,按文档顺序的第一个节点为文本节点 x
,因此它将被使用;第二个节点将被忽略。因此,您最终调用了 normalize-space(’x’)
。自然,这不会等于用户名。为此,请使用:
In our example, the first node "in document order" is the text node x
, so it is the one which will be used; the second node is ignored. Thus, you end up calling normalize-space('x')
. Naturally, this won't compare equal to 'User Name'. To make this work, use:
//tr[td[normalize-space(text()) = 'User Name']]
这可以转录为选择所有 tr
个节点,其子节点 td
个节点,第一个子节点 text()
节点具有规范化的字符串值用户名
-这就是您想要的。此外,您可以将其简化为:
This can be transcribed as "select all tr
nodes that have child td
nodes, the first child text()
node of which has a normalized string value of User Name
" - which is what you want. Furthermore, you can simplify this to:
//tr[td[normalize-space() = 'User Name']]
由于无参数 normalize-space()
将应用于当前节点(将为 td
),并处理其中的所有文本节点。
Since a no-argument normalize-space()
will apply to the current node (which will be td
), and process all text nodes within.
这篇关于如何使normalize-space()xpath函数起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!