Sparql查询RDF和mutilple属性值

时间:2016-03-13 18:46:03

标签: sparql

您好我想写一个查询:

<car name="BMW">

<attribute =speed="">

<value>1.5</value>

</attribute>

<attribute =weight="">

<value>30</value>

</attribute>

</car>

我希望输出格式为:宝马的速度为1.5,重量为30.我已将上述内容转换为RDF,但我无法得到我想要的结果。任何帮助? 我的实际数据来自XML文件,我转换为RDF:

我创建了这个本体:

    @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 .

因此基于上面的映射生成了我的RDF图并获得了以下内容:

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".

喜欢什么应该是查询? 我的疑问是:

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.
}   

但输出效果不佳:

我得到了

speed 1.5
weight 1.5
speed 30
wieght 30

为什么我会得到重复值?

我希望结果为:

speed 1.5

weight 30

1 个答案:

答案 0 :(得分:3)

如果您能提供我们可以使用的实际数据,那总是更好。您提供的数据没有声明所有前缀,我认为使用 aml:Vehicle 是一个错字,应该是 veh:Vehicle 。这是带前缀的数据。我还注意到你的数据中有些奇怪的东西:你为一个人和一个班级使用相同的IRI。这不会导致SPARQL查询出现问题(因为SPARQL只关心RDf三元组),但对于OWL本体来说这是相当不寻常的,尽管我认为在这种情况下这种重用(称为双关语)可能是合法的。这是数据:

@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".

现在,SPARQL查询与数据结构大致相同。您注意到您获得了“重复”结果。我们来看看它们。

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" |
--------------------

你没有得到“重复”,因为你得到了可能值的笛卡尔积。你已经宣布有一辆车,而且这辆车有一个属性。然后你说该属性有两个名称(“权重”和“速度”),并且该属性有两个值(“35”和“1.5”)。然后你有一个查询说:

查找具有名称和值的属性的汽车。

您可以选择两个名称,可以选择两个值,四种方法可以满足该查询。这就是为什么你得到四个不同的结果;没有任何“重复”。听起来你真的希望汽车拥有两个属性,每个属性都有一个名称和值。因此,您更新的数据如下所示:

# 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" .

查询可以保持不变,您将获得以下结果:

--------------------
| name     | value |
====================
| "weight" | "35"  |
| "speed"  | "1.5" |
--------------------
相关问题