XPath查询搜索具有特定文本的元素

时间:2020-03-06 14:51:36  来源:igfitidea点击:

给定以下XML结构

<html>
  <body>
    <div>
      <span>Test: Text2</span>
    </div>
    <div>
      <span>Test: Text3</span>
    </div>
    <div>
      <span>Test: Text5</span>
    </div>
  </body>
</html>

定位以Test开头的文本的span的最佳XPath查询是什么?

解决方案

//span[starts-with(.,'Test')]

参考:

http://www.w3.org/TR/xpath/#function-starts-with

https://developer.mozilla.org/zh-CN/docs/Web/XPath/Functions/starts-with