Skip to content


Subversion checkout URL

You can clone with
Download ZIP


xread XError 2 when parsing XML declaration #4

robstewart57 opened this Issue · 3 comments

2 participants


How should one use Text.XML.HXT.Parser.XmlParsec.xread when reading in a String which contains XML declaration information. e.g. this does not parse:

<?xml version="1.0" encoding="ISO-8859-1"?>

<shiporder orderid="889923"
  <orderperson>John Smith</orderperson>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>

Here's my code:

main = do
  str <- readFile "xml_test.xml"
  let x = xread str
  putStrLn (show x


[NTree (XError 2 "\"string: \"<?xml version=\\\"1.0\\\" encoding=\\\"ISO-8859-1...\"\" (line 1, column 6):\nunexpected xml\nexpecting legal XML name character\n") []]

@UweSchmidt UweSchmidt closed this

Thanks. I'm not familiar with how to compose arrows. To ask another way, here is some Haskell code that demonstrates my problem. Executing test1 (which tries to parse the <?xml .. ?> processing instruction) fails. The test2 function succeeds, which parses the same XML document, but this time with this first processing instruction omitted. Any suggestions on how to introduce a pure arrow to parse xmlDoc1 successfully in test2 ?

module HXTRport where

import Text.XML.HXT.Core

data GParseState = GParseState { stateGenId :: Int } deriving(Show)

-- | Doesn't work
test1 :: (GParseState,[XmlTree])
test1 = runSLA xread initState xmlDoc1
    initState = GParseState { stateGenId = 0 }

{- ERROR running test1
(GParseState {stateGenId = 0},[NTree (XError 2 "\"string: \"<?xml version=\\\"1.0\\\"?><rdf:RDF xmlns:rdf=...\"\" (line 1, column 6):\nunexpected xml\nexpecting legal XML name character\n") []])

-- | Works
test2 :: (GParseState,[XmlTree])
test2 = runSLA xread initState xmlDoc2
    initState = GParseState { stateGenId = 0 }

{- output of runnnig test2
(GParseState {stateGenId = 0},[NTree (XTag "rdf:RDF" [NTree (XAttr "xmlns:rdf") [NTree (XText "") []]]) [NTree (XTag "rdf:Description" [NTree (XAttr "rdf:about") [NTree (XText "") []]]) [NTree (XTag "rdf:value" []) [NTree (XText "xxx") []]]]])

xmlDoc1 :: String
xmlDoc1 = "<?xml version=\"1.0\"?>" ++
    "<rdf:RDF xmlns:rdf=\"\">" ++
      "<rdf:Description rdf:about=\"\">" ++
        "<rdf:value>xxx</rdf:value>" ++
      "</rdf:Description>" ++

xmlDoc2 :: String
xmlDoc2 =
    "<rdf:RDF xmlns:rdf=\"\">" ++
      "<rdf:Description rdf:about=\"\">" ++
        "<rdf:value>xxx</rdf:value>" ++
      "</rdf:Description>" ++

Hi Rob,

parsing a whole document with xread does not work, just an XML content is parsed.
To enable parsing a whole doc in a pure context, I've added a new xreadDoc function.
So if you change

test1 = runSLA xread initState xmlDoc1


test1 = runSLA xreadDoc initState xmlDoc1

you will get a result list of 2 elements, the first for the "<?xml ...?>" and the second
with the content. If you want to get rid of everything around the interesting "rdf:RDF"
element, use

test1 = runSLA (xread >>> isElem) initState xmlDoc1

There is a corresponding hreadDoc for HTML. This extension is in
hxt- I've just uploaded it on hackage.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.