Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/XML-XPath/Haskell/xml-xpath-1.hs
Normal file
25
Task/XML-XPath/Haskell/xml-xpath-1.hs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import Data.List
|
||||
import Control.Arrow
|
||||
import Control.Monad
|
||||
|
||||
takeWhileIncl :: (a -> Bool) -> [a] -> [a]
|
||||
takeWhileIncl _ [] = []
|
||||
takeWhileIncl p (x:xs)
|
||||
| p x = x : takeWhileIncl p xs
|
||||
| otherwise = [x]
|
||||
|
||||
getmultiLineItem n = takeWhileIncl(not.isInfixOf ("</" ++ n)). dropWhile(not.isInfixOf ('<': n))
|
||||
getsingleLineItems n = map (takeWhile(/='<'). drop 1. dropWhile(/='>')). filter (isInfixOf ('<': n))
|
||||
|
||||
main = do
|
||||
xml <- readFile "./Rosetta/xmlpath.xml"
|
||||
let xmlText = lines xml
|
||||
|
||||
putStrLn "\n== First item ==\n"
|
||||
mapM_ putStrLn $ head $ unfoldr (Just. liftM2 (id &&&) (\\) (getmultiLineItem "item")) xmlText
|
||||
|
||||
putStrLn "\n== Prices ==\n"
|
||||
mapM_ putStrLn $ getsingleLineItems "price" xmlText
|
||||
|
||||
putStrLn "\n== Names ==\n"
|
||||
print $ getsingleLineItems "name" xmlText
|
||||
18
Task/XML-XPath/Haskell/xml-xpath-2.hs
Normal file
18
Task/XML-XPath/Haskell/xml-xpath-2.hs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
{-# LANGUAGE Arrows #-}
|
||||
import Text.XML.HXT.Arrow
|
||||
{- For HXT version >= 9.0, use instead:
|
||||
import Text.XML.HXT.Core
|
||||
-}
|
||||
|
||||
deepElem name = deep (isElem >>> hasName name)
|
||||
|
||||
process = proc doc -> do
|
||||
item <- single (deepElem "item") -< doc
|
||||
_ <- listA (arrIO print <<< deepElem "price") -< doc
|
||||
names <- listA (deepElem "name") -< doc
|
||||
returnA -< (item, names)
|
||||
|
||||
main = do
|
||||
[(item, names)] <- runX (readDocument [] "xmlpath.xml" >>> process)
|
||||
print item
|
||||
print names
|
||||
Loading…
Add table
Add a link
Reference in a new issue