2

我有以下带有 Schema.org RDFa 的 HTML:

<li class="product" typeof="s:Product">
  <a href="item.php?id=227">
     <img property="s:img" src="http://www.test.com/pictures/227.jpg"></a>
  <h2 property="s:name">Example name</h2>
  <div property="s:brand">Examplebrand</div>
  <div property="s:model">Examplemodel</div>
  <div rel="s:offers">
    <div class="description" typeof="s:Offer">
      <div property="s:price">79,00</div>
      <div property="s:priceCurrency" content="EUR"></div>
    </div>
  </div>
  <div property="s:productID" content="NQ==">
    <div rel="s:seller">
      <div class="description" typeof="s:Organization">
        <div property="s:name">Shop1</div>
      </div>
    </div>
  </div>
</li>

加载页面后,我想使用 SPARQL 选择(例如)> 70,00 欧元的所有产品。

但这只会返回 NULL:

PREFIX s: <http://schema.org/>
SELECT   ?a ?price
WHERE {
  ?a s:price ?price.
  FILTER (?price > 70).
}

我认为这不是将价格解释为价格/浮动。我究竟做错了什么?

4

1 回答 1

1

XHTML 不足以让我们从 RDFa 中获取相应的 RDF 数据。我已将您的 XHTML 填写到以下内容中。请注意,我使s前缀http://schema.org/基于您的 SPARQL 查询。但是,如果这些前缀没有在您的数据中排列,那将是一个容易发生故障的地方。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.1//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" 
      version="XHTML+RDFa 1.1"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:s="http://schema.org/"
      xsi:schemaLocation="http://www.w3.org/1999/xhtml
                          http://www.w3.org/MarkUp/SCHEMA/xhtml-rdfa-2.xsd"
      lang="en"
      xml:lang="en">
  <head><title>Some title</title></head>
  <body>
    <li class="product" typeof="s:Product">
      <a href="item.php?id=227">
        <img property="s:img" src="http://www.test.com/pictures/227.jpg"/></a>
      <h2 property="s:name">Example name</h2>
      <div property="s:brand">Examplebrand</div>
      <div property="s:model">Examplemodel</div>
      <div rel="s:offers">
        <div class="description" typeof="s:Offer">
          <div property="s:price">79,00</div>
          <div property="s:priceCurrency" content="EUR"></div>
        </div>
      </div>
      <div property="s:productID" content="NQ==">
        <div rel="s:seller">
          <div class="description" typeof="s:Organization">
            <div property="s:name">Shop1</div>
          </div>
        </div>
      </div>
    </li>
  </body>
</html>

将其放入W3C 的 RDFa distiller中,我们可以得到这个 RDF:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:s="http://schema.org/"
  xmlns:xhv="http://www.w3.org/1999/xhtml/vocab#"
  xmlns:xml="http://www.w3.org/XML/1998/namespace"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
  <rdf:Description rdf:about="http://www.test.com/pictures/227.jpg">
    <s:img xml:lang="en"></s:img>
  </rdf:Description>
  <s:Product>
    <s:seller>
      <s:Organization>
        <s:name xml:lang="en">Shop1</s:name>
      </s:Organization>
    </s:seller>
    <s:productID xml:lang="en">NQ==</s:productID>
    <s:model xml:lang="en">Examplemodel</s:model>
    <s:offers>
      <s:Offer>
        <s:priceCurrency xml:lang="en">EUR</s:priceCurrency>
        <s:price xml:lang="en">79,00</s:price>
      </s:Offer>
    </s:offers>
    <s:name xml:lang="en">Example name</s:name>
    <s:brand xml:lang="en">Examplebrand</s:brand>
  </s:Product>
</rdf:RDF>

查看 RDF,很容易看出为什么价格被解释为字符串:

<s:price xml:lang="en">79,00</s:price>

属性值是一个字符串,并且是一个带有语言标签的字符串!datatype但是,您可以通过添加命名空间和属性轻松指定数据类型:

<html xmlns="http://www.w3.org/1999/xhtml" 
      version="XHTML+RDFa 1.1"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      ...>
  ...
  <div property="s:price" datatype="xsd:float">79,00</div>
  ...
</html>

但是,逗号符号对于该xsd:float类型实际上是不合法的,因此您实际上还需要指定一个content属性,如下所示:

<div property="s:price" datatype="xsd:float" content="79.00">79,00</div>

在这些更改之后,您将获得以下 RDF:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  xmlns:s="http://schema.org/"
  xmlns:xhv="http://www.w3.org/1999/xhtml/vocab#"
  xmlns:xml="http://www.w3.org/XML/1998/namespace"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
  <s:Product>
    <s:productID xml:lang="en">NQ==</s:productID>
    <s:model xml:lang="en">Examplemodel</s:model>
    <s:brand xml:lang="en">Examplebrand</s:brand>
    <s:offers>
      <s:Offer>
        <s:priceCurrency xml:lang="en">EUR</s:priceCurrency>
        <s:price rdf:datatype="http://www.w3.org/2001/XMLSchema#float">79.00</s:price>
      </s:Offer>
    </s:offers>
    <s:name xml:lang="en">Example name</s:name>
    <s:seller>
      <s:Organization>
        <s:name xml:lang="en">Shop1</s:name>
      </s:Organization>
    </s:seller>
  </s:Product>
  <rdf:Description rdf:about="http://www.test.com/pictures/227.jpg">
    <s:img xml:lang="en"></s:img>
  </rdf:Description>
</rdf:RDF>

进行这些更改后,您的查询无需修改即可正常工作:

$ arq --data data3.rdf --query query.sparql 
------------------------------------------------------------
| a    | price                                             |
============================================================
| _:b0 | "79.00"^^<http://www.w3.org/2001/XMLSchema#float> |
------------------------------------------------------------
于 2013-08-07T01:25:46.273 回答