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

Mar*_*rra 17 xml xpath

给出以下XML结构

<html>
  <body>
    <div>
      <span>Test: Text2</span>
    </div>
    <div>
      <span>Test: Text3</span>
    </div>
    <div>
      <span>Test: Text5</span>
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

什么是最好的XPath查询来查找任何spanTest?开头的文本?

A. *_*Rex 23

//span[starts-with(.,'Test')]
Run Code Online (Sandbox Code Playgroud)


参考文献:

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

https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/starts-with

  • 你很有可能想要对它进行normalize-space(),使它成为`// span [starts-with(normalize-space(.),'Test')]`. (2认同)