Skip to content

Instantly share code, notes, and snippets.

@tfausak
Created December 12, 2017 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfausak/9ad3612a2f20bfcc4951d0a4ff9153d9 to your computer and use it in GitHub Desktop.
Save tfausak/9ad3612a2f20bfcc4951d0a4ff9153d9 to your computer and use it in GitHub Desktop.
XML lenses have stumped me.
-- stack --resolver nightly-2017-12-01 script
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.Text.Lazy.IO as LazyText
import Text.XML
import Text.XML.Lens
main :: IO ()
main = do
let original = parseText_ def "<root> <old>1</old> <old>2</old> </root>"
let document = original & root ./ named "old" .~ Element "new" mempty []
LazyText.putStrLn $ renderText def { rsXMLDeclaration = False } document
-- This prints:
-- <root> <new/> <new/> </root>
-- But I want:
-- <root> <new/> </root>
-- This is, *all* <old> elements replaced by a *single* <new> element.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment