Search code examples
haskellsplithxt

split a word using HXT


I would like to know, how am i able to split a word with HXT ? For example :

I have that ->

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
import Text.XML.HXT.Core
import System.Environment  --para uso do getArgs

data Class = Class { name ::String }
    deriving (Show,Eq)

main = do
   [src]<- getArgs
   teams <- runX(readDocument [ withValidate no] src  >>> getClass)
   print teams

atTag tag = deep (isElem >>> hasName tag)
getClass = atTag "owl:Class" >>>
    proc l -> do
    className <- getAttrValue "rdf:about" -< l
    returnA -< Class { name = className }

And i want to split the word ClassName ! Because the result of that programs (teams), gives me a set of hyperlink website (http:// ......) ! (Due to the XML file ) Can anyone give me some hints to solve it, please ? Thank you !


Solution

  • You can use the function splitOn of the package split:

    {-# LANGUAGE Arrows, NoMonomorphismRestriction #-}
    import Text.XML.HXT.Core
    import Data.List.Split (splitOn)
    
    ...
    
    getClass = atTag "owl:Class" >>>
        proc l -> do
        className <- getAttrValue "rdf:about" -< l
        returnA -< Class { name = splitOn "#" className !! 1 }
    

    Example in ghci:

    > import Data.List.Split
    > let className = "http://www.xfront.com/owl/ontologies/camera/#Window"
    > splitOn "#" className !! 1
    Loading package split-0.2.2 ... linking ... done.
    "Window"
    

    The above code just works, if there is just one "#" in all of your URLs. If they are more complex, you shall have a look an the package Parsec.