HXT解析列表

时间:2013-10-29 14:19:57

标签: haskell hxt

我最近发布了关于使用HXT泡菜进行解析的文章。经过一番阅读后,我决定改用常规的HXT。

但是,我无法创建列表,即。我有一个XML文档:

<meta>
    <sampleQuery>sample1</sampleQuery>
    <sampleQuery>sample2</sampleQuery>
</meta>

和解析函数

parseMeta =
  proc x -> do
    meta          <- deep (isElem >>> hasName "meta") -< x
    sampleQueries <- getText <<< getChildren <<< deep (hasName "sampleQuery") -< meta
    returnA -< Meta sampleQueries

sampleQueries应该具有[String]类型(在这种情况下为[“sample1”,“sample2”])但我无法实现此目的。

1 个答案:

答案 0 :(得分:2)

箭头符号在这里似乎有些过分。

import Text.XML.HXT.Core

xml = unlines 
  [ "<meta>"
  , "<sampleQuery>sample1</sampleQuery>"
  , "<sampleQuery>sample2</sampleQuery>"
  , "</meta>"
  ]

queries = hasName "meta" /> hasName "sampleQuery" /> getText

main = runX (readString [] xml /> queries) >>= print

这将按预期打印["sample1","sample2"]