Skip to content
Browse files

Added reading of predicate when omitting children nodes, ex 14 working

Ignore-this: 1e40362d05a82f6735cb1cea1853821b

darcs-hash:20110202162845-1786f-f2f24cff9cb33aa4dbd17f4e2a8e86ddc4d486be.gz
  • Loading branch information...
1 parent 903af92 commit 553534d236f1f2331355d335419561e20cfcd44c @amccausl committed Feb 2, 2011
Showing with 10 additions and 2 deletions.
  1. +10 −2 src/Text/RDF/RDF4H/XmlParser.hs
View
12 src/Text/RDF/RDF4H/XmlParser.hs
@@ -81,21 +81,29 @@ parseDescription = (updateState
replaceLiElems acc n (Triple s p o : rest) = replaceLiElems (Triple s p o : acc) n rest
replaceLiElems acc _ [] = acc
+-- |Parse the current predicate element as a rdf:Description element (used when rdf:parseType = "Resource")
+parseAsResource :: forall a. (ArrowXml a, ArrowState GParseState a) => Node -> a (LParseState, XmlTree) Triple
+parseAsResource n = (updateState
+ >>> (arr2A parsePredicatesFromAttr
+ <+> (second getName >>> arr (\(s, p) -> Triple (stateSubject s) ((unode . s2b) p) n))
+ <+> (arr (\s -> s { stateSubject = n }) *** (getChildren >>> isElem) >>> parsePredicatesFromChildren))) -- If the rdf:Description element has another name, that is it's type
+
-- |Read the attributes of an rdf:Description element. These correspond to the Predicate Object pairs of the Triple
parsePredicatesFromAttr :: forall a. (ArrowXml a, ArrowState GParseState a) => LParseState -> a XmlTree Triple
parsePredicatesFromAttr s = getAttrl >>> ((getName
>>> isA (/= "rdf:about")
>>> isA (/= "rdf:nodeID")
>>> isA (/= "rdf:ID")
>>> isA (/= "xml:lang")
+ >>> isA (/= "rdf:parseType")
>>> (arr (unode . s2b)))
&&& (getChildren >>> getText >>> arr (lnode . plainL . s2b))) >>> arr (attachSubject (stateSubject s))
-- |Read a children of an rdf:Description element. These correspond to the Predicate portion of the Triple
parsePredicatesFromChildren :: forall a. (ArrowXml a, ArrowState GParseState a) => a (LParseState, XmlTree) Triple
parsePredicatesFromChildren = updateState
>>> choiceA [ second (hasAttrValue "rdf:parseType" (== "Literal")) :-> arr2A parseAsLiteralTriple
- , second (hasAttrValue "rdf:parseType" (== "Resource")) :-> (defaultA <+> parseDescription)
+ , second (hasAttrValue "rdf:parseType" (== "Resource")) :-> (defaultA <+> (mkBlankNode &&& arr id >>> arr2A parseAsResource))
, second (hasAttrValue "rdf:parseType" (== "Collection")) :-> (listA (defaultA >>> arr id &&& mkBlankNode) >>> mkCollectionTriples >>> unlistA)
, second (hasAttr "rdf:datatype") :-> arr2A getTypedTriple
, second (hasAttr "rdf:resource") :-> arr2A getResourceTriple
@@ -151,7 +159,7 @@ mkCollectionTriples :: forall a. (ArrowXml a, ArrowState GParseState a) => a [(T
mkCollectionTriples = arr (mkCollectionTriples' [])
where mkCollectionTriples' [] ((Triple s1 p1 o1, n1):rest) = mkCollectionTriples' [Triple s1 p1 n1] ((Triple s1 p1 o1, n1):rest)
mkCollectionTriples' acc ((Triple _ _ o1, n1):(t2, n2):rest) = mkCollectionTriples' (Triple n1 headNode o1 : Triple n1 tailNode n2 : acc) ((t2, n2):rest)
- mkCollectionTriples' acc ((Triple _ _ o1, n1):[]) = Triple n1 headNode o1 : Triple n1 tailNode nilNode : acc
+ mkCollectionTriples' acc [(Triple _ _ o1, n1)] = Triple n1 headNode o1 : Triple n1 tailNode nilNode : acc
mkCollectionTriples' _ [] = []
headNode = (unode . s2b) "rdf:first"
tailNode = (unode . s2b) "rdf:rest"

0 comments on commit 553534d

Please sign in to comment.
Something went wrong with that request. Please try again.