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
Bash 如何选择元素后面的文本?_Bash_Xpath_Xmllint - Fatal编程技术网

Bash 如何选择元素后面的文本?

Bash 如何选择元素后面的文本?,bash,xpath,xmllint,Bash,Xpath,Xmllint,我有以下选择元素的xmllint示例: $ curl -s http://lists.opencsw.org/pipermail/users/2015-January/date.html | xmllint --html --xpath '/html/body/p/b[contains(., "Messages:")]' - <b>Messages:</b> 我原以为下面的兄弟姐妹轴可能会给我确切的数字,但事实并非如此: $ curl -s http://lists.o

我有以下选择元素的
xmllint
示例:

$ curl -s http://lists.opencsw.org/pipermail/users/2015-January/date.html |
xmllint --html --xpath '/html/body/p/b[contains(., "Messages:")]' -
<b>Messages:</b>
我原以为
下面的兄弟姐妹轴可能会给我确切的数字,但事实并非如此:

$ curl -s http://lists.opencsw.org/pipermail/users/2015-January/date.html |
xmllint --html --xpath '/html/body/p/b[contains(., "Messages:")]/following-sibling::*' -
XPath set is empty

您所关注的这个文本节点实际上是一个后续同级节点,但它是一个文本节点,而不是元素节点。像这样的表情

following-sibling::*
仅查找以下作为元素的同级。要匹配文本节点,请使用
text()

上面的命令在我的计算机上不起作用,在MacOSX上使用bash,但我相信它对您有效。如果我首先从
curl
保存结果,然后使用

$ xmllint example.html --html --xpath '/html/body/p/b[contains(., "Messages:")]/following-sibling::text()'
结果是
\u 28
。这不是下划线,而是我想指出的空白。要删除前导空格,请使用

$ xmllint example.html --html --xpath 'normalize-space(/html/body/p/b[contains(., "Messages:")]/following-sibling::text())'

不,使用正则表达式并不是一个真正的选择

$ xmllint example.html --html --xpath '/html/body/p/b[contains(., "Messages:")]/following-sibling::text()'
$ xmllint example.html --html --xpath 'normalize-space(/html/body/p/b[contains(., "Messages:")]/following-sibling::text())'