[Haskell-cafe] How do I insert an element in HaXml?

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Sun May 20 05:55:26 EDT 2007


"Jeremy Shaw" <jeremy.shaw at linspireinc.com> writes:

> How do I create a HaXml filter that adds a new element as a child of
> an existing element. For example, let's say I have the XML document:
>     <a> <b/> <c/> </a>
> How do I add a new element under <a /> so that I have the document:
>     <a> <newElement/> <b/> <c/> </a>

Using the combinators, it goes something like this:

    mkElem "a" [ mkElem "newElement" []
               , children ] `o` tag "a"

Having matched the outer tag, you then rebuild it with some extra stuff
added.  Obviously you would abstract the common pattern if you do this
frequently:

    insertAtStart :: String -> String -> CFilter
    insertAtStart outer newelem =
        mkElem outer [ mkElem newElem []
                     , children ] `o` tag outer

Regards,
    Malcolm


More information about the Haskell-Cafe mailing list