如何克服由datatype属性引起的重复结果

时间:2013-11-06 12:26:41

标签: sparql

我使用java WS来查询存储库。我无法更改此存储库中的数据。 当我查询任何内容时,如果任何变量具有“datatype”属性,我会得到重复的结果。

有没有办法只使用查询删除重复项?

感谢您的帮助。问题的例子如下,

我的固定名称空间

public static final String PREFIX = "prefix kb: <http://protege.stanford.edu/kb#>\n" +      
                                    "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
                                    "prefix owl: <http://www.w3.org/2002/07/owl#>\n" +
                                    "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +         
                                    "prefix xsd: <http://www.w3.org/2001/XMLSchema#>\n";

我的查询字符串

String query = "SELECT DISTINCT ?name WHERE {" +
            "?unit a kb:Unit ; " +
            "kb:hasName ?name ;" +
            "} ORDER BY ?name ";

当我使用此查询时,我得到带有“datatype”的重复值,例如

    <?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
  <head>
    <variable name="name"/>
  </head>
  <results>
    <result>
      <binding name="name">
        <literal>Hours</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>Meters per Hour</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal datatype="http://www.w3.org/2001/XMLSchema#string">Meters per Hour</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>Meters per Minute</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>PercentageCompleted</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>Pieces per Hour</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal datatype="http://www.w3.org/2001/XMLSchema#string">Pieces per Hour</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>Pieces per Minute</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal datatype="http://www.w3.org/2001/XMLSchema#string">Pieces per Minute</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>Product parts</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>Products per Day</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal datatype="http://www.w3.org/2001/XMLSchema#string">Products per Day</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>SI Cubic Meter</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>€ per Hour</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>€ per Minute</literal>
      </binding>
    </result>
    <result>
      <binding name="name">
        <literal>€ per Product</literal>
      </binding>
    </result>
  </results>
</sparql>

1 个答案:

答案 0 :(得分:1)

您的查询与此类似

prefix xsd: <http://www.w3.org/2001/XMLSchema#>

select distinct ?name where {
  values ?name { "meters per hour"
                 "meters per hour"^^xsd:string }
}
---------------------------------
| name                          |
=================================
| "meters per hour"             |
| "meters per hour"^^xsd:string |
---------------------------------

相反,您应该选择str(?name)的不同值(即字面值的字符串值):

prefix xsd: <http://www.w3.org/2001/XMLSchema#>

select distinct (str(?name) as ?sname) where {
  values ?name { "meters per hour"
                 "meters per hour"^^xsd:string }
}
---------------------
| sname             |
=====================
| "meters per hour" |
---------------------

请注意,将来这可能不是一个问题。 RDF 1.1将使所有文字都具有数据类型,而以前简单的文字(例如"meters per hour")将具有数据类型xsd:string(例如,"meters per hour"^^xsd:string)。