XPath for element whose attribute value ends with a specific string?

时光毁灭记忆、已成空白 提交于 2019-12-01 17:32:19

XPath 2.0

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

XPath 1.0

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

XPath 2 or 3: There's always regex.

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

You can use ends-with (Xpath 2.0)

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

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

//div[string-length(substring-before(@tagname, 'Destination')) >= 0 and string-length(substring-after(@tagname, 'Destination')) = 0 and contains(@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

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"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!