Omar Rana
Omar Rana

Reputation: 75

Sparql query for RDF and mutilple attribute values

hi i want to write a query for :

<car name="BMW">

<attribute =speed="">

<value>1.5</value>

</attribute>

<attribute =weight="">

<value>30</value>

</attribute>

</car>

i want the output in the format : BMW has speed 1.5 and weight 30. i have converted the above into RDF but i cant get my desired results. any help? hi my actual data is from XML file , and i converted into RDF:

i created this ontology:

    @prefix veh:     <http://www.example.com#> .
    @prefix schema:  <http://schema.org/>

    veh:Car
          rdf:type owl:Class ;
          rdfs:label "Car"@en .
  veh:Attribute
      rdf:type owl:Class ;
      rdfs:comment "Attributes that belongs to Car"@en ;
      rdfs:label "Attribute"@en .

veh:hasAttribute
          rdf:type owl:ObjectProperty ;
          rdfs:domain aml:Car ;
          rdfs:label "has Attribute"@en ;
          rdfs:range veh:Attribute .

    veh:hasValue
          rdf:type owl:DatatypeProperty ;
          rdfs:domain veh:Attribute ;
          rdfs:label "has Value"@en ;
          rdfs:range xsd:string .

So generated my RDF graph based on above mapping and got the following.:

veh:Car
  schema:name "BMW" ;
  veh:hasAttribute veh:Attribute ;
  a veh:Car .

veh:Attribute
  schema:name "speed","weight" ;
  a veh:Attribute ;
  veh:hasValue "1.5", "35".

like what should be the query? my query is:

select  distinct ?name ?value

from <http://localhost.com/worksheets/Phd_Example>

where {
?x a veh:Car.
?x veh:hasAttribute ?z.
?z veh:hasValue ?value.
?z schema:name ?name.
}   

but the output is not good:

i get

speed 1.5
weight 1.5
speed 30
wieght 30

why am i getting duplicate values?

i want the result to be:

speed 1.5

weight 30

Upvotes: 1

Views: 1493

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85813

It's always better if you can provide actual data that we can work with. The data you've provided doesn't have all the prefixes declared, and I assume that the use of aml:Vehicle is a typo and should be veh:Vehicle. Here's data with prefixes. I've also noted something rather strange in your data: you're using the same IRI for an individual and for a class. That won't cause an issue with the SPARQL query (since SPARQL just cares about the RDf triples), but it's rather unusual for an OWL ontology, though I think that kind of reuse (called punning) might be legal in this case. Here's the data:

@prefix veh:     <http://www.example.com#> .
@prefix schema:  <http://schema.org/>
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
@prefix owl: <http://www.w3.org/2002/07/owl#>
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>

# Classes and Properties
veh:Car
  rdf:type owl:Class ;
  rdfs:label "Car"@en .

veh:Attribute
  rdf:type owl:Class ;
  rdfs:comment "Attributes that belongs to Car"@en ;
  rdfs:label "Attribute"@en .

veh:hasAttribute
  rdf:type owl:ObjectProperty ;
  rdfs:domain veh:Car ;
  rdfs:label "has Attribute"@en ;
  rdfs:range veh:Attribute .

veh:hasValue
  rdf:type owl:DatatypeProperty ;
  rdfs:domain veh:Attribute ;
  rdfs:label "has Value"@en ;
  rdfs:range xsd:string .

# Individuals 
veh:Car                # same name as class, unusual
  schema:name "BMW" ;
  veh:hasAttribute veh:Attribute ;
  a veh:Car .

veh:Attribute          # same name as class, unusual
  schema:name "speed","weight" ;
  a veh:Attribute ;
  veh:hasValue "1.5", "35".

Now, the SPARQL query will be just about the same as the structure of the data. You noted that you're getting "duplicate" results. Let's take a look at them.

prefix veh:     <http://www.example.com#>
prefix schema:  <http://schema.org/>

select distinct ?name ?value {
  ?x a veh:Car ;
     veh:hasAttribute ?z .
  ?z veh:hasValue ?value ;
     schema:name ?name .
}
--------------------
| name     | value |
====================
| "weight" | "35"  |
| "speed"  | "35"  |
| "weight" | "1.5" |
| "speed"  | "1.5" |
--------------------

You're not getting "duplicates" so much as you're getting a Cartesian product of the possible values. You've declared that there's a car, and that the car has an attribute. Then you said that the attribute has two names ("weight" and "speed"), and that the attribute has two values ("35" and "1.5"). Then you have a query that says:

        Find a car with an attribute with a name and a value.

There are two names that you can pick, and two values that you can pick, there are four ways to satisfy that query. That's why you're getting fourdistinct results; there aren't any "duplicates". It sounds like you actually want the car to have two attributes, each with a single name and value. So your updated data would look like this:

# Individuals 
veh:Car                # same name as class, unusual
  schema:name "BMW" ;
  veh:hasAttribute veh:Attribute1, veh:Attribute2 ;
  a veh:Car .

veh:Attribute1        
  schema:name "speed" ;
  a veh:Attribute ;
  veh:hasValue "1.5" .

veh:Attribute2        
  schema:name "weight" ;
  a veh:Attribute ;
  veh:hasValue "35" .

The query can stay the same, and you'll get these results:

--------------------
| name     | value |
====================
| "weight" | "35"  |
| "speed"  | "1.5" |
--------------------

Upvotes: 3

Related Questions