JSON-LD上下文中的不同值前缀

问题描述 投票:2回答:2

所以我有一个预定义的本体和一个现有的JSON服务(两次都读作“不能改变那里的现有内容”)返回类似下面的JSON:

{
  "propA": "someResource",
  "propB": "otherResource"
}

我想通过添加(我不能更改现有属性,但可以添加新属性)上下文定义将该输出转换为JSON-LD。在第一步中,我添加了一个默认上下文,如下所示:

  "@context": {
    "@vocab": "https://example.org/",

    "propA": { "@type": "@vocab" },
    "propB": { "@type": "@vocab" }
  },
  "@id": "https://example.org/blub",

这将两个资源映射到@vocabplayground)给出的命名空间。

<https://example.org/blub> <https://example.org/propA> <https://example.org/someResource> .
<https://example.org/blub> <https://example.org/propB> <https://example.org/otherResource> .

但是,两个引用的资源都属于两个不同的名称空间。所以我需要的是一些上下文,映射到以下内容:

<https://example.org/blub> <https://example.org/propA> <https://foo.com/someResource> .
<https://example.org/blub> <https://example.org/propB> <https://bar.com/otherResource> .

在某处我找到了hack using @base,但这只是一种解决方法,如果你需要一个额外的命名空间而不是多个命名空间。

那么当我需要两个以上的属性时,如何为不同的属性定义单独的命名空间前缀?

namespaces json-ld
2个回答
2
投票

这是一种方法:

{
  "@context": {
    "dcat": "http://www.w3.org/ns/dcat#",
    "org": "http://www.w3.org/ns/org#",
    "vcard": "http://www.w3.org/2006/vcard/ns#",
    "foaf": "https://project-open-data.cio.gov/v1.1/schema#",
    "dc": "http://purl.org/dc/terms/",
    "pod": "https://project-open-data.cio.gov/v1.1/schema#",
    "skos": "http://www.w3.org/2004/02/skos/core#",
  }
}

那么你将使用CURIE格式(https://en.wikipedia.org/wiki/CURIE)来指定key:value对,其中keyvocabvalue是公理/声明中使用的vocab term


2
投票

经过一些摆弄后,我认为JSON-LD 1.1提供了一个答案:scoped contexts

{
  "@context": {
    "@version": 1.1,
    "@vocab": "https://example.org/",

    "propA": { "@type": "@id", "@context": { "@base": "https://foo.com/"} },
    "propB": { "@type": "@id", "@context": { "@base": "https://bar.com/"} }
  },
   "@id": "https://example.org/blub",

  "propA": "someResource",
  "propB": "otherResource"
}

有两点需要注意:

  • 加入"@version": 1.1至关重要。这告诉兼容处理器使用JSON-LD 1.1规则集。
  • 然后我们可以将每个属性放在一个单独的上下文中,并在这里更改@base

根据this (dev) playground example,这将导致以下Turtle表示:

<https://example.org/blub> <https://example.org/propA> <https://foo.com/someResource> .
<https://example.org/blub> <https://example.org/propB> <https://bar.com/otherResource> .

旁注:我们甚至可以通过将@type设置为@vocab并定义范围上下文的映射来以类似的方式定义可能的值映射的枚举。注意,这次我们必须在作用域上下文中设置@vocab而不是@base

{
  "@context": {
    "@version": 1.1,
    "@vocab": "https://example.org/",

    "propA": { 
      "@type": "@vocab", 
      "@context": { 
        "@vocab": "http://foo.com/", 

        "abc": "http://bar.com/abc", 
        "xyz": "http://baz.com/xyz"
      } 
    }
  },
   "@id": "https://example.org/blub"
}

现在,根据给予propA的值,使用了不同的命名空间(play with it):

"abc" -> <https://example.org/blub> <https://example.org/propA> <http://bar.com/abc> .
"xyz" -> <https://example.org/blub> <https://example.org/propA> <http://baz.com/xyz> .
"mnl" -> <https://example.org/blub> <https://example.org/propA> <http://foo.com/mnl> .
© www.soinside.com 2019 - 2024. All rights reserved.