xml-1.3.14: A simple XML library.

Copyright | (c) Galois Inc. 2008
License | BSD3
Maintainer | Iavor S. Diatchki <diatchki@galois.com>
Stability | provisional
Portability | portable
Safe Haskell | Safe
Language | Haskell98

Text.XML.Light.Cursor

Contents

- Conversions
- Moving around
  - Searching
- Node classification
- Updates
  - Inserting content
  - Removing content

Description

XML cursors for working XML content withing the context of an XML document. This implementation is based on the general tree zipper written by Krasimir Angelov and Iavor S. Diatchki.

Synopsis

- data Tag = Tag {
  - tagName :: QName
  - tagAttribs :: [Attr]
  - tagLine :: Maybe Line
  }
- getTag :: Element -> Tag
- setTag :: Tag -> Element -> Element
- fromTag :: Tag -> [Content] -> Element
- data Cursor = Cur {
  - current :: Content
  - lefts :: [Content]
  - rights :: [Content]
  - parents :: Path
  }
- type Path = [([Content], Tag, [Content])]
- fromContent :: Content -> Cursor
- fromElement :: Element -> Cursor
- fromForest :: [Content] -> Maybe Cursor
- toForest :: Cursor -> [Content]
- toTree :: Cursor -> Content
- parent :: Cursor -> Maybe Cursor
- root :: Cursor -> Cursor
- getChild :: Int -> Cursor -> Maybe Cursor
- firstChild :: Cursor -> Maybe Cursor
- lastChild :: Cursor -> Maybe Cursor
- left :: Cursor -> Maybe Cursor
- right :: Cursor -> Maybe Cursor
- nextDF :: Cursor -> Maybe Cursor
- findChild :: (Cursor -> Bool) -> Cursor -> Maybe Cursor
- findLeft :: (Cursor -> Bool) -> Cursor -> Maybe Cursor
- findRight :: (Cursor -> Bool) -> Cursor -> Maybe Cursor
- findRec :: (Cursor -> Bool) -> Cursor -> Maybe Cursor
- isRoot :: Cursor -> Bool
- isFirst :: Cursor -> Bool
- isLast :: Cursor -> Bool
- isLeaf :: Cursor -> Bool
- isChild :: Cursor -> Bool
- hasChildren :: Cursor -> Bool
- getNodeIndex :: Cursor -> Int
- setContent :: Content -> Cursor -> Cursor
- modifyContent :: (Content -> Content) -> Cursor -> Cursor
- modifyContentM :: Monad m => (Content -> m Content) -> Cursor -> m Cursor
- insertLeft :: Content -> Cursor -> Cursor
- insertRight :: Content -> Cursor -> Cursor
- insertGoLeft :: Content -> Cursor -> Cursor
- insertGoRight :: Content -> Cursor -> Cursor
- removeLeft :: Cursor -> Maybe (Content, Cursor)
- removeRight :: Cursor -> Maybe (Content, Cursor)
- removeGoLeft :: Cursor -> Maybe Cursor
- removeGoRight :: Cursor -> Maybe Cursor
- removeGoUp :: Cursor -> Maybe Cursor

# Documentation

data Tag Source #

Constructors

Tag |

Fields

- tagName :: QName
- tagAttribs :: [Attr]
- tagLine :: Maybe Line

#### Instances

Instances details

Show Tag Source # |
Instance details Defined in Text.XML.Light.Cursor Methods showsPrec :: Int -> Tag -> ShowS show :: Tag -> String showList :: [Tag] -> ShowS

getTag :: Element -> Tag Source #

setTag :: Tag -> Element -> Element Source #

fromTag :: Tag -> [Content] -> Element Source #

data Cursor Source #

The position of a piece of content in an XML document.

Constructors

Cur |

Fields

- current :: Content

  The currently selected content.

- lefts :: [Content]

  Siblings on the left, closest first.

- rights :: [Content]

  Siblings on the right, closest first.

- parents :: Path

  The contexts of the parent elements of this location.

#### Instances

Instances details

Show Cursor Source # |
Instance details Defined in Text.XML.Light.Cursor Methods showsPrec :: Int -> Cursor -> ShowS show :: Cursor -> String showList :: [Cursor] -> ShowS

type Path = [([Content], Tag, [Content])] Source #

# Conversions

fromContent :: Content -> Cursor Source #

A cursor for the given content.

fromElement :: Element -> Cursor Source #

A cursor for the given element.

fromForest :: [Content] -> Maybe Cursor Source #

The location of the first tree in a forest.

toForest :: Cursor -> [Content] Source #

Computes the forest containing this location.

toTree :: Cursor -> Content Source #

Computes the tree containing this location.

# Moving around

parent :: Cursor -> Maybe Cursor Source #

The parent of the given location.

root :: Cursor -> Cursor Source #

The top-most parent of the given location.

getChild :: Int -> Cursor -> Maybe Cursor Source #

The child with the given index (starting from 0).

firstChild :: Cursor -> Maybe Cursor Source #

The first child of the given location.

lastChild :: Cursor -> Maybe Cursor Source #

The last child of the given location.

left :: Cursor -> Maybe Cursor Source #

The left sibling of the given location.

right :: Cursor -> Maybe Cursor Source #

The right sibling of the given location.

nextDF :: Cursor -> Maybe Cursor Source #

The next position in a left-to-right depth-first traversal of a document: either the first child, right sibling, or the right sibling of a parent that has one.

## Searching

findChild :: (Cursor -> Bool) -> Cursor -> Maybe Cursor Source #

The first child that satisfies a predicate.

findLeft :: (Cursor -> Bool) -> Cursor -> Maybe Cursor Source #

Find the next left sibling that satisfies a predicate.

findRight :: (Cursor -> Bool) -> Cursor -> Maybe Cursor Source #

Find the next right sibling that satisfies a predicate.

findRec :: (Cursor -> Bool) -> Cursor -> Maybe Cursor Source #

Perform a depth first search for a descendant that satisfies the given predicate.

# Node classification

isRoot :: Cursor -> Bool Source #

Are we at the top of the document?

isFirst :: Cursor -> Bool Source #

Are we at the left end of the the document?

isLast :: Cursor -> Bool Source #

Are we at the right end of the document?

isLeaf :: Cursor -> Bool Source #

Are we at the bottom of the document?

isChild :: Cursor -> Bool Source #

Do we have a parent?

hasChildren :: Cursor -> Bool Source #

Do we have children?

getNodeIndex :: Cursor -> Int Source #

Get the node index inside the sequence of children

# Updates

setContent :: Content -> Cursor -> Cursor Source #

Change the current content.

modifyContent :: (Content -> Content) -> Cursor -> Cursor Source #

Modify the current content.

modifyContentM :: Monad m => (Content -> m Content) -> Cursor -> m Cursor Source #

Modify the current content, allowing for an effect.

## Inserting content

insertLeft :: Content -> Cursor -> Cursor Source #

Insert content to the left of the current position.

insertRight :: Content -> Cursor -> Cursor Source #

Insert content to the right of the current position.

insertGoLeft :: Content -> Cursor -> Cursor Source #

Insert content to the left of the current position. The new content becomes the current position.

insertGoRight :: Content -> Cursor -> Cursor Source #

Insert content to the right of the current position. The new content becomes the current position.

## Removing content

removeLeft :: Cursor -> Maybe (Content, Cursor) Source #

Remove the content on the left of the current position, if any.

removeRight :: Cursor -> Maybe (Content, Cursor) Source #

Remove the content on the right of the current position, if any.

removeGoLeft :: Cursor -> Maybe Cursor Source #

Remove the current element. The new position is the one on the left.

removeGoRight :: Cursor -> Maybe Cursor Source #

Remove the current element. The new position is the one on the right.

removeGoUp :: Cursor -> Maybe Cursor Source #

Remove the current element. The new position is the parent of the old position.