2

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]でした]。

私は何が間違っているのですか?

4

2 に答える 2

2

を使用listAして、適切な時点で結果をリストに収集できます。

import System.Environment (getArgs)
import Text.XML.HXT.Core

processor filename =
  readDocument [withValidate no] filename
    />  hasName "spreadsheet"
    />  hasName "row"
    >>> listA (getChildren >>> hasName "cell" /> hasName "p" /> getText)

main = fmap head getArgs >>= runX . processor >>= print

これにより、必要な結果が印刷されます。

私はあなたの代わりに提供された/>andを使用していることに注意してください。hasNameatTagatTag

于 2012-11-20T11:41:23.067 に答える
0

これはHXTではありませんが、次を使用してxml-conduitで解決できます。

{-# LANGUAGE OverloadedStrings #-}
import Text.XML
import Text.XML.Cursor
import qualified Data.Text as T

main = do
    c <- fmap fromDocument $ Text.XML.readFile def "foo.xml"
    print $ c $// element "row" >=> perRow
  where
    perRow row = [row $/ element "cell" >=> perCell]
    perCell cell = [T.strip $ T.concat $ cell $// content]
于 2012-11-15T07:10:48.187 に答える