2014-04-05 34 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