【发布时间】:2019-10-29 18:12:28
【问题描述】:
我希望使用 SHACL 验证以下 JSON-LD:
{
"@context" : {
"day" : {
"@id" : "test:day"
},
"month" : {
"@id" : "test:month"
},
"myList" : {
"@id" : "test:myList"
},
"year" : {
"@id" : "test:year"
},
"schema" : "http://schema.org/",
"rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"xsd" : "http://www.w3.org/2001/XMLSchema#",
"test" : "http://www.test.com/ns#"
},
"@graph" : [ {
"@id" : "test:MyNode",
"@type" : "test:MyTargetClass",
"myList" : [
{
"year" : "2019",
"month" : "October",
"day" : "29"
},
{
"year" : "2018",
"month" : "January",
"day" : "17"
}
]
} ]
}
在上面的示例中,myList 是必须包含至少一个元素的对象列表,每个元素必须包含所有三个字段:year、month 和 @ 987654326@。以下 TTL 用于验证它:
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix test: <http://www.test.com/ns#> .
test:MyListShape
a sh:NodeShape ;
sh:closed true ;
sh:ignoredProperties ( rdf:type ) ;
sh:property [
sh:path test:year ;
sh:datatype xsd:string ;
sh:minCount 1 ;
sh:maxCount 1 ;
] ;
sh:property [
sh:path test:month ;
sh:datatype xsd:string ;
sh:minCount 1 ;
sh:maxCount 1 ;
] ;
sh:property [
sh:path test:day ;
sh:datatype xsd:string ;
sh:minCount 1 ;
sh:maxCount 1 ;
] .
test:MyShape
a sh:NodeShape ;
sh:closed true ;
sh:ignoredProperties ( rdf:type ) ;
sh:targetClass test:MyTargetClass ;
sh:property [
sh:path test:myList ;
sh:node test:MyListShape ;
sh:minCount 1 ;
] .
尝试验证 JSON-LD 返回包含以下 sn-p 的响应,说明数据为什么不符合:
{
"@id" : "_:b3",
"@type" : "http://www.w3.org/ns/shacl#ValidationResult",
"focusNode" : "test:MyNode",
"resultMessage" : "Property may only have 1 value, but found 2",
"resultPath" : "test:myList",
"resultSeverity" : "http://www.w3.org/ns/shacl#Violation",
"sourceConstraintComponent" : "http://www.w3.org/ns/shacl#MaxCountConstraintComponent",
"sourceShape" : "_:b4"
}
为什么 test:myList 属性必须只有 1 个值,即使我没有指定 sh:maxCount?
我还尝试将@context 中的myList 更改为以下内容:
"myList" : {
"@id" : "test:myList",
"@container" : "@list"
}
但是,这也确实不符合,并返回包含以下 sn-p 的响应:
{
"@id" : "_:b0",
"@type" : "http://www.w3.org/ns/shacl#ValidationResult",
"focusNode" : "test:MyNode",
"resultMessage" : "Value does not have shape test:MyListShape",
"resultPath" : "test:myList",
"resultSeverity" : "http://www.w3.org/ns/shacl#Violation",
"sourceConstraintComponent" : "http://www.w3.org/ns/shacl#NodeConstraintComponent",
"sourceShape" : "_:b2",
"value" : "_:b1"
}
我遇到的另一种解决方案是将myList 存储在@graph 的单独节点中,但这不适合我的用例:
{
"@id" : "test:myListNode",
"@type" : "test:myListNode",
"year" : [ "2019", "2018" ],
"month" : [ "October", "January" ],
"day" : [ "29", "17" ]
}
因此,是否可以使用 SHACL 来验证包含对象列表的 JSON-LD 而无需使用此替代解决方案?
【问题讨论】:
-
您使用的是什么 SHACL 引擎?对于第一个示例,无论是使用 TopBraid SHACL API 还是 SHACL Playground,我都没有收到错误。
-
关于验证 rdf:Lists 的一般主题,请参阅topquadrant.com/constraints-on-rdflists-using-shacl
-
也无法使用 Apache Jena 3.13.1 重现它
-
@HolgerKnublauch 我意识到它失败了,因为我不想进入这个问题。你是正确的,第一个例子 确实 符合。感谢您的帮助!
-
@Ann 就像我说的那样,这可以被认为是一个包含单个元素的数组,因此不需要方括号。如果您使用
"myList" : [{ "year" : "2019", "month" : "October", "day" : "29" }],我认为验证仍然会成功
标签: rdf jena json-ld turtle-rdf shacl