2

My question is about SPARQL query language of RDF triples, suppose we have a family ontology written in RDF/XML format.

Now, I want to query all parents, for example, with at least two children (cardinality on hasChild relation) with SPARQL.

My question is, is it possible to write this query in SPARQL language, however I know this is possible to write this query in DL query language (Description Logic)

In more general form, Is it possible to apply some cardinality restrictions in SPARQL language?

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • "a family ontology written in RDF/XML format" Do you mean an OWL ontology? An RDFS "ontology"? RDF/XML is just one *serialization* format for RDF, but a number of different ontology languages can be encoded in RDF. – Joshua Taylor Jan 30 '15 at 17:41

1 Answers1

6

Now, I want to query all parents, for example, with at least two children (cardinality on hasChild relation) with SPARQL.

You just select a parent and child in each row, then group by the parent, and then only take those that have at least two values for child:

select ?parent where {
  ?parent :hasChild ?child
}
group by ?parent
having (count(distinct ?child) >= 2)

Beware though; In OWL you can have an individual that must have at least two children, but that this query wouldn't return. E.g., if you have

TwoChildParent subClassof (hasChild min 2)
Joe a TwoChildParent

but don't have any

Joe hasChild ?x

triples, this query won't return Joe, even though Joe has at least two children.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Thanks Joshua! my ontology is of type OWL and your query worked perfect – frogatto Jan 30 '15 at 19:11
  • @abforce Beware though; In OWL you can have an individual that must have at least two children, but that this query wouldn't return. E.g., if you have `TwoChildParent subClassof (hasChild min 2)` and `Joe a TwoParentChild`, but don't have any `Joe hasChild x` triples, this query won't return Joe, even though Joe has at least two children. – Joshua Taylor Jan 30 '15 at 19:43
  • I might misunderstand, but shouldn't it be `Joe a TwoChildParent` instead of `Joe a TwoParentChild`? – Barry NL Jun 22 '20 at 13:32
  • @BarryNL Good catch! Fixed now. – Joshua Taylor Jun 22 '20 at 20:54