我希望使用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和day。正在使用以下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返回一个包含以下代码段的响应,说明为什么不符合:
{
"@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属性必须只有一个值,即使我没有指定一个sh:maxCount
我还尝试将@context中的@context更改为:
"myList" : {
"@id" : "test:myList",
"@container" : "@list"
}但是,这也使不符合,并返回包含以下代码段的响应:
{
"@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,而不必使用此替代解决方案?
发布于 2019-10-31 15:41:05
是的,SHACL可以验证包含对象列表的JSON。
PEBCAK错误导致故障。(见评论)
https://stackoverflow.com/questions/58613222
复制相似问题