Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XPath部分属性已知_Xpath_String Matching - Fatal编程技术网

XPath部分属性已知

XPath部分属性已知,xpath,string-matching,Xpath,String Matching,我知道文档中某个属性的部分值,但不知道全部值。是否有一个字符可以用来表示任何值?例如,输入的标签值为“a.选项1”。我知道上面写的是“选择1”,但不是在“选择1”之前写的是“A”还是“B”。下面是相关的HTML。输入和标签还有其他属性,但它们在每次呈现页面时都不相同,因此我不能将它们用作引用: <tr> <td><input type="checkbox" /><label>A. Choice 1</label></td>

我知道文档中某个属性的部分值,但不知道全部值。是否有一个字符可以用来表示任何值?例如,输入的标签值为“a.选项1”。我知道上面写的是“选择1”,但不是在“选择1”之前写的是“A”还是“B”。下面是相关的HTML。输入和标签还有其他属性,但它们在每次呈现页面时都不相同,因此我不能将它们用作引用:

<tr>
<td><input type="checkbox" /><label>A. Choice 1</label></td>
</tr><tr>
    <td><input type="checkbox" /><label>B. Choice 2</label></td>
</tr><tr>
<td><input type="checkbox" /><label>C. Choice 3</label></td>
</tr><tr>
     <td><input type="checkbox" /><label>D. Choice 4</label></td>
</tr>

我不知道HTML中的A是A、B还是C等等,但我知道正确的输入旁边总是有选项1文本。如果标签包含选项1而不是等于选项1,我该如何说选择它?

您的XPath表达式应该如下所示:

//td[contains(@label, 'Choice 1')]/input
选择标签中包含
Choice 1
的所有
td
元素,然后选择这些
td
元素中的
input
元素


编辑:Tomalak的评论正确地建议了一项改进,以防止与“选项11”(或“选项12345”,…)匹配。

找到了Ronald的解决方案,同时尝试找到一种方法来测试节点是否具有非空的属性“align”或包含文本值“text align”的属性“style”。这些是有问题的“节点”:

<node> 
  <p>This is left-aligned.</p> 
  <div align="left" >This is aligned LEFT using HTML attribute.</div> 
  <p style="text-align: center;" >This is centered using CSS style attribute.</p> 
  <div align="center" >This is CENTERED.</div> 
  <p style="text-align: right;" >This is right-aligned.</p> 
</this>

不,它应该看起来像
//td[contains(concat('',label,''),'Choice 1')]/input
,或者它将匹配
选项11
。正如其他遇到此问题的人注意的那样:您必须使用@label,因为您引用的是一个属性
<node> 
  <p>This is left-aligned.</p> 
  <div align="left" >This is aligned LEFT using HTML attribute.</div> 
  <p style="text-align: center;" >This is centered using CSS style attribute.</p> 
  <div align="center" >This is CENTERED.</div> 
  <p style="text-align: right;" >This is right-aligned.</p> 
</this>
//*[contains(@style, 'align') or @align!='']