[Haskell-cafe] understanding HXT's insertChildrenAfter

Jake lambda at 10k.org
Mon Jul 20 22:15:35 EDT 2009


Playing on HXT 8.3.0 via cabal on GHC 6.10.2.

While there are plenty of useful examples of extract
data from XML, applying the appropriate API to transform
and/or merging still baffle me. In this case, it
is the insertChildrenAfter,

let's try it with the following example: given 2 files

--- update.xml
<?xml version="1.0" encoding="UTF-8"?>
<update>
     <hot>sky is now orange</hot>
     <hot>fish is fresh</hot>
     <hot>violets are blue</hot>
</update>

--- master.xml
<?xml version="1.0" encoding="UTF-8"?>
<news>
     <page1>
     <hot>sky is blue</hot>
     <hot>roses are red</hot>
     </page1>
     <page2>
     <story>game shipped on sunday</story>
     </page2>
</news>


with the intention of augment the master list with the new items:

import Text.XML.HXT.Arrow
import qualified Text.XML.HXT.DOM as DOM

getMaster =
         readDocument [(a_validate,v_0)] "master.xml"
     >>> removeDocWhiteSpace

getUpdate =
         readDocument [(a_validate,v_0)] "update.xml"
     >>> removeDocWhiteSpace

selectUpdateArea =
         getChildren >>> hasName "news"
     >>> getChildren >>> hasName "page1"

updateContent =
     insertChildrenAfter
         (selectUpdateArea)
         (getUpdate >>> getChildren >>> getChildren)

main =
     do runX (
             getMaster
         >>> updateContent
         >>> putXmlTree "-"
             )


instead the following tree is produced:

---XTag "/"
    |
    +---XTag "hot"
    |   |
    |   +---XText "sky is red"
    |
    +---XTag "hot"
    |   |
    |   +---XText "fish is fresh"
    |
    +---XTag "hot"
    |   |
    |   +---XText "violets are blue"
    |
    +---XTag "news"
        |
        +---XTag "page1"
        |   |
        |   +---XTag "hot"
        |   |   |
        |   |   +---XText "sky is blue"
        |   |
        |   +---XTag "hot"
        |       |
        |       +---XText "roses are red"
        |
        +---XTag "page2"
            |
            +---XTag "story"
                |
                +---XText "game shipped "


Can someone give some examples of how insertChildrenAfter
works?  How about also some general guidelines when comes
to merging/grafting XML documents.

Jake


More information about the Haskell-Cafe mailing list