Skip to content

Instantly share code, notes, and snippets.

@jbpotonnier
Created February 24, 2012 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbpotonnier/1901205 to your computer and use it in GitHub Desktop.
Save jbpotonnier/1901205 to your computer and use it in GitHub Desktop.
Transform XML document using Haskell and XPath

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.

Input document :

<?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>

Output document :

<?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>

Haskell code:

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 ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment