xml - 使用 xmllint 返回多个结果

标签 xml linux bash terminal xmllint

我正在尝试提取包含指定标记的 XML 文件的所有部分。我在网上搜索了一下,发现这个很管用。

xmllint --xpath "/all/string(//title)"

但是它只返回第一个结果,我怎样才能让它找到所有结果呢?谢谢!

示例 XML

<programme start="20170913125500 +0100" stop="20170913144500 +0100" channel="3b6963d34ba31ea21db5c3aee8e3b26f">
  <title lang="eng">Yangtse Incident</title>
  <sub-title lang="eng">(1957) Michael Anderson&apos;s drama, starring Richard Todd and William Hartnell, tells the true story of HMS Amethyst, a British frigate captured by Chinese communists during Mao&apos;s revolution.  [AD,S]</sub-title>
</programme>
<programme start="20170913144500 +0100" stop="20170913165500 +0100" channel="3b6963d34ba31ea21db5c3aee8e3b26f">
  <title lang="eng">The Comancheros</title>
  <sub-title lang="eng">(1961) Western starring John Wayne and Stuart Whitman. A Texas Ranger is forced to team up with his prisoner while he&apos;s on a covert mission to take on a band of thieves and gunrunners.  [S]</sub-title>
</programme>
<programme start="20170913165500 +0100" stop="20170913185500 +0100" channel="3b6963d34ba31ea21db5c3aee8e3b26f">
  <title lang="eng">The Cockleshell Heroes</title>
  <sub-title lang="eng">(1955) World War II drama. In a true-life tale of incredible bravery, ten marines try to break the blockade of Bordeaux. With José Ferrer, Trevor Howard, Victor Maddern and Anthony Newley.  [S]</sub-title>
</programme>
<programme start="20170913185500 +0100" stop="20170913190500 +0100" channel="3b6963d34ba31ea21db5c3aee8e3b26f">
  <title lang="eng">Dunkirk Interview Special</title>
  <sub-title lang="eng">Stars Harry Styles, Mark Rylance, Jack Lowden, Fionn Whitehead and Tom Glynn-Carney talk about making director Christopher Nolan&apos;s intense Second World War dramatic thriller.  [S]</sub-title>
</programme>

结果应该是

Yangtse Incident
The Comancheros
The Cockleshell Heroes
Dunkirk Interview Special

最佳答案

我无法让它以您想要的方式仅与 xmllint 一起工作。最接近的是:

 xmllint --xpath "//something/programme/title/text()" test.xml

但这会在一行中为您提供所有输出。

对我来说最好的解决方案是:

xmllint --xpath "//something/programme/title" test.xml | sed 's/<\/title>/\n/g' | sed 's/<title lang="eng">//g'

当然,您可以使用任何其他工具来清理输出。

关于xml - 使用 xmllint 返回多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46199079/

相关文章:

bash - 在 solaris 上规范化路径名

linux - !# :3? 是什么意思

javascript - 如何通过jquery遍历xml?

xml - 是否有用于Delphi的“快速信息集” XML压缩库?

linux - 在 Amazon EC2 实例上安装 librsync

linux - 为什么由 systemd 启动的进程与以交互方式启动时的行为不同?

linux - 在 Bash 脚本中,函数的第二个参数未按预期处理

java - 使用 JAXB 将 XML 解析为不同的数据结构

java - 重复一定时间后启用按钮

linux - 无法在 bash 中增加变量