2

我希望使用 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一个元素的对象列表,每个元素都必须包含所有三个字段:、和。以下 TTL 用于验证它:yearmonthday

@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 会返回包含以下代码段的响应,说明数据不符合的原因

{
  "@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即使我没有指定 a ,为什么该属性必须只有 1 个值sh:maxCount


我还尝试将myList其更改@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 而无需使用此替代解决方案?

4

1 回答 1

0

是的,SHACL 可以验证包含对象列表的 JSON-LD。

PEBCAK 错误导致失败。(看评论。)

于 2019-10-31T15:41:05.760 回答