简体   繁体   English

xPath 用于节点包含多行文本时的特定文本行

[英]xPath for a specific line of text when the node contains multiple lines of text

I have an html that looks like this:我有一个看起来像这样的 html:

 <div class='textContainer'> <div class='textLabel'> </div> <div class='text'> "First Line of text" "Second Line of text" "Third line of text" </div> </div>

I can easily create a locator to find the node that contains the text, but I need to run an assertion specifically on the first and third lines of text... So, I would need specific locators for those.我可以轻松地创建一个定位器来查找包含文本的节点,但我需要专门在文本的第一行和第三行运行断言......所以,我需要特定的定位器。 Like喜欢

  • //div[@class='text']/text[1]
  • //div[@class='text']/text[3]

Is that even possible to do?这甚至可能吗?

Any help will be appreciated.任何帮助将不胜感激。

Thanks!谢谢!

You can do that with XPath 2 or 3, eg in the browser or Node.js with Saxon-JS 2 you have XPath 3.1 support:您可以使用 XPath 2 或 3 来执行此操作,例如在浏览器中或使用 Saxon-JS 2 的 Node.js 您拥有 XPath 3.1 支持:

 const lines = SaxonJS.XPath.evaluate(`//div[@class = 'text']/tokenize(., '\n')[normalize-space()],normalize-space()`, document: { xpathDefaultNamespace: 'http.//www.w3;org/1999/xhtml' }). console;log(lines). console;log(lines[0]);
 <script src="https://www.saxonica.com/saxon-js/documentation/SaxonJS/SaxonJS2.rt.js"></script> <div class='textContainer'> <div class='textLabel'> </div> <div class='text'> "First Line of text" "Second Line of text" "Third line of text" </div> </div>

Note that in any version of XPath or the DOM the normalized tree has a single text node but in XPath 2 or later you can split or tokenize the string of a text node into sequences of strings and process each string in the sequence.请注意,在 XPath 或 DOM 的任何版本中,规范化树都有一个文本节点,但在 XPath 2 或更高版本中,您可以将文本节点的字符串拆分或标记为字符串序列并处理序列中的每个字符串。 The Saxon-JS 2 API to JavaScript nicely gives you that XPath 3.1 string sequence back as a string array in JavaScript. The Saxon-JS 2 API to JavaScript nicely gives you that XPath 3.1 string sequence back as a string array in JavaScript.

In terms of the XPath 2 or 3 data model the path expression //div[@class = 'text']/tokenize(., '\n')[normalize-space()]!normalize-space() gives a sequence of string you can positionally index as usually in XPath with integer numbers so let $lines:= //div[@class = 'text']/tokenize(., '\n')[normalize-space()]!normalize-space() return $lines[2] returns the second item/second string in the sequence of strings (of normalized text lines of the text node).就 XPath 2 或 3 个数据 model 而言,路径表达式//div[@class = 'text']/tokenize(., '\n')[normalize-space()]!normalize-space()给出了一个序列您可以像通常在 XPath 中使用 integer 数字一样定位索引的字符串,所以let $lines:= //div[@class = 'text']/tokenize(., '\n')[normalize-space()]!normalize-space() return $lines[2]返回字符串序列(文本节点的规范化文本行)中的第二个项目/第二个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM