Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XPath表达式,用于选择给定节点中的所有文本及其子节点的文本_Xpath - Fatal编程技术网

XPath表达式,用于选择给定节点中的所有文本及其子节点的文本

XPath表达式,用于选择给定节点中的所有文本及其子节点的文本,xpath,Xpath,基本上,我需要刮一些有嵌套标签的文本 大概是这样的: <div id='theNode'> This is an <span style="color:red">example</span> <b>bolded</b> text </div> 我已经为此奋斗了一个多小时,没有任何结果 任何帮助都是值得赞赏的元素节点的作用是按文档顺序将元素节点的所有文本节点后代的字符串值串联起来 您希望调用div元素上的XPathstrin

基本上,我需要刮一些有嵌套标签的文本

大概是这样的:

<div id='theNode'>
This is an <span style="color:red">example</span> <b>bolded</b> text
</div>
我已经为此奋斗了一个多小时,没有任何结果

任何帮助都是值得赞赏的

元素节点的作用是按文档顺序将元素节点的所有文本节点后代的字符串值串联起来

您希望调用div元素上的XPath
string()
函数

string(//div[@id='theNode'])
您还可以使用该函数来减少源文档中可能由于换行和缩进而出现的不需要的空白。这将删除前导和尾随空格,并用单个空格替换空格字符序列。将节点集传递给normalize-space()时,该节点集将首先转换为其字符串值。如果没有传递参数来规范化空间,它将使用上下文节点

normalize-space(//div[@id='theNode'])

// if theNode was the context node, you could use this instead
normalize-space()
您可能希望使用比我使用的示例XPath更有效的方法来选择上下文节点。例如,以下Javascript示例可以在某些浏览器中针对该页面运行

var el = document.getElementById('question');
var result = document.evaluate('normalize-space()', el, null ).stringValue;
span
b
元素之间的纯空白文本节点可能有问题。

使用

string(//div[@id='theNode'])
normalize-space(string(//div[@id='theNode']))
  " This is an 
    example
    bolded text 
"
===========
  "This is an example bolded text"
计算此表达式时,结果是文档中第一个(希望是唯一的)
div
元素的字符串值

由于元素的字符串值在中定义为其所有文本节点子体的文档顺序连接,因此这正是所需的字符串

由于这可能包括许多所有空格文本节点,因此您可能希望消除连续的前导和尾随空格,并用单个空格字符替换任何此类中间空格:

使用

string(//div[@id='theNode'])
normalize-space(string(//div[@id='theNode']))
  " This is an 
    example
    bolded text 
"
===========
  "This is an example bolded text"
基于XSLT的验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  "<xsl:copy-of select="string(//div[@id='theNode'])"/>"
===========
  "<xsl:copy-of select="normalize-space(string(//div[@id='theNode']))"/>"
 </xsl:template>
</xsl:stylesheet>
<div id='theNode'> This is an 
    <span style="color:red">example</span>
    <b>bolded</b> text 
</div>
这个怎么样:

/div/text()[1]|/div/span/text()|/div/b/text()|/div/text()[2]


嗯,不过我不确定最后一部分。您可能必须使用它。

如果您在python中使用scrapy,您可以使用
后代或self::*/text()
。完整示例:

txt=”“”
这是一个粗体文本示例
"""
选择器=scrapy.selector(text=txt,type=“html”)#从html文本创建html文档
all_txt=selector.xpath('//div/genderant或self:*/text()).getall()
final_txt=''.join(u for u)in all_txt.strip()
打印(最终文本)#“这是一个粗体文本示例”
正常代码

//div[@id='theNode']

获取所有文本,但如果它们被拆分,则

//div[@id='theNode']/text()


不确定,但如果您提供链接,我将尝试

@MartinTaleski:此答案中的XPath表达式的计算结果为字符串
“true”
。这真的是你想要得到的吗?大概答案是在Dimitre的评论之后编辑的,因此评论不再适用。你可能想看看我的答案,与目前接受的答案不同,我的答案是正确的。