2012-02-08 81 views
0

所以我解析這個XML文件。一旦我到達一個節點裏面,我有一個子代碼,一個用於描述,一個或多個(孫子)節點保存在節點引用之外。HXT XML搜索不返回預期結果

我第一次嘗試簡單的箭頭符號和< +>聚合得到所有我想要的信息,但是這只是突然的大名單,我想也許有一個更優雅的方式來實現這一

所以我嘗試箭頭做記號代替,使沿線的代碼看起來:

import Text.XML.HXT.Core 

getDocument cale = readDocument [withValidate no] cale 

atName name=deep (hasName name) 
text = getChildren>>>getText 

getList = deep (hasName "list-info")>>> 
proc x -> do 
    desc <- text <<< atName "desc" -< x 
    code <- text <<< atName "code" -< x 
    refs <- getAttrValue "idref" <<< deep (hasName "service-id-ref") -< x 
    returnA -< (desc,code,refs) 

基本上深刻的應該儘量回到同一級別的所有「服務標識引用」節點作爲我的理解去該過濾器,但的滿足「list-info」名稱的第一個節點在2個不同的grand-grand chi上有2個「service-id-ref」子節點每次ldren節點和refs只綁定到第一個節點。所以基本上我期待一個3元組(String,String,[String]),但我得到的只有3個普通字符串。

這是因爲我對符號(或一般)中箭頭的理解不是很清楚,還是應該嘗試用其他方式綁定裁判?

在此先感謝

回答

1

我沒有你的XML文件,所以很難檢查我的答案是正確的。但是根據預期的結果,你需要一個參考字符串列表。

在箭頭獲取列表中有函數listA。

所以,你的代碼可以變成像這樣(未經):

refs <- listA (getAttrValue "idref" <<< deep (hasName "service-id-ref")) -< x 

你讀過的例子:http://www.haskell.org/haskellwiki/HXT/Practical/Simple2

看代碼getTeams3的靈感。

+0

謝謝,就是這樣....我將不得不花費更多的時間盯着箭頭 – 2012-02-09 07:23:16