MyNixOS website logo
Description

Extension to the xml package to extract data from parsed xml.

This library provides functions to simplify extraction of data from generic xml tree structures (as produced by parsing xml with the xml package), while handling location information to provide decent error messages in case of errors.

Some limitations

  • Only handles unqualified names. (This is by design to simplify usage.)

  • No column number and sometimes no line number reference in error values.

xml-extractors

This is an extension to the xml package, providing functions to extract data from parsed xml.

Motivation

The xml package provides functions to parse and get information from xml data. It will parse an xml string into a generic tree representation. Extracting information from such a tree to some custom data structure while keeping track of location to handle errors is tricky. This library helps with that.

If there is an error during extraction (expected information is absent or wrong), it will return an error value with position information. The idea is to provide decent error messages.

Example usage

Suppose you have an xml file of books like this:

<?xml version="1.0"?>
<library>
  <book id="1" isbn="23234-1">
    <author>John Doe</author>
    <title>Some book</title>
  </book>
  <book id="2">
    <author>You</author>
    <title>The Great Event</title>
  </book>
  ...
</library>

and a Haskell data type to represent a book:

data Book = Book { bookId        :: Int
                 , isbn          :: Maybe String
                 , author, title :: String
                 }

You can parse the xml file into a generic tree structure using parseXMLDoc from the xml package. Then to transform this generic xml tree into Book objects you define extractors for books, like so:

book = element "book" $ do
         i <- attribAs "id" integer
         s <- optional (attrib "isbn")
         children $ do
           a <- element "author" $ contents $ text
           t <- element "title" $ contents $ text
           return Book { bookId = i, author = a, title = t, isbn = s }

library = element "library" $ children $ only $ many book

extractLibrary :: Element -> Either ExtractionErr [Book]
extractLibrary = extractDocContents library
Metadata

Version

0.4.0.3

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows