parsing XML with Haskell with HXT

135 views Asked by At

I am trying to parse an XML file with https://hackage.haskell.org/package/hxt. In the code below, I need to parse the inner elements with an extra parameter obtained from an attribute in the outer tag. But i is not available in the second line (second arrow?). How can I pass i to the getTerminal?

getSentence = atTag "s" >>>
  proc x -> do
    i   <- getAttrValue "id" -< x
    ts  <- listA (getTerminal i) <<< atTag "terminals"    -< x
    returnA -< Sentence { sid = i, terminals = ts }

...

getTerminal sid = atTag "t" >>> 
  proc x -> do
    i   <- getAttrValue "id"    -< x    
    lem <- getAttrValue "lemma" -< x
    returnA -< Terminal { lemma = lem, tid = nid i }
  where
    nid x = fromMaybe x (stripPrefix (sid ++ "_") x)

That is, i contains the value that I would like to pass to getTerminal.

1

There are 1 answers

0
duplode On

Using -<< instead of -< makes local bindings available to the arrow:

getSentence = atTag "s" >>>
  proc x -> do
    i   <- getAttrValue "id" -< x
    ts  <- listA (getTerminal i) <<< atTag "terminals" -<< x
    returnA -< Sentence { sid = i, terminals = ts }

Note that -<< is implemented in terms of app from ArrowApply. For instance, syntactical awkwardness aside, its occurrence above could be replaced with:

    ts  <- app <<< listA . getTerminal *** atTag "terminals" -< (i, x)

The relevant arrows of hxt are instances of ArrowApply, so -<< can be used with them.