Schema.org价格和SPARQL

时间:2011-11-30 23:45:40

标签: sparql rdfa

我在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选择所有产品(例如)&gt; €70,00。

但这只会返回NULL:

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

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

1 个答案:

答案 0 :(得分:0)

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

<!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's 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> |
------------------------------------------------------------