【发布时间】:2012-11-15 05:51:18
【问题描述】:
我正在尝试使用 HXT 解析 ods(libreoffice 电子表格)文件,但遇到了问题。 在电子表格中,一行有许多单元格(所有单元格名称都为“cell”),而电子表格有很多行(所有单元格名称为 row)。 当我尝试获取单元格的文本时,代码将它们混合在一起,最终得到一大堆不按行分隔的单元格......
尝试解析以下内容时:
<spreadsheet>
<row>
<cell> <p>ABC</p> </cell>
<cell> <p>DEF</p> </cell>
<cell> <p>GHI</p> </cell>
</row>
<row>
<cell> <p>abc</p> </cell>
<cell> <p>def</p> </cell>
<cell> <p>ghi</p> </cell>
</row>
<row>
<cell> <p>123</p> </cell>
<cell> <p>456</p> </cell>
<cell> <p>789</p> </cell>
</row>
</spreadsheet>
附上代码:
import Text.XML.HXT.Core
play arg = do { results <- runX (processor arg) ; print results }
atTag x = getChildren >>> isElem >>> hasName x
processor filename =
readDocument [withValidate no] filename >>>
atTag "spreadsheet" >>>
atTag "row" >>>
atTag "cell" >>>
atTag "p" >>>
getChildren >>> getText
它给出 [ABC, DEF, GHI, abc, def, ghi, 123, 456, 789] 而我想要的是 [[ABC, DEF, GHI], [abc, def, ghi], [123, 456 , 789]]。
我做错了什么?
【问题讨论】:
-
atTag的类型是什么?如果是Tree -> [Tree],那么您应该可以先执行let rows = readDocument >>> ... >> atTag "row",然后再执行map (\t -> t >>> atTag "cell" >>> ... >>> getText) rows,以获得满意的结果。虽然我从来没有使用过 HXT...
标签: list haskell spreadsheet hxt