2.6. Access functions

Besides filters and combinators there exist some access functions for processing the attribute list TagAttrl of nodes.

Functions for processing the attribute list

getAttrValue :: Eq a => a -> [(a,b)] -> Maybe b

Looks up the value for a certain attribute. If the attribute does not exist, the data type Nothing is returned.

getAttrValue1 :: Eq a => a -> [(a, String)] -> String

Looks up the value for a certain attribute. If the attribute does not exist, an empty string is returned.

hasAttrValue :: Eq a => a -> [(a,b)] -> Bool

Returns true if there is an attribute with the searched name in the attribute list.

addAttrValue :: Eq a => a -> b -> [(a,b)] -> [(a,b)]

Adds an attribute, the name and value, to the list.

addAttrValues :: Eq a => [(a,b)] -> [(a,b)] -> [(a,b)]

Adds a list of name-value pairs to the attribute list.

changeAttrValue :: Eq a => a -> b -> [(a,b)] -> [(a,b)]

Changes the value of a certain attribute.

delAttrValue :: Eq a => a -> [(a,b)] -> [(a,b)]

Deletes an attribute from the list.

delAttrValues :: Eq a => [a] -> [(a,b)] -> [(a,b)]

Deletes a list of attributes from the attribute list.

The following example shows the use of access functions for adding a new attribute with the name "att2" and the value "val2" to the attribute list of an XTag node. The type of the node is detected by pattern-matching. If the node is of another type, it is simply returned.

Example 2-4. Adding a new attribute to an XTag node.


addAttribute :: XmlFilter
addAttribute n@(NTree (XTag name al) cs)
    = replaceAttrl newAttList n
      where
      newAttList = addAttrValue "att2" "val2" al

addAttribute n = [n]