xmlgen-0.6.2.2: Fast XML generation library

Safe Haskell | None
Language | Haskell2010

Text.XML.Generator

Contents

- General
- Documents
- Namespaces
- Elements
- Attributes
- Text
- Other
- Rendering
- XHTML documents

Description

This module provides combinators for generating XML documents.

As an example, suppose you want to generate the following XML document:

    <?xml version="1.0"?>
    <people>
      <person age="32">Stefan</person>
      <person age="4">Judith</person>
    </people>

Then you could use the following Haskell code:

    let people = [("Stefan", "32"), ("Judith", "4")]
    in doc defaultDocInfo $
         xelem "people" $
           xelems $ map ((name, age) -> xelem "person" (xattr "age" age <#> xtext name)) people

Synopsis

- data Xml t
- data Doc
- data DocInfo = DocInfo {
  - docInfo_standalone :: Bool
  - docInfo_docType :: Maybe String
  - docInfo_preMisc :: Xml Doc
  - docInfo_postMisc :: Xml Doc
  }
- doc :: DocInfo -> Xml Elem -> Xml Doc
- defaultDocInfo :: DocInfo
- data Namespace
- type Prefix = Text
- type Uri = Text
- type Name = Text
- namespace :: Prefix -> Uri -> Namespace
- noNamespace :: Namespace
- defaultNamespace :: Namespace
- data Elem
- xelem :: AddChildren c => Name -> c -> Xml Elem
- xelemQ :: AddChildren c => Namespace -> Name -> c -> Xml Elem
- xelemEmpty :: Name -> Xml Elem
- xelemQEmpty :: Namespace -> Name -> Xml Elem
- class AddChildren c
- xelems :: [Xml Elem] -> Xml Elem
- noElems :: Xml Elem
- xelemWithText :: Name -> TextContent -> Xml Elem
- (<>) :: Semigroup a => a -> a -> a
- (<#>) :: a -> b -> (a, b)
- data Attr
- xattr :: Name -> TextContent -> Xml Attr
- xattrQ :: Namespace -> Name -> TextContent -> Xml Attr
- xattrQRaw :: Namespace -> Name -> Builder -> Xml Attr
- xattrs :: [Xml Attr] -> Xml Attr
- noAttrs :: Xml Attr
- type TextContent = Text
- xtext :: TextContent -> Xml Elem
- xtextRaw :: Builder -> Xml Elem
- xentityRef :: Name -> Xml Elem
- xempty :: Renderable t => Xml t
- class Renderable t => Misc t where
  - xprocessingInstruction :: String -> String -> Xml t
  - xcomment :: String -> Xml t
- xrender :: (Renderable r, XmlOutput t) => Xml r -> t
- class XmlOutput t where
  - fromBuilder :: Builder -> t
- class Renderable t
- xhtmlFramesetDocInfo :: DocInfo
- xhtmlStrictDocInfo :: DocInfo
- xhtmlTransitionalDocInfo :: DocInfo
- xhtmlRootElem :: Text -> Xml Elem -> Xml Elem

# General

data Xml t Source #

The type `Xml t` represent a piece of XML of type `t`, where `t` is usually one of `Elem`, `Attr`, or `Doc`.

#### Instances

Instances details

Monoid (Xml Attr) Source # |
Instance details Defined in Text.XML.Generator Methods mempty :: Xml Attr Source # mappend :: Xml Attr -> Xml Attr -> Xml Attr Source # mconcat :: [Xml Attr] -> Xml Attr Source #
Monoid (Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods mempty :: Xml Elem Source # mappend :: Xml Elem -> Xml Elem -> Xml Elem Source # mconcat :: [Xml Elem] -> Xml Elem Source #
Semigroup (Xml Attr) Source # |
Instance details Defined in Text.XML.Generator Methods (<>) :: Xml Attr -> Xml Attr -> Xml Attr Source # sconcat :: NonEmpty (Xml Attr) -> Xml Attr Source # stimes :: Integral b => b -> Xml Attr -> Xml Attr Source #
Semigroup (Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods (<>) :: Xml Elem -> Xml Elem -> Xml Elem Source # sconcat :: NonEmpty (Xml Elem) -> Xml Elem Source # stimes :: Integral b => b -> Xml Elem -> Xml Elem Source #
AddChildren (Xml Attr) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: Xml Attr -> NsEnv -> Builder
AddChildren (Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: Xml Elem -> NsEnv -> Builder
AddChildren (Xml Attr, Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: (Xml Attr, Xml Elem) -> NsEnv -> Builder
AddChildren (Xml Attr, [Xml Elem]) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: (Xml Attr, [Xml Elem]) -> NsEnv -> Builder

# Documents

data Doc Source #

A piece of XML at the document level.

#### Instances

Instances details

Misc Doc Source # |
Instance details Defined in Text.XML.Generator Methods xprocessingInstruction :: String -> String -> Xml Doc Source # xcomment :: String -> Xml Doc Source #
Renderable Doc Source # |
Instance details Defined in Text.XML.Generator Methods builder :: Doc -> Builder mkRenderable :: Builder -> Doc

data DocInfo Source #

The `DocInfo` type contains all information of an XML document except the root element.

Constructors

DocInfo |

Fields

- docInfo_standalone :: Bool

  Value of the `standalone` attribute in the `<?xml ... ?>` header

- docInfo_docType :: Maybe String

  Document type (N.B.: rendering does not escape this value)

- docInfo_preMisc :: Xml Doc

  Content before the root element

- docInfo_postMisc :: Xml Doc

  Content after the root element

doc :: DocInfo -> Xml Elem -> Xml Doc Source #

Constructs an XML document from a `DocInfo` value and the root element.

defaultDocInfo :: DocInfo Source #

The default document info (standalone, without document type, without content before/after the root element).

# Namespaces

data Namespace Source #

Type for representing presence or absence of an XML namespace.

#### Instances

Instances details

Show Namespace Source # |
Instance details Defined in Text.XML.Generator Methods showsPrec :: Int -> Namespace -> ShowS Source # show :: Namespace -> String Source # showList :: [Namespace] -> ShowS Source #
Eq Namespace Source # |
Instance details Defined in Text.XML.Generator Methods (==) :: Namespace -> Namespace -> Bool Source # (/=) :: Namespace -> Namespace -> Bool Source #

type Prefix = Text Source #

Namespace prefix.

type Uri = Text Source #

Namespace URI.

type Name = Text Source #

A type for names

namespace :: Prefix -> Uri -> Namespace Source #

Constructs a qualified XML namespace. The given URI must not be the empty string.

noNamespace :: Namespace Source #

A `Namespace` value denoting the absence of any XML namespace information.

defaultNamespace :: Namespace Source #

A `Namespace` value denoting the default namespace.

- For elements, this is the namespace currently mapped to the empty prefix.
- For attributes, the default namespace does not carry any namespace information.

# Elements

data Elem Source #

A piece of XML at the element level.

#### Instances

Instances details

Misc Elem Source # |
Instance details Defined in Text.XML.Generator Methods xprocessingInstruction :: String -> String -> Xml Elem Source # xcomment :: String -> Xml Elem Source #
Renderable Elem Source # |
Instance details Defined in Text.XML.Generator Methods builder :: Elem -> Builder mkRenderable :: Builder -> Elem
Monoid (Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods mempty :: Xml Elem Source # mappend :: Xml Elem -> Xml Elem -> Xml Elem Source # mconcat :: [Xml Elem] -> Xml Elem Source #
Semigroup (Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods (<>) :: Xml Elem -> Xml Elem -> Xml Elem Source # sconcat :: NonEmpty (Xml Elem) -> Xml Elem Source # stimes :: Integral b => b -> Xml Elem -> Xml Elem Source #
AddChildren (Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: Xml Elem -> NsEnv -> Builder
AddChildren (Xml Attr, Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: (Xml Attr, Xml Elem) -> NsEnv -> Builder
AddChildren (Xml Attr, [Xml Elem]) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: (Xml Attr, [Xml Elem]) -> NsEnv -> Builder

xelem :: AddChildren c => Name -> c -> Xml Elem Source #

Construct a simple-named element with the given children.

xelemQ :: AddChildren c => Namespace -> Name -> c -> Xml Elem Source #

Construct an element with the given children.

xelemEmpty :: Name -> Xml Elem Source #

Construct a simple-named element without any children.

xelemQEmpty :: Namespace -> Name -> Xml Elem Source #

Construct an element without any children.

class AddChildren c Source #

Class for adding children to an element.

The various instances of this class allow the addition of different kinds of children.

Minimal complete definition

addChildren

#### Instances

Instances details

AddChildren TextContent Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: TextContent -> NsEnv -> Builder
AddChildren String Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: String -> NsEnv -> Builder
AddChildren () Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: () -> NsEnv -> Builder
AddChildren (Xml Attr) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: Xml Attr -> NsEnv -> Builder
AddChildren (Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: Xml Elem -> NsEnv -> Builder
AddChildren (Xml Attr, Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: (Xml Attr, Xml Elem) -> NsEnv -> Builder
AddChildren (Xml Attr, [Xml Elem]) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: (Xml Attr, [Xml Elem]) -> NsEnv -> Builder

xelems :: [Xml Elem] -> Xml Elem Source #

Merges a list of elements into a single piece of XML at the element level.

noElems :: Xml Elem Source #

No elements at all.

xelemWithText :: Name -> TextContent -> Xml Elem Source #

The expression `xelemWithText n t` constructs an XML element with name `n` and text content `t`.

(<>) :: Semigroup a => a -> a -> a infixr 6 Source #

An associative operation.

#### Examples

Expand

    >>> [1,2,3] <> [4,5,6]
    [1,2,3,4,5,6]

    >>> Just [1, 2, 3] <> Just [4, 5, 6]
    Just [1,2,3,4,5,6]

    >>> putStr "Hello, " <> putStrLn "World!"
    Hello, World!

(<#>) :: a -> b -> (a, b) infixl 5 Source #

Shortcut for constructing pairs. Used in combination with `xelem` for separating child-attributes from child-elements.

# Attributes

data Attr Source #

A piece of XML at the attribute level.

#### Instances

Instances details

Renderable Attr Source # |
Instance details Defined in Text.XML.Generator Methods builder :: Attr -> Builder mkRenderable :: Builder -> Attr
Monoid (Xml Attr) Source # |
Instance details Defined in Text.XML.Generator Methods mempty :: Xml Attr Source # mappend :: Xml Attr -> Xml Attr -> Xml Attr Source # mconcat :: [Xml Attr] -> Xml Attr Source #
Semigroup (Xml Attr) Source # |
Instance details Defined in Text.XML.Generator Methods (<>) :: Xml Attr -> Xml Attr -> Xml Attr Source # sconcat :: NonEmpty (Xml Attr) -> Xml Attr Source # stimes :: Integral b => b -> Xml Attr -> Xml Attr Source #
AddChildren (Xml Attr) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: Xml Attr -> NsEnv -> Builder
AddChildren (Xml Attr, Xml Elem) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: (Xml Attr, Xml Elem) -> NsEnv -> Builder
AddChildren (Xml Attr, [Xml Elem]) Source # |
Instance details Defined in Text.XML.Generator Methods addChildren :: (Xml Attr, [Xml Elem]) -> NsEnv -> Builder

xattr :: Name -> TextContent -> Xml Attr Source #

Construct a simple-named attribute by escaping its value.

xattrQ :: Namespace -> Name -> TextContent -> Xml Attr Source #

Construct an attribute by escaping its value.

xattrQRaw :: Namespace -> Name -> Builder -> Xml Attr Source #

Construct an attribute without escaping its value. Note: attribute values are quoted with double quotes.

xattrs :: [Xml Attr] -> Xml Attr Source #

Merge a list of attributes into a single piece of XML at the attribute level.

noAttrs :: Xml Attr Source #

The empty attribute list.

# Text

type TextContent = Text Source #

Text content subject to escaping.

xtext :: TextContent -> Xml Elem Source #

Constructs a text node by escaping the given argument.

xtextRaw :: Builder -> Xml Elem Source #

Constructs a text node without escaping the given argument.

xentityRef :: Name -> Xml Elem Source #

Constructs a reference to the named entity. Note: no escaping is performed on the name of the entity

# Other

xempty :: Renderable t => Xml t Source #

An empty, polymorphic piece of XML.

class Renderable t => Misc t where Source #

Class providing methods for adding processing instructions and comments.

Minimal complete definition

Nothing

Methods

xprocessingInstruction :: String -> String -> Xml t Source #

Constructs a processing instruction with the given target and content. Note: Rendering does not perform escaping on the target and the content.

xcomment :: String -> Xml t Source #

Constructs an XML comment. Note: No escaping is performed on the text of the comment.

#### Instances

Instances details

Misc Doc Source # |
Instance details Defined in Text.XML.Generator Methods xprocessingInstruction :: String -> String -> Xml Doc Source # xcomment :: String -> Xml Doc Source #
Misc Elem Source # |
Instance details Defined in Text.XML.Generator Methods xprocessingInstruction :: String -> String -> Xml Elem Source # xcomment :: String -> Xml Elem Source #

# Rendering

xrender :: (Renderable r, XmlOutput t) => Xml r -> t Source #

Renders a given piece of XML.

class XmlOutput t where Source #

Instances of the `XmlOutput` class may serve as target of serializing an XML document.

Methods

fromBuilder :: Builder -> t Source #

Creates the target type from a `Builder`.

#### Instances

Instances details

XmlOutput Builder Source # |
Instance details Defined in Text.XML.Generator Methods fromBuilder :: Builder -> Builder Source #
XmlOutput ByteString Source # |
Instance details Defined in Text.XML.Generator Methods fromBuilder :: Builder -> ByteString Source #
XmlOutput ByteString Source # |
Instance details Defined in Text.XML.Generator Methods fromBuilder :: Builder -> ByteString Source #

class Renderable t Source #

Any type subject to rendering must implement this type class.

Minimal complete definition

builder, mkRenderable

#### Instances

Instances details

Renderable Attr Source # |
Instance details Defined in Text.XML.Generator Methods builder :: Attr -> Builder mkRenderable :: Builder -> Attr
Renderable Doc Source # |
Instance details Defined in Text.XML.Generator Methods builder :: Doc -> Builder mkRenderable :: Builder -> Doc
Renderable Elem Source # |
Instance details Defined in Text.XML.Generator Methods builder :: Elem -> Builder mkRenderable :: Builder -> Elem

# XHTML documents

xhtmlFramesetDocInfo :: DocInfo Source #

Document info for XHTML 1.0 frameset.

xhtmlStrictDocInfo :: DocInfo Source #

Document info for XHTML 1.0 strict.

xhtmlTransitionalDocInfo :: DocInfo Source #

Document info for XHTML 1.0 transitional.

xhtmlRootElem :: Text -> Xml Elem -> Xml Elem Source #

Constructs the root element of an XHTML document.