0

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

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

谢谢你的帮助。问题示例如下,

我的固定命名空间

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

当我使用此查询时,我会得到带有“数据类型”的重复值,例如

    <?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>
4

1 回答 1

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)。

于 2013-11-06T13:11:49.680 回答