[Haskell-cafe] HXT error handling

Mads Lindstrøm mads_lindstroem at yahoo.dk
Sun Apr 4 16:12:57 EDT 2010


I am trying to use HXT to evaluate XPath expressions. The XPath
expressions are not specified by myself, but by users of my program.
However, the idea behind HXT's error handling confuses me. Maybe
somebody can enlighten me.

This program fragment:

evalXPath :: String -> String -> [XmlTree]
evalXPath xpath xml =
  runLA ( xread
          >>> propagateNamespaces
          >>> getXPathTrees xpath
        ) xml

seems to work fine, except when it comes to error handling. If the
xml-string is malformed, the error is simply ignored and the
evalXPath-function just returns an empty list. I can understand what
happens. 'getXPathTrees xpath' tries to match the xpath to a XmlTree
which represents a parse error, and it therefore never matches anything.

What I do not understand, is when this behavior would be desirable. Or
why do the error in 'xread' not propagate throughout the arrow, similar
to how:

foobar :: Either String Int
foobar = do
  _ <- fail "Some failure"
  return 2

the error in 'foobar' would propagate and 'foobar = Left "Some

What if the xpath-parameter contains an invalid XPath expression?
Well, then the output depends upon both the xpath and the xml
-parameters. That is:

 length (evalXPath "/...../" " <foo/> ") == 3


 length (evalXPath "/...../" "<foo/>") == 1

Which just seems strange to me.

If we add a getText to evalXPath:

evalXPath :: String -> String -> [String]
evalXPath xpath xml =
  runLA ( xread
          >>> propagateNamespaces
          >>> getXPathTrees xpath
          >>> getText     -- added here
        ) xml

Then an error in the xpath-parameter will be hidden by the getText
function, similar to how 'getXPathTrees xpath' ignores errors in the
xread function. Again, I understand what happens, I just cannot see why
it is designed as it is.

What I really would like was functions like:

parseStr :: (Monad m) => String -> m XmlTrees
parseStr xml =
  case Parsec.xread xml of
    (x:xs) -> if (Dom.isError x) then fail "Could not parse tree" else
return (x:xs)
    []     -> fail "No XML tree resultet from parsing XML"

And a similar function for the XPath. This could also speed up my
program, as I would not parse the same XPath again and again. A
stand-alone function to parse an XPass expression, would also make it
easy to create a Template Haskell parse-xpath-function, that gave
compile-time error messages and increased performance for XPath
expression know at compile-time.


Mads Lindstrøm

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100404/5b71bf27/attachment.bin

More information about the Haskell-Cafe mailing list