It is much easier to answer these questions if a minimal working example is provided (for example, the complete RDF dataset that we can query). For example, in the above, since we do not know the base of the XML document, we cannot know whether the individual described by <Group rdf:ID="group_actinoid">...</Group> will actually match the template ?group rdf:type pt:Group .
Here, some data is based on yours, but which contains another group, so that we can see the grouping and aggregation:
@prefix pt: <http://www.daml.org/2003/01/periodictable/PeriodicTable#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>. @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. @prefix xsd: <http://www.w3.org/2001/XMLSchema#>. pt:actinoid a pt:Group ; pt:name "Actinoid" ; pt:element pt:Ac ; pt:element pt:Th ; pt:element pt:Pa ; pt:element pt:U ; pt:element pt:Np ; pt:element pt:Pu ; pt:element pt:Am ; pt:element pt:Cm ; pt:element pt:Bk ; pt:element pt:Cf ; pt:element pt:Es ; pt:element pt:Fm ; pt:element pt:Md ; pt:element pt:No . pt:beatles a pt:Group ; pt:name "Beatles" ; pt:element pt:John ; pt:element pt:Paul ; pt:element pt:George ; pt:element pt:Ringo .
Here's the SPARQL query, which is very similar to yours (although I used some of the shorter forms where possible) and adjusted the change of ?element pt:element ?group to ?group pt:element ?element . Using this SPARQL query, we get results that you think look the way you are looking.
PREFIX pt:<http://www.daml.org/2003/01/periodictable/PeriodicTable#> PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd:<http://www.w3.org/2001/XMLSchema
Calling data groups.n3 and query groups.sparql , here are the results obtained on the Apache Jena ARQ command line:
$ /usr/local/lib/apache-jena-2.10.0/bin/arq --data groups.n3 --query groups.sparql -------------------------- | NAME | NELEMENTS | ========================== | "Beatles" | 4 | | "Actinoid" | 14 | --------------------------
When I run the same query according to http://www.daml.org/2003/01/periodictable/PeriodicTable.owl (after loading and saving as PeriodicTable.owl ), I get the names and numbers shown in the following:
$ /usr/local/lib/apache-jena-2.10.0/bin/arq \ --data ~/Downloads/PeriodicTable.owl \ --query groups.sparql -------------------------------------------------- | NAME | NELEMENTS | ================================================== | "Lanthanoid"^^xsd:string | 14 | | "Noble gas"^^xsd:string | 7 | | "Halogen"^^xsd:string | 6 | | "Actinoid"^^xsd:string | 14 | | "Chalcogen"^^xsd:string | 6 | | "Pnictogen"^^xsd:string | 6 | | "Coinage metal"^^xsd:string | 4 | | "Alkali metal"^^xsd:string | 7 | | "Alkaline earth metal"^^xsd:string | 6 | --------------------------------------------------