我試圖讓我的腦袋繞過HXT,一個解析使用箭頭的XML的Haskell庫。對於我的具體使用情況,我寧願不使用deep
,因爲有些情況下<outer_tag><payload_tag>value</payload_tag></outer_tag>
與<outer_tag><inner_tag><payload_tag>value</payload_tag></inner_tag></outer_tag>
不同,但我碰到了一些覺得它應該工作但不知道的奇怪。是否將因箭頭而產生的箭標記爲有效轉換?
我已經成功地拿出了基於this example從文檔測試用例:
{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
module Main where
import Text.XML.HXT.Core
data Guest = Guest { firstName, lastName :: String }
deriving (Show, Eq)
getGuest = deep (isElem >>> hasName "guest") >>>
proc x -> do
fname <- getText <<< getChildren <<< deep (hasName "fname") -< x
lname <- getText <<< getChildren <<< deep (hasName "lname") -< x
returnA -< Guest { firstName = fname, lastName = lname }
getGuest' = deep (isElem >>> hasName "guest") >>>
proc x -> do
fname <- getText <<< getChildren <<< (hasName "fname") <<< getChildren -< x
lname <- getText <<< getChildren <<< (hasName "lname") <<< getChildren -< x
returnA -< Guest { firstName = fname, lastName = lname }
getGuest'' = deep (isElem >>> hasName "guest") >>> getChildren >>>
proc x -> do
fname <- getText <<< getChildren <<< (hasName "fname") -< x
lname <- getText <<< getChildren <<< (hasName "lname") -< x
returnA -< Guest { firstName = fname, lastName = lname }
driver finalArrow = runX (readDocument [withValidate no] "guestbook.xml" >>> finalArrow)
main = do
guests <- driver getGuest
print "getGuest"
print guests
guests' <- driver getGuest'
print "getGuest'"
print guests'
guests'' <- driver getGuest''
print "getGuest''"
print guests''
之間getGuest
和getGuest'
我擴大deep
到正確數量的getChildren
。由此產生的功能仍然有效。然後我把do
塊以外的getChildren
分解,但這會導致產生的功能失敗。輸出是:
"getGuest"
[Guest {firstName = "John", lastName = "Steinbeck"},Guest {firstName = "Henry", lastName = "Ford"},Guest {firstName = "Andrew", lastName = "Carnegie"},Guest {firstName = "Anton", lastName = "Chekhov"},Guest {firstName = "George", lastName = "Washington"},Guest {firstName = "William", lastName = "Shakespeare"},Guest {firstName = "Nathaniel", lastName = "Hawthorne"}]
"getGuest'"
[Guest {firstName = "John", lastName = "Steinbeck"},Guest {firstName = "Henry", lastName = "Ford"},Guest {firstName = "Andrew", lastName = "Carnegie"},Guest {firstName = "Anton", lastName = "Chekhov"},Guest {firstName = "George", lastName = "Washington"},Guest {firstName = "William", lastName = "Shakespeare"},Guest {firstName = "Nathaniel", lastName = "Hawthorne"}]
"getGuest''"
[]
我覺得這應該是一個有效的轉換來執行,但我對箭頭的理解有點不穩定。難道我做錯了什麼?這是我應該報告的錯誤嗎?
我正在使用HXT版本9.3.1.3(寫作時的最新版本)。 ghc --version打印「The Glorious Glasgow Haskell Compilation System,version 7.4.1」。我也用ghc 7.6.3在一個盒子上測試過,並得到了相同的結果。
XML文件有下列重複結構(完整的文件可以發現here)
<guestbook>
<guest>
<fname>John</fname>
<lname>Steinbeck</lname>
</guest>
<guest>
<fname>Henry</fname>
<lname>Ford</lname>
</guest>
<guest>
<fname>Andrew</fname>
<lname>Carnegie</lname>
</guest>
</guestbook>
你可以發佈一個示例XML文件去與此? – bheklilr
@bheklilr好的,做到了。 –