Question

I am trying to learn to use the xmlgen library, but when I tried the example from http://factisresearch.blogspot.in/2011/05/xmlgen-feature-rich-and-high.html I get a compile error.

Here is the code:

import Text.XML.Generator
import Data.ByteString.Lazy as BSL
import Prelude as P

genXml' :: Xml Doc
genXml' =
  let people = [("Stefan", "32"), ("Judith", "4")]
  in doc defaultDocInfo $
       xelem "people" $
         xelems $ P.map (\(name, age) -> xelem "person" (xattr "age" age <#> xtext name)) people

outputXml :: IO ()
outputXml = BSL.putStr (xrender genXml')

The compiler errors:

$ ghc --make prog.hs
[1 of 1] Compiling Main             ( prog.hs, prog.o )

prog.hs:13:25:
    No instance for (XmlOutput ByteString)
      arising from a use of `xrender'
    Possible fix:
      add an instance declaration for (XmlOutput ByteString)
    In the first argument of `BSL.putStr', namely `(xrender genXml')'
    In the expression: BSL.putStr (xrender genXml')
    In an equation for `outputXml':
        outputXml = BSL.putStr (xrender genXml')

The compiler seems to be deducing that "xrender genXML'" needs to be of type "XmlOutput ByteString" and there is an instance of this in the xmlgen library. So please help me with understanding what this error means, and how it can be fixed.

If it helps, I am using the Haskell Platform in Ubuntu 12.10, ghc 7.4.2 and the xmlgen 0.4.0.3

Was it helpful?

Solution

The compiler seems to be deducing that "xrender genXML'" needs to be of type "XmlOutput ByteString"

No, the compiler deduces (from the use of BSL.putStr) that xrender genXML' needs to be of type Data.ByteString.Lazy.ByteString.

Since the type of xrender is

xrender :: (Renderable r, XmlOutput t) => Xml r -> t

a necessary condition to be able to instantiate t with Data.ByteString.Lazy.ByteString is an XmlOutput instance for lazy ByteStrings.

Since there is such an instance exported from Text.XML.Generator, the only cause for the error message that I see is that your

import Data.ByteString.Lazy as BSL

imports the module from a different version of the bytestring package than the xmlgen library was built against.

Can you check that with ghc-pkg describe xmlgen, which lists the bytestring version it was built against among the dependencies, and ghc-pkg list bytestring to check which bytestring versions you have installed?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top