[Haskell-cafe] HXT: Replace an element with its text

Michael Orlitzky michael at orlitzky.com
Tue Jun 26 07:58:20 CEST 2012


I would like to replace,

  <body><a href="#">foo</a></body>

with,

  <body>foo</body>

using HXT. So far, the closest I've come is to parse the HTML and apply
the following stuff:

  is_link :: (ArrowXml a) => a XmlTree XmlTree
  is_link =
    hasName "a"

  replace_links_with_their_text :: (ArrowXml a) => a XmlTree XmlTree
  replace_links_with_their_text =
    processTopDown $ (getText >>> mkText) `when` is_link

Unfortunately, this just removes the "a" element and its text entirely.
The other-closest solution is,

  replace_links_with_their_text :: (ArrowXml a) => a XmlTree XmlTree
  replace_links_with_their_text =
    processTopDown $ (txt "foo") `when` is_link

Of course, I don't want to hard-code the value "foo", and I can't figure
out a way to feed the element's text back into 'txt'.

Anyone tried this before?



More information about the Haskell-Cafe mailing list