module Text.XML.Light.Proc where
import Text.XML.Light.Types
import Data.Maybe(listToMaybe)
import Data.List(find)
strContent :: Element -> String
strContent e = concatMap cdData $ onlyText $ elContent e
onlyElems :: [Content] -> [Element]
onlyElems xs = [ x | Elem x <- xs ]
elChildren :: Element -> [Element]
elChildren e = [ x | Elem x <- elContent e ]
onlyText :: [Content] -> [CData]
onlyText xs = [ x | Text x <- xs ]
findChildren :: QName -> Element -> [Element]
findChildren q e = filterChildren ((q ==) . elName) e
filterChildren :: (Element -> Bool) -> Element -> [Element]
filterChildren p e = filter p (onlyElems (elContent e))
filterChildrenName :: (QName -> Bool) -> Element -> [Element]
filterChildrenName p e = filter (p.elName) (onlyElems (elContent e))
findChild :: QName -> Element -> Maybe Element
findChild q e = listToMaybe (findChildren q e)
filterChild :: (Element -> Bool) -> Element -> Maybe Element
filterChild p e = listToMaybe (filterChildren p e)
filterChildName :: (QName -> Bool) -> Element -> Maybe Element
filterChildName p e = listToMaybe (filterChildrenName p e)
findElement :: QName -> Element -> Maybe Element
findElement q e = listToMaybe (findElements q e)
filterElement :: (Element -> Bool) -> Element -> Maybe Element
filterElement p e = listToMaybe (filterElements p e)
filterElementName :: (QName -> Bool) -> Element -> Maybe Element
filterElementName p e = listToMaybe (filterElementsName p e)
findElements :: QName -> Element -> [Element]
findElements qn e = filterElementsName (qn==) e
filterElements :: (Element -> Bool) -> Element -> [Element]
filterElements p e
| p e = [e]
| otherwise = concatMap (filterElements p) $ onlyElems $ elContent e
filterElementsName :: (QName -> Bool) -> Element -> [Element]
filterElementsName p e = filterElements (p.elName) e
findAttr :: QName -> Element -> Maybe String
findAttr x e = lookupAttr x (elAttribs e)
lookupAttr :: QName -> [Attr] -> Maybe String
lookupAttr x = lookupAttrBy (x ==)
lookupAttrBy :: (QName -> Bool) -> [Attr] -> Maybe String
lookupAttrBy p as = attrVal `fmap` find (p . attrKey) as
findAttrBy :: (QName -> Bool) -> Element -> Maybe String
findAttrBy p e = lookupAttrBy p (elAttribs e)