0
我們最近從3.0.1切換到Jena 3.1.0,並發現Jena寫出JSON- LD轉換爲字符串格式。Jena 3.0.1和3.1.0,RDF/XML到JSON-LD缺少前綴
下面是JSON-LD的樣子與耶拿3.0.1:
{
"@graph" : [ {
"@id" : "data:4d1a75b0-484f-4dfa-998f-4382f34e411f",
"@type" : "assertion:assertion",
"data:UUID" : "4d1a75b0-484f-4dfa-998f-4382f34e411f"
}, {
"@id" : "data:UUID",
"@type" : "owl:DatatypeProperty",
"rdfs:label" : {
"@language" : "en",
"@value" : "UUID"
}
}, {
"@id" : "urn:example.data.1.0",
"@type" : "owl:Ontology",
"rdfs:comment" : {
"@language" : "en",
"@value" : "This is an OWL ontology to describe data."
},
"rdfs:label" : {
"@language" : "en",
"@value" : "Data ontology"
},
"owl:versionInfo" : "1.0"
}, {
"@id" : "assertion:assertion",
"@type" : "owl:Class",
"subClassOf" : "data:entity"
} ],
"@context" : {
"comment" : {
"@id" : "http://www.w3.org/2000/01/rdf-schema#comment",
"@type" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
},
"label" : {
"@id" : "http://www.w3.org/2000/01/rdf-schema#label",
"@type" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
},
"versionInfo" : {
"@id" : "http://www.w3.org/2002/07/owl#versionInfo",
"@type" : "http://www.w3.org/2001/XMLSchema#string"
},
"UUID" : {
"@id" : "urn:example.data#UUID",
"@type" : "http://www.w3.org/2001/XMLSchema#string"
},
"subClassOf" : {
"@id" : "http://www.w3.org/2000/01/rdf-schema#subClassOf",
"@type" : "@id"
},
"data" : "urn:example.data#",
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"owl" : "http://www.w3.org/2002/07/owl#",
"xsd" : "http://www.w3.org/2001/XMLSchema#",
"rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
"assertion" : "urn:example.data.assertion#"
}
}
下面是JSON-LD的樣子在耶拿3.1.0:
{
"@graph" : [ {
"@id" : "data:4d1a75b0-484f-4dfa-998f-4382f34e411f",
"@type" : "assertion:assertion",
"UUID" : "4d1a75b0-484f-4dfa-998f-4382f34e411f"
}, {
"@id" : "data:UUID",
"@type" : "owl:DatatypeProperty",
"label" : {
"@language" : "en",
"@value" : "UUID"
}
}, {
"@id" : "urn:example.data.1.0",
"@type" : "owl:Ontology",
"comment" : {
"@language" : "en",
"@value" : "This is an OWL ontology to describe data."
},
"label" : {
"@language" : "en",
"@value" : "Data ontology"
},
"versionInfo" : "1.0"
}, {
"@id" : "assertion:assertion",
"@type" : "owl:Class",
"subClassOf" : "data:entity"
} ],
"@context" : {
"comment" : {
"@id" : "http://www.w3.org/2000/01/rdf-schema#comment"
},
"label" : {
"@id" : "http://www.w3.org/2000/01/rdf-schema#label"
},
"versionInfo" : {
"@id" : "http://www.w3.org/2002/07/owl#versionInfo"
},
"UUID" : {
"@id" : "urn:example.data#UUID"
},
"subClassOf" : {
"@id" : "http://www.w3.org/2000/01/rdf-schema#subClassOf",
"@type" : "@id"
},
"data" : "urn:example.data#",
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"owl" : "http://www.w3.org/2002/07/owl#",
"xsd" : "http://www.w3.org/2001/XMLSchema#",
"rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
"assertion" : "urn:example.data.assertion#"
}
}
的區別二者之間的區別在於名稱空間前綴數據:和rfds:不再出現在標籤(如UUID和標籤)旁邊。
根據Jena,JSON-LD是有效的,但不幸的是我們需要將JSON-LD發送到期望這些前綴在那裏的服務器。
我們能做些什麼來控制輸出嗎?我們不是專家耶拿,請處理我們心慈手軟:(
下面是XML格式的原始消息:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:data="urn:example.data#"
xmlns:assertion="urn:example.data.assertion#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:owl="http://www.w3.org/2002/07/owl#">
<owl:Ontology rdf:about="urn:example.data.1.0">
<owl:versionInfo>1.0</owl:versionInfo>
<rdfs:label xml:lang="en">Data ontology</rdfs:label>
<rdfs:comment xml:lang="en">This is an OWL ontology to describe data.</rdfs:comment>
</owl:Ontology>
<owl:Class rdf:about="urn:example.data.assertion#assertion">
<rdfs:subClassOf rdf:resource="urn:example.data#entity"/>
</owl:Class>
<owl:DatatypeProperty rdf:about="urn:example.data#UUID">
<rdfs:label xml:lang="en">UUID</rdfs:label>
</owl:DatatypeProperty>
<assertion:assertion rdf:about="urn:example.data#4d1a75b0-484f-4dfa-998f-4382f34e411f">
<data:UUID>4d1a75b0-484f-4dfa-998f-4382f34e411f</data:UUID>
</assertion:assertion>
</rdf:RDF>
而下面是我們運行的最低代碼:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("convert-xml-json-test/temp.xml");
String inputXml = IOUtils.toString(inputStream);
// Convert the XML to RDF model
StringReader stringReader = new StringReader(inputXml);
Model model = ModelFactory.createDefaultModel();
model.read(stringReader, null, RDFLanguages.RDFXML.getLabel());
// Convert the model to JSON String
ByteArrayOutputStream out = new ByteArrayOutputStream();
model.write(out, RDFLanguages.JSONLD.getLabel());
outputJson = out.toString(StandardCharsets.UTF_8.toString());
我們非常有信心,這是由於耶拿的變化,因爲我們最小的測試項目只包括Jena,如在mvn依賴中所見:樹下
+- org.apache.jena:jena-tdb:jar:3.1.0:compile
| +- org.apache.jena:jena-arq:jar:3.1.0:compile
| | +- org.apache.jena:jena-core:jar:3.1.0:compile
| | | +- org.apache.jena:jena-iri:jar:3.1.0:compile
| | | +- xerces:xercesImpl:jar:2.11.0:compile
| | | | \- xml-apis:xml-apis:jar:1.4.01:compile
| | | +- commons-cli:commons-cli:jar:1.3:compile
| | | \- org.apache.jena:jena-base:jar:3.1.0:compile
| | | \- com.github.andrewoma.dexx:collection:jar:0.6:compile
| | +- org.apache.jena:jena-shaded-guava:jar:3.1.0:compile
| | +- org.apache.httpcomponents:httpclient:jar:4.2.6:compile
| | | +- org.apache.httpcomponents:httpcore:jar:4.2.5:compile
| | | \- commons-codec:commons-codec:jar:1.6:compile
| | +- com.github.jsonld-java:jsonld-java:jar:0.7.0:compile
| | | +- com.fasterxml.jackson.core:jackson-core:jar:2.3.3:compile
| | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.3.3:compile
| | | | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.3.0:compile
| | | \- commons-io:commons-io:jar:2.4:compile
| | +- org.apache.httpcomponents:httpclient-cache:jar:4.2.6:compile
| | +- org.apache.thrift:libthrift:jar:0.9.2:compile
| | +- org.slf4j:jcl-over-slf4j:jar:1.7.20:compile
| | +- org.apache.commons:commons-csv:jar:1.0:compile
| | \- org.apache.commons:commons-lang3:jar:3.3.2:compile
| \- org.slf4j:slf4j-api:jar:1.7.20:compile
\- junit:junit:jar:4.11:test
\- org.hamcrest:hamcrest-core:jar:1.3:test
爲什麼其他系統需要這些設置?它是相同的RDF(對於RDF 1.1)https://issues.apache.org/jira/browse/JENA-1163。這就是爲什麼rdf:langString和xsd:字符串不再出現的原因 - RDF 1.1不需要它們。這改變了@type。它似乎影響了第三方jsonld-java庫的編寫。有關更多可配置性的工作,另請參閱https://issues.apache.org/jira/browse/JENA-1208。 – AndyS
非常漫長而複雜的公司故事,但我可以把它歸結爲客戶端的糟糕設計。他們通過JSON-LD進行解析,就好像它是常規的JSON一樣。我們試圖向客戶解釋,但他們不願意做出任何改變。最終,他們的錯誤已成爲我們的問題,我們在截止到週三之前有一個截止日期。我們非常絕望,我們已經編寫了一個類來解析JSON-LD並重新應用前綴。到目前爲止,它似乎工作。在我們通過幾個單元測試運行之後,它會將其發佈。 – Thirlan
有一個Jena 3.0.1過程,它執行「riot --pretty JSONLD」並讀入3.1.0輸出。如果你想通過它,它會以舊式寫。 – AndyS