简体   繁体   English

用于选择所有文本节点的XPath

[英]XPath for selecting all text nodes

I'm writing a JavaScript function that can be used to replace text with HTML code, but to do this I need to be able to access text in text node form. 我正在编写一个JavaScript函数,可以用HTML代码替换文本,但要做到这一点,我需要能够以文本节点的形式访问文本。 The following XPath selects all div tags in a document: 以下XPath选择文档中的所有div标记:

//div

The following XPath selects all elements with the attribute class assigned the value myclass : 以下XPath选择具有赋值myclass的属性class所有元素:

//*[@class="myclass"]

The following selects all of the text (not text nodes) that occurs at any level underneath the element with the ID comments : 以下选择具有ID comments的元素下面的任何级别的所有文本 (不是文本节点):

//*[@id="comments"]//text()

What is an XPath that can be used to select all text nodes under any element? 什么是XPath可用于选择任何元素下的所有文本节点? So, say I want to replace all the non-comment occurrences of the string Hebert and I need all of the text nodes so I can scan through them for that string. 所以,假设我想要替换字符串Hebert所有非注释事件,并且我需要所有文本节点,以便我可以扫描它们以查找该字符串。 Would it use text() in the query? 它会在查询中使用text()吗?

Two options: 两种选择:

  • To select all text nodes whose string value contains the substring "Herbert": 要选择其字符串值包含子字符串“Herbert”的所有文本节点:

     //text()[contains(.,'Herbert')] 
  • To select all text nodes whose string value is "Herbert": 要选择所有文本节点,其字符串值 “赫伯特”:

     //text()[.='Herbert'] 

Note that your comment, 请注意你的评论,

The following selects all of the text (not text nodes) 以下选择所有文本(不是文本节点)

regarding the XPath, //text() , is incorrect. 关于XPath, //text() ,是不正确的。 The node test text() selects text nodes. 节点测试text()选择文本节点。 In a string context, it will return the string-value of the text node, but text() alone most certainly selects actual text nodes. 在字符串上下文中,它将返回文本节点的字符串值 ,但text()单独肯定会选择实际的文本节点。

See also Testing text() nodes vs string values in XPath 另请参阅在XPath中测试text()节点与字符串值

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

相关问题 XPath查找带有文本+符合特定条件的所有后代和兄弟姐妹的节点 - XPath to find nodes with text + all their descendants & siblings that match certain criteria XPath表达式以源顺序选择* all *元素,文本节点和注释节点 - XPath expression to select *all* elements, text nodes, and comment nodes in source order XPath的。 如何选择所有文本节点,但只选择&#39;select&#39;元素中的&#39;option&#39; - XPath. How to select all text nodes but only selected 'option' in 'select' element 在Greasemonkey脚本中,XPath没有在XHTML页面上选择正确的节点 - XPath, in a Greasemonkey script, is not selecting right nodes on an XHTML page 在dom中查找包含带有xpath的单词的文本节点 - Find text nodes that contain a word with xpath in dom 查找所有文本节点 - Find all text nodes 选择 <p> jQuery中没有文本节点的元素 - Selecting <p> elements that have not text nodes in jQuery 选择相邻兄弟节点而不插入文本节点 - Selecting Adjacent Sibling without intervening text nodes XPath:选择包含规范化文本的节点,不包括祖先节点 - XPath: select node that contains normalized text, excluding ancestor nodes 如何在kendo TreeList中选择父节点时选择所有子节点? javascript - How to select all child nodes on selecting parent in kendo TreeList? javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM