ShExC vs SHACL

Jose Emilio Labra Gayo edited this page Dec 17, 2015 · 21 revisions

Examples comparing ShEx compact syntax with SHACL

This page contains some examples comparing ShExC with SHACL.

Contents

Example: Simple shapes

Example: a :Item has a rdfs:label with value xsd:string and an :code with value xsd:integer

:Item {
   rdfs:label xsd:string,
   :code xsd:integer
}

In SHACL

:Item 
   a sh:Shape ;
   sh:property [
 	  sh:predicate rdfs:label ;
 	  sh:datatype xsd:string ;
 	  sh:minCount 1 ;
 	  sh:maxCount 1 ;
 	] ;
   sh:property [
 	  sh:predicate :code ;
 	  sh:datatype xsd:integer ;
 	  sh:minCount 1 ;
 	  sh:maxCount 1 ;
 	] .

Example: Optionals with Fixed values

Example: a <UserShape has a rdfs:label with value xsd:string and an optional property ex:role which in case that appears must have value ex:User.

<UserShape> {
   rdfs:label xsd:string,
   ex:role ( ex:User ) ?
}

In SHACL the following code doesn't work:

:UserShape
   a sh:Shape ;
   sh:property [
        sh:predicate rdfs:label ;
        sh:datatype xsd:string ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
    ] ;
    sh:property [
        sh:predicate :role ;
        sh:hasValue :User ;
        sh:minCount 0 ;
        sh:maxCount 1 ;
    ]    .

It can be defined using filterShape as:

:UserShape
   a sh:Shape ;
   sh:property [
        sh:predicate rdfs:label ;
        sh:datatype xsd:string ;
        sh:minCount 1 ;
        sh:maxCount 1 ;
    ] ; 
    sh:property [
        sh:predicate :role ;
        sh:hasValue :User ;
        sh:filterShape [
            sh:property [
                sh:predicate :role ;
                sh:minCount 1 ;
            ]
        ] ;        
	    sh:maxCount 1 ;
    ]    .

Another solution is to use OrConstraint as:

:UserShape a sh:Shape ;
  sh:property [
	  sh:predicate rdfs:label ;
	  sh:datatype xsd:string ;
	  sh:minCount 1 ;
	  sh:maxCount 1 ;
	] ;
  sh:constraint [
   a sh:OrConstraint ;
   sh:shapes (
    [ a sh:Shape ;
  	  sh:property [
 	   sh:predicate :role ;
 	   sh:maxCount 0
 	  ]
	]
	[ a sh:Shape ;
	 sh:property [
      sh:predicate :role ;
	  sh:hasValue :User ;
	  sh:minCount 1 ;
	  sh:maxCount 1 ;
	 ]
	]
   )
  ] 
.

You can Run example in ShEx

Example: Groupings and cardinalities

The following example defines a <Parent> shape as nodes that the one or more combination of properties :hasSon with any value and :sonBirthDate with xsd:date value.

<Parent> {
  ( :hasSon ., 
    :sonBirthDate xsd:date ) +
}

Is it possible to represent it in SHACL? See Issue-57

You can Run the example in ShEx

Repeated properties: two codes

Define items that must have two codes, one with two digits and the other that starts with an uppercase letter.

ShExC:

<item> {
 :code xsd:string PATTERN "\\d{2}", 
 :code xsd:string PATTERN "^[A-Z]"
}

SHACL:

:ItemShape
	a sh:Shape ;
	sh:scopeClass :Item ;
	sh:property [
		sh:predicate :code ;
		sh:qualifiedMinCount 1 ;
		sh:qualifiedValueShape [ 
		 sh:constraint [ sh:pattern "^[A-Z]" ;] ]
	] ;
	sh:property [
		sh:predicate :code ;
		sh:qualifiedMinCount 1 ;
		sh:qualifiedValueShape [
		 sh:constraint [ sh:pattern "\\d{2}" ; ]
		]
	] ;
	sh:property [
	   sh:predicate :code ;
	   sh:minCount 2 ;
	   sh:maxCount 2
	]
.

Example: Exclusive Or

An :Item shape must have a property rdf:type with value :Item and either :source with an IRI or a :computation with a value of shape :Computation.

In ShEx:

 :Item {
   a (:Item), 
   ( :source IRI 
   | :computation @:Computation 
   )
 }

 :Computation { a (:Computation) }

In SHACL:

 :Item a sh:Shape ;
  sh:property [
   sh:predicate rdf:type ;
   sh:hasValue :Item ;
   sh:minCount 1 ;
   sh:maxCount 1
  ] ;
  sh:constraint [
  a sh:OrConstraint ;
  sh:shapes (
 	[ sh:property [
 	  sh:predicate :source ;
 	  sh:nodeKind sh:IRI ;
 	  sh:minCount 1 ;
 	  sh:maxCount 1 ;
 	]]
 	[ sh:property [
 	  sh:predicate :computation ;
 	  sh:valueShape :Computation ;
 	  sh:minCount 1 ;
 	  sh:maxCount 1 ;
 	]]
   ) 
  ] ;
  sh:constraint [
   a sh:NotConstraint ;
   sh:shape [ 
   sh:constraint [
   a sh:AndConstraint ;
   sh:shapes (
    [ sh:property [
 	  sh:predicate :source ;
 	  sh:nodeKind sh:IRI ;
 	  sh:minCount 1 ;
 	  sh:maxCount 1 
    ]]
    [ sh:property [
 	  sh:predicate :computation ;
 	  sh:valueShape :Computation ;
 	  sh:minCount 1 ;
 	  sh:maxCount 1 ;
 	]
    ] 
   )
   ]
   ]
  ] 
 .
 
 :Computation a sh:Shape ;
  sh:property [
   sh:predicate rdf:type ;
   sh:hasValue :Computation ;
   sh:minCount 1 ;
   sh:maxCount 1
 ] .

WebIndex data model

The following paper contains a more detailed comparison between ShEx and SHACL applied to describe the contents of a real data portal

  • Validating and Describing Linked Data Portals using Shapes, by Jose Emilio Labra Gayo, Eric Prud'hommeaux, Harold Solbrig, Iovka Boneva. Draft under review, submitted to Semantic Web Journal. PDF
  • Schema in ShEx
  • Schema in SHACL
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.