Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, a SHACL schema can incorporate the definition of a discriminated union using the sh:or constraint. The sh:or constraint allows the definition of a union of constraints, where only one of the constraints needs to be satisfied for the data to be valid. The sh:or constraint can be used to define a discriminated union by using a combination of sh:nodeKind and sh:or constraints. Here is an example of how this can be done:

@prefix ex: <http://example.com/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Document
  a sh:NodeShape ;
  sh:targetClass ex:Document .

ex:TextDocument
  a sh:NodeShape ;
  sh:targetClass ex:TextDocument ;
  sh:nodeKind sh:BlankNodeOrLiteral ;
  sh:or (
    [ sh:datatype xsd:string ] 
    [ sh:nodeKind sh:Literal ; sh:hasValue "text" ]
  ) .

ex:ImageDocument
  a sh:NodeShape ;
  sh:targetClass ex:ImageDocument ;
  sh:nodeKind sh:Resource ;
  sh:or (
    [ sh:class ex:JpegImage ] 
    [ sh:class ex:PngImage ]
  ) .

ex:JpegImage
  a sh:NodeShape ;
  sh:targetClass ex:JpegImage .

ex:PngImage
  a sh:NodeShape ;
  sh:targetClass ex:PngImage .

In this example, we have defined a discriminated union between ex:TextDocument and ex:ImageDocument. The ex:TextDocument shape specifies that it must either be a string or have the value "text". The ex:ImageDocument shape specifies that it must either be an instance of ex:JpegImage or ex:PngImage.