~singpolyma/xml-types-haskell

b2eb7497ee3f16cf8b76d4c0bc41f466b95b46a3 — John Millikin 11 years ago eedf856
Add utility functions for getting node and attribute text.
1 files changed, 21 insertions(+), 0 deletions(-)

M Data/XML/Types.hs
M Data/XML/Types.hs => Data/XML/Types.hs +21 -0
@@ 56,14 56,18 @@ module Data.XML.Types
	-- ** Element traversal
	, elementChildren
	, elementContent
	, elementText
	
	-- ** Node traversal
	, nodeChildren
	, nodeContent
	, nodeText
	
	-- ** Attributes
	, hasAttribute
	, hasAttributeText
	, attributeContent
	, attributeText
	) where
import Control.Monad ((>=>))
import qualified Data.Map as M


@@ 207,14 211,31 @@ elementChildren = elementNodes >=> isElement
elementContent :: Element -> [Content]
elementContent = elementNodes >=> isContent

elementText :: Element -> Text
elementText = contentText . elementContent

nodeChildren :: Node -> [Node]
nodeChildren = isElement >=> elementNodes

nodeContent :: Node -> [Content]
nodeContent = nodeChildren >=> isContent

nodeText :: Node -> Text
nodeText = contentText . nodeContent

hasAttribute :: Name -> Element -> [Element]
hasAttribute name e = [e | isJust (attributeContent name e)]

hasAttributeText :: Name -> (Text -> Bool) -> Element -> [Element]
hasAttributeText name p e = [e | maybe False p (attributeText name e)]

attributeContent :: Name -> Element -> Maybe [Content]
attributeContent name e = M.lookup name (elementAttributes e)

attributeText :: Name -> Element -> Maybe Text
attributeText name e = fmap contentText (attributeContent name e)

contentText :: [Content] -> Text
contentText = T.concat . map step where
	step (ContentText t) = t
	step (ContentEntity entity) = T.concat [T.pack "&", entity, T.pack ";"]