如何在JSON-LD中为RDF值编码数据类型IRI?

时间:2016-02-14 00:03:29

标签: rdf json-ld

JSON-LD上下文可用于指定属性的范围。例如,以下统计数据rdf:value的范围由整数组成:

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "rdf:value": { "@type": "xsd:integer" }
  },
  "rdf:value": "1"
}

在RDF建模中,对rdf:value的不同用途使用不同的范围是很常见的。例如,以下表示物体成本为2.50欧元,温度为28.2℃(使用海龟表示法):

_:1 ex:price [ rdf:value "2.50"​^^xsd:decimal ; ex:unit ex:euros ] ;
    ex:temperature [ rdf:value "28.2"^^xsd:float ; ex:unit ex:degreesCelsius ] .

如何根据JSON-LD上下文对此进行描述?在我看来,我需要属性路径(从SPARQL借用一个概念)作为键,特别是当前示例的以下内容:

"ex:price/rdf:value": "xsd:decimal"
"ex:temperature/rdf:value": "xsd:float"

有没有办法在JSON-LD中指定它?

3 个答案:

答案 0 :(得分:1)

您可以通过指定typed value来提供value object

示例:

{
  "@context": 
  {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  },
  "rdf:value": 
  {
    "@value": "1",
    "@type": "xsd:integer"
  }
}

答案 1 :(得分:1)

您还可以nest @context专门化/覆盖属性。举个例子:

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "rdf:value": { "@type": "xsd:integexr" }
  },
  "rdf:value": "1",
  "ex:price": {
    "@context": {
      "rdf:value": { "@type": "xsd:float"}
    },
    "rdf:value": "35.3"
  },
  "ex:temperature": {
    "@context": {
      "rdf:value": { "@type": "xsd:decimal"}
    },
    "rdf:value": "2.50"
  }
}

你可以experiment with this in the JSON-LD Playground

另一种方法是使用全部映射到一个@idrdf:value)但具有不同数据类型的自定义属性:

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "value_integer": {
      "@id": "rdf:value",
      "@type": "xsd:integer"
    },
    "value_float": {
      "@id": "rdf:value",
      "@type": "xsd:float"
    },
    "value_decimal": {
      "@id": "rdf:value",
      "@type": "xsd:decimal"
    }
  },
  "value_integer": "1",
  "ex:price": {
    "value_decimal": "35.3"
  },
  "ex:temperature": {
    "value_float": "2.50"
  }
}

请参阅this example on the JSON-LD playground

答案 2 :(得分:1)

最简单的方法是引入单独的属性。类似的东西(我还在这里设置了@vocabex):

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "price_value": { "@id": "rdf:value", "@type": "xsd:decimal" },
    "temperature_value": { "@id": "rdf:value", "@type": "xsd:float" },
    "@vocab": "http://ex.org/",
    "unit": { "@type": "@vocab" }
  },
  "price": {
    "price_value": "2.50",
    "unit": "euros"
  },
  "temperature": {
    "temperature_value": "28.2",
    "unit": "degreesCelsius"
  }
}