从XML雕刻元素(xmlstarlet,awk,perl ..)

时间:2014-04-05 15:14:28

标签: xml perl awk xmlstarlet

我试图从数百个XML文件中划分出来。 XML文档的结构类似于:

<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>

任何人都可以推荐更好的选择,工具或方法吗?非常感谢。

1 个答案:

答案 0 :(得分: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