2014-10-07 18 views
3

我有一個JSON-LD文檔。現在在JSON-LD中,是否可以擴展上下文?

{ 
    "@id": "VDWW1LL3MZ", 
    "first_name": "Vincent", 
    "last_name": "Willems", 
    "knows":["MartyP"], 
    "@context": { 
    "foaf": "http://xmlns.com/foaf/0.1/", 
    "first_name": "foaf:givenName", 
    "last_name": "foaf:familyName", 
    "knows": "foaf:knows", 
    "MartyP": { 
     "@id": "http://example.com/martyp", 
     "first_name": "Marty", 
     "last_name": "P" 
    } 
    } 
} 

,在運行時(該Marty P對象)生成此文檔的上下文中的一部分,但foaf前綴定義是靜態的,並重復每個文檔。

如果我有10個前綴定義,將它們放在每個文檔中會感到很浪費。所以我想這樣做

generated document

{ 
    "@id": "VDWW1LL3MZ", 
    "first_name": "Vincent", 
    "last_name": "Willems", 
    "knows":["MartyP"], 
    "@context": { 
    "@extends": "http://example.com/base_context.jsonld", 
    "MartyP": { 
     "@id": "http://example.com/martyp", 
     "first_name": "Marty", 
     "last_name": "P" 
    } 
    } 
} 

base_context.jsonld

{ 
    "foaf": "http://xmlns.com/foaf/0.1/", 
    "first_name": "foaf:givenName", 
    "last_name": "foaf:familyName", 
    "knows": "foaf:knows" 
    } 

這可能嗎?

+2

順便說一下,'MartyP'資源不應該在@context中。上下文僅用於將JSON鍵和值轉換爲URI。相反,'MartyP'對象應該完全位於'known'數組中。請試試[JSON-LD playgroud](http://json-ld.org/playground/)以獲取它的一個掛起。 – 2014-10-07 12:53:07

回答

5

每個@context實際上可以是多個對象(或URL),然後將其在它們出現(這樣它可以改變術語的含義 - 謹慎那裏)的順序組合。

爲此,您使用一個數組,您可以在其中混合局部和外部上下文。以下是您的示例

{ 
    "@context": 
    [ 
    "http://example.com/base_context.jsonld", 
    { 
     "@vocab": "http://example.com/" 
    } 
    ] 
} 

它在JSON-LD規範的section 6.7中描述。