在@context 中提供@type 作为值

Providing the @type in the @context for a value

我有以下 json-ld 文档:

{
     "@context": {
        "ex": "http://example.com/",
        "yyyy": "ex:yyyy",
        "name": "ex:name",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "sch": "http://schema.org/",
        "xml": "http://www.w3.org/XML/1998/namespace",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
    "@id": "ex:Bobe",
    "@type": "ex:MyType",
    "yyyy": {
        "@type": "ex:XXXX",
        "name": "my name"
    }
}

RDF 表示是:

@prefix ex: <http://example.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sch: <http://schema.org/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Bobe a ex:MyType ;
    ex:yyyy [ a ex:XXXX ;
            ex:name "my name" ] .

我希望能够做的是将 "yyyy": { ... } 写成

"yyyy": {
    "name": "my name"
}

并在“@context”中指定 "@type": "ex:XXXX"

这可能吗?

我试过但没想到会奏效的是:

{
    "@context": {
        "ex": "http://example.com/",
        "yyyy": {
            "@id": "ex:yyyy",
            "@type": "ex:XXXX"
        },
        "name": "ex:name",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "sch": "http://schema.org/",
        "xml": "http://www.w3.org/XML/1998/namespace",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
    "@id": "ex:Bobe",
    "@type": "ex:MyType",
    "yyyy": {
        "name": "my name"
    }
}

这有一个 RDF 表示:

@prefix ex: <http://example.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sch: <http://schema.org/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Bobe a ex:MyType ;
    ex:yyyy [ ex:name "my name" ] .

JSON-LD Playground 上的 N-Quad 表示是:

<http://example.com/Bobe> <http://example.com/yyyy> _:b0 .
<http://example.com/Bobe> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/MyType> .
_:b0 <http://example.com/name> "my name" .

所以,“@type”信息丢失了。

当然,它需要在我遇到的情况下工作:

{
    "@context": {
        "ex": "http://example.com/",
        "yyyy": {
            "@id": "ex:yyyy",
            "@type": "ex:XXXX"
        },
        "name": "ex:name",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "sch": "http://schema.org/",
        "xml": "http://www.w3.org/XML/1998/namespace",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
    "@id": "ex:Bobe",
    "@type": "ex:MyType",
    "yyyy": [ { "name": "my name" },
              { "name": "my other" } ]
}

我认为这不可能,但想确认一下。

通常用于生成此输出的 python 代码是:

graph_data = """
{
    "@id": "ex:Bobe",
    "@type": "ex:MyType",
    "@context": {
        "ex": "http://example.com/",
        "yyyy": "ex:yyyy",
        "name": "ex:name",
        "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
        "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
        "sch": "http://schema.org/",
        "xml": "http://www.w3.org/XML/1998/namespace",
        "xsd": "http://www.w3.org/2001/XMLSchema#"
    },
    "yyyy": {
        "@type": "ex:XXXX",
        "name": "my name"
    }
}
"""

print( graph_data )

data  = rdflib.Graph().parse( data = graph_data, format = 'json-ld' )
print( f"{data.serialize( format = 'ttl' ).decode( 'utf8' )}" )

来自https://www.w3.org/TR/json-ld11/#context-definitions

"A context definition MUST be a map whose keys MUST be either terms, compact IRIs, IRIs, or one of the keywords @base, @import, @language, @propagate, @protected, @type, @version, or @vocab. "

所以 @context 真的对嵌套信息一无所知,因为

"yyyy": {
    "@id": "ex:yyyy",
    "@type": "ex:XXXX"
},

@context不能用于结构,只能用于命名空间管理