[Haskell] Re: understanding HaXml and escaping

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Thu Oct 28 11:46:40 EDT 2004


"S. Alexander Jacobson" <alex at alexjacobson.com> writes:

> Is there a good entry point into HaXml?
> I've now spent some time trying to understand it
> and feel like I've gotten nowhere.

It is a large package with many diverse facilities, so I'm not
surprised.  I take it you have read the ICFP'99 paper linked to from
the HaXml webpage?  To give a fuller answer, it would be helpful to
know more about your specific XML needs.

> The Haddock documentation enumerates what each
> function does, but I still don't know how to
> produce a valid XML document?

Where does your document come from?  Has it been parsed already,
then manipulated, and you want to spit it out again?  Or are you
trying to generate a fresh document from nothing?  Or perhaps you
have some existing Haskell data-structure you want to convert to XML
for external representation only?

> For example, this is obviously the wrong way to
> go:
> 
>   simp2 = document $ Document (Prolog Nothing [] Nothing []) [] $
> 		Elem "root" [("attr",AttValue [Left "v\"al"])]
> 		 [CString False "<<<<<>>&&&"]
> 
> Because, it produces the obviously wrong:
> 
>   <root attr="v"al"><<<<<>>&&&</root>

Ah.  Escaping of special characters within text is a separate
issue.  It need only be done once, just before output.
See Text.XML.HaXml.Escape - specifically you want something like

    simp2 = document $ Document (Prolog Nothing [] Nothing []) [] $
		xmlEscape stdXmlEscaper $
  		Elem "root" [("attr",AttValue [Left "v\"al"])]
  		 [CString False "<<<<<>>&&&"]

> I assume/hope that the combinators properly
> encode/escape attribute values and CDATA,

No, at the moment they don't.  You can always do it one-shot at the
end, as in the example above, although it would probably be better
from a correctness point of view if the combinators did as you suggest.

> And once I've done so, is there a way to put PIs
> in via the combinators

Currently, there are no combinators specifically for generating PIs
(simply because no-one has asked for them before), but it would be
extremely easy to add.  For instance:

    mkPI :: String -> String -> CFilter
    mkPI pitarget str = \t-> [ CMisc (PI (pitarget,str)) ]

Regards,
    Malcolm


More information about the Haskell mailing list