I'm using HXT to transform a XML into another XML document. Selecting some nodes using XPath.
Here I want to collect all the authors in a new authors
node.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<authors>
<author>Giada De Laurentiis</author>
</authors>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<authors>
<author>J K. Rowling</author>
</authors>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<authors>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
</authors>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<authors>
<author>Erik T. Ray</author>
</authors>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
<?xml version="1.0" encoding="ISO-8859-1"?>
<authors>
<author>Giada De Laurentiis</author>
<author>J K. Rowling</author>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<author>Erik T. Ray</author>
</authors>
module Main where
import Text.XML.HXT.Core
import System.Environment
import Text.XML.HXT.XPath.Arrows
main :: IO ()
main = do
[src, dst] <- getArgs
runX $ readDocument [] src
>>>
root [] [ selem "authors" [getXPathTrees "/bookstore/book/authors/author"] ]
>>>
writeDocument [withIndent yes, withOutputEncoding isoLatin1] dst
return ()