属性值以特定字符串结尾的元素的XPath?

时间:2022-11-28 00:26:08

Given that the HTML contains:

鉴于HTML包含:

  <div tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination" class="panel panel-default"></div>

How do we write the following expression in XPath:

我们如何在XPath中编写以下表达式:

Find a <div> element whose tagname attribute ends with the string 'Destination'

找到一个

元素,其tagname属性以字符串'Destination'结尾

I've been searching for days and I can't come up with something that works. Among many, I tried for example:

我一直在寻找几天,我无法想出一些有用的东西。在众多中,我试过例如:

div[contains(@tagname, 'Destination')]

4 个解决方案

#1


6  

XPath 2.0

//div[ends-with(@tagname, 'Destination')]

XPath 1.0

//div[substring(@tagname,string-length(@tagname) -string-length('Destination') +1) 
      = 'Destination']

#2


4  

XPath 2 or 3: There's always regex.

XPath 2或3:总是正则表达式。

.//div[matches(@tagname,".*_Destination$")]

#3


3  

You can use ends-with (Xpath 2.0)

你可以使用ends-with(Xpath 2.0)

//div[ends-with(@tagname, 'Destination')]

#4


1  

You could use the below xpath which will work with Xpath 1.0

您可以使用下面的xpath,它将与Xpath 1.0一起使用

//div[string-length(substring-before(@tagname, 'Destination')) >= 0 and string-length(substring-after(@tagname, 'Destination')) = 0 and contains(@tagname, 'Destination')]

// div [string-length(substring-before(@ pentame,'Destination'))> = 0和string-length(substring-after(@ pentame,'Destination'))= 0并包含(@tagname,'Destination “)]

Basically it checks if there is any string ( or no strings ) before the first occurrence of Destination but there should not be any text after the Destination

基本上它会在第一次出现Destination之前检查是否有任何字符串(或没有字符串),但是在Destination之后不应该有任何文本

Test input :

测试输入:

<root>
<!--Ends with Destination-->
<div tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination" class="panel panel-default"></div>
<!--just Destination-->
<div tagname="Destination" class="panel panel-default"></div>
<!--Contains Destination-->
<div tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination_some_text" class="panel panel-default"></div>
<!--Doesn't contain destination-->
<div tagname="779853cd-355b-4242-8399-dc15f95b3276" class="panel panel-default"></div>
</root>

Test output:

测试输出:

<div class="panel panel-default"
     tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination"/>
<div class="panel panel-default" tagname="Destination"/>

#1


6  

XPath 2.0

//div[ends-with(@tagname, 'Destination')]

XPath 1.0

//div[substring(@tagname,string-length(@tagname) -string-length('Destination') +1) 
      = 'Destination']

#2


4  

XPath 2 or 3: There's always regex.

XPath 2或3:总是正则表达式。

.//div[matches(@tagname,".*_Destination$")]

#3


3  

You can use ends-with (Xpath 2.0)

你可以使用ends-with(Xpath 2.0)

//div[ends-with(@tagname, 'Destination')]

#4


1  

You could use the below xpath which will work with Xpath 1.0

您可以使用下面的xpath,它将与Xpath 1.0一起使用

//div[string-length(substring-before(@tagname, 'Destination')) >= 0 and string-length(substring-after(@tagname, 'Destination')) = 0 and contains(@tagname, 'Destination')]

// div [string-length(substring-before(@ pentame,'Destination'))> = 0和string-length(substring-after(@ pentame,'Destination'))= 0并包含(@tagname,'Destination “)]

Basically it checks if there is any string ( or no strings ) before the first occurrence of Destination but there should not be any text after the Destination

基本上它会在第一次出现Destination之前检查是否有任何字符串(或没有字符串),但是在Destination之后不应该有任何文本

Test input :

测试输入:

<root>
<!--Ends with Destination-->
<div tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination" class="panel panel-default"></div>
<!--just Destination-->
<div tagname="Destination" class="panel panel-default"></div>
<!--Contains Destination-->
<div tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination_some_text" class="panel panel-default"></div>
<!--Doesn't contain destination-->
<div tagname="779853cd-355b-4242-8399-dc15f95b3276" class="panel panel-default"></div>
</root>

Test output:

测试输出:

<div class="panel panel-default"
     tagname="779853cd-355b-4242-8399-dc15f95b3276_Destination"/>
<div class="panel panel-default" tagname="Destination"/>