代码之家  ›  专栏  ›  技术社区  ›  Sirko

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

  •  2
  • Sirko  · 技术社区  · 6 年前

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

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

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

    这将两个资源映射到 @vocab playground ).

    <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

    2 回复  |  直到 6 年前
        1
  •  2
  •   Jay Gray    6 年前

    有一种方法:

    {
      "@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#",
      }
    }
    

    然后你会使用居里格式( https://en.wikipedia.org/wiki/CURIE )指定 key:value 配对 key vocab value vocab term 在公理/陈述中使用。

        2
  •  2
  •   Sirko    6 年前

    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-ld1.1规则集。
    • 然后我们可以将每个属性放在一个单独的上下文中,并更改 @base 在这里。

    this (dev) playground example :

    <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 @声带 在作用域上下文中而不是 @底座

    {
      "@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> .