2014-04-05 75 views
0

我试图从数百个XML文件中切出部分。在XML文档的结构类似于:从XML(xmlstarlet,awk,perl ..)雕刻元素

<document> 
<nodes> 
<node id=123>pages of txt</node> 
<node id-=124>more example pages of txt and sub elements</node> 
</nodes></document> 

我只是想提取所有<node>元素。我一直在尝试使用xmlstarlet:

xmlstarlet sel -t -c “/document/nodes” 

的问题是,它只返回</nodes>

我只需要提取下面的例子:

<node id=123>pages of txt</node> 
<node id-=124>more example pages of txt and sub elements</node> 

谁能推荐一个更好的选择,工具或方法?非常感谢。

+0

你想要什么输出?从你写的内容来看,你似乎只需要从文件的任一端删除''和''。 – Borodin

回答

2

请让你的XPath错误:

xmlstarlet sel -t -c '//node' 

此外,有效的XML所需的所有属性值加引号

<document> 
<nodes> 
<node id="123">pages of txt</node> 
<node id="124">more example pages of txt and sub elements</node> 
</nodes></document> 

我发现这个页面提供了许多有用的XPath的例子:http://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

+0

完美工作,非常感谢:D – user3501474