34

我尝试使用管道 curl 和 xmllint 来解析来自 url 的 xml 输出。但由于某种原因,xml 不会解析 xml,而是显示 curl 生成的 xml。我缺少一个设置?如果将 curl 操作的结果存储为文件并将其用作 xmllint 的输入,则它可以正确解析。

 curl --location --header "Accept: application/rdf+xml" http://www.test.com | xmllint --format - --xpath '//title'
4

2 回答 2

60

似乎xmllint要求-标准输入重定向位于命令的末尾。

curl --location --header "Accept: application/rdf+xml" http://www.test.com \
  | xmllint --format --xpath '//title' -
于 2014-01-04T23:57:25.787 回答
6

更简洁

curl foo.com/somefile.xml | xmllint --format -

解释:

在这里,我们将 xml 从 curl 命令传递到 xmllint 命令xmllint 手册页说

$ man xmllint
> ... The xmllint program parses one or more XML files, specified on the command line as XML-FILE (or the standard input if the filename provided is - ).

这就是我们这样做的原因,因为如果您指定为文件名xmllint --format -,此特定命令将从标准输入读取。旁注,这里-有关于-arg的讨论。我个人不喜欢 stdin 不是默认值,但我不是作者。

于 2020-08-03T20:10:38.147 回答