使用xmllint从XML文档中提取多个节点

时间:2017-05-12 11:40:41

标签: xml xmllint

我正在尝试使用xmllint来提取名为//item的多个父节点下的多个节点,如下所示:

<item>
        <title>A title</title>
        <link>http://www.example.com</link>
        <pubDate>Mon, 08 Aug 2016 09:04:11 +0000</pubDate>
        <dc:creator><a name></dc:creator>
        <location><a name></dc:creator>
</item>

如果我只想提取节点(例如标题),我通常会这样做:

xmllint --shell myXml.xml 

然后cat //item/title,这将只检索所有标题标签及其值。我可以使用xmllint获取节点的子集,例如:

        <title>A title</title>
        <link>http://www.example.com</link>
        <pubDate>Mon, 08 Aug 2016 09:04:11 +0000</pubDate>

谢谢,

1 个答案:

答案 0 :(得分:1)

您可以使用or作为元素名称:

cat //item/*[name()="title" or name()="link" or name()="pubDate"]

我必须修复你的XML以使其形成良好。

或者,使用更高级的工具,例如xsh(我碰巧维护):

for //item ls ( title | link | pubDate ) ;
相关问题