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_Xquery_Xidel - Fatal编程技术网

xpath:提取节点的尾部文本

xpath:提取节点的尾部文本,xpath,xquery,xidel,Xpath,Xquery,Xidel,我有一个包含以下内容的html文件 ... <table><tbody> ... <tr> <td><span class="myclass">C</span> <a href="/myurl" title="myclick">mytext</a>

我有一个包含以下内容的html文件

...
<table><tbody>
...
            <tr>
              <td><span class="myclass">C</span>
                <a href="/myurl" title="myclick">mytext</a>
                   tailing text
              </td>
            </tr>
...
</tbody></table>
...
。。。
...
C
拖尾文本
...
...
我想提取信息并以以下格式写入TSV文件

C<TAB>mytext<T>tailing text
Cmytexttailing文本
到目前为止,我只能用这个xpath代码来提取前两列。有人能告诉我如何提取第三列吗?谢谢

xidel -s -e '//table/tbody/tr/td/join((span, a), x:cps(9))' - < infile.html
xidel-s-e'//table/tbody/tr/td/join((span,a),x:cps(9))——
您可以使用以下命令:

xidel infile.html --xpath '//table/tbody/tr/td/string-join((span, "<TAB>", a, "<T>", a/following::text()[1]))'
xidel infle.html--xpath'//table/tbody/tr/td/string join((span,“,a,”,a/following::text()[1]))

xidel--xpath'//table/tbody/tr/td/string-join((span,“,a,”,a/following::text()[1])”--
另一种方法是

xidel infile.html --xpath '//table/tbody/tr/td/concat(span, "<TAB>", a, "<T>", a/following-sibling::text()[1])' 
xidel infle.html--xpath'//table/tbody/tr/td/concat(span,“,a,”,a/以下同级::text()[1])'
在所有三种情况下,输出为:

C<TAB>mytext<T>tailing text
Cmytexttailing文本

如果使用
//table/tbody/tr/td/string连接(node()[normalize-space()],x:cps(9))
将得到三列,但最后一列可能在文本前后包含空格,因此可能
//table/tbody/tr/td/string连接(node()[normalize-space()]/normalize-space(),x:cps(9))
确保您不会得到未显示在所需结果中的空白。

当有许多
tr
s时,这似乎非常缓慢。是因为
a/following::text()[1]
?谢谢。另一种方法是
xidel infle.html--xpath'//table/tbody/tr/td/concat(span,“,a,”,a/following sibling::text()[1])
。但这是绝对最小值。我怀疑它会变得更好。性能问题可能是由于表达式中的
/
导致的,因为这会检查整个文档中的所有表。我不认为
/
是问题所在,因为删除
a/following sibling::text()[1]
会使运行更快完成。您是说
/
a/以下同级::text()[1]
结合会导致性能问题吗?谢谢。你确定你的“infle.html”中保留了空格(如OP中所示)?如果是这样,那么您应该看到
a/following::text()[1]
返回3行<代码>跟踪文本
,周围有大量空白。另外,OP想要创建一个TSV文件,所以我怀疑他/她所要的字符串是“和”。@Reino Yes。我需要制表符。我在第二列有一个额外的制表符。你知道为什么吗
xidel-s--xpath'//table/tbody/tr/td/string-join(node()/normalize-space(),x:cps(9))-好吧,您没有使用我建议的,而是一个选择空白文本节点的变体,即使减少为空字符串。为了防止出现这种情况,我在两个建议中都使用了谓词
[normalize-space()]
,您已经删除了这两个建议。但即使没有尾随文本,我也需要尾随空字段。然后,您需要解释需要哪些文本节点,哪些不需要。也许zx485对
a/following-text()
的回答是您想要的,它显式地选择
span
子项、
a
子项和
a
子项后面的文本,而我试图选择任何子项的文本,在空白规范化之后,这些子项都有文本内容。但是如果您希望读取
a
元素后的空格,那么这种方法就不起作用。
//table/tbody/tr/td/join((span,a,a/following::text()[1],a/@href),x:cps(9))
是我需要的(我添加了href)。但是它太慢了。但您的代码似乎要快得多,但它修剪了我仍然希望保留的空字段。我想知道是否有什么可以做,以实现快速的性能,但保持空的尾部字段。
C<TAB>mytext<T>tailing text