### SHACL sh:node Constraint Example (Turtle) Source: https://www.w3.org/TR/shacl/index This example illustrates the sh:node constraint, ensuring that all values of the 'ex:address' property conform to the 'ex:AddressShape'. The 'ex:AddressShape' itself defines constraints for the 'ex:postalCode' property. Example data for persons and their addresses are provided, along with a sample validation report. ```turtle ex:AddressShape a sh:NodeShape ; sh:property [ sh:path ex:postalCode ; sh:datatype xsd:string ; sh:maxCount 1 ; ] . ex:PersonShape a sh:NodeShape ; sh:targetClass ex:Person ; sh:property [ # _:b1 sh:path ex:address ; sh:minCount 1 ; sh:node ex:AddressShape ; ] . ex:Bob a ex:Person ; ex:address ex:BobsAddress . ex:BobsAddress ex:postalCode "1234" . ex:Reto a ex:Person ; ex:address ex:RetosAddress . ex:RetosAddress ex:postalCode 5678 . [ a sh:ValidationReport ; sh:conforms false ; sh:result [ a sh:ValidationResult ; sh:resultSeverity sh:Violation ; sh:focusNode ex:Reto ; sh:resultPath ex:address ; sh:value ex:RetosAddress ; sh:resultMessage "Value does not conform to shape ex:AddressShape."; sh:sourceConstraintComponent sh:NodeConstraintComponent ; sh:sourceShape _:b1 ; ] ] . ``` -------------------------------- ### SHACL Pattern Matching Example Source: https://www.w3.org/TR/shacl/index A SHACL shape example that enforces a pattern on the 'bCode' property using sh:pattern and sh:flags. The example requires the 'bCode' to start with 'B' (case-insensitive). It includes sample data, showing which values would pass and fail validation. ```turtle ex:PatternExampleShape a sh:NodeShape ; sh:targetNode ex:Bob, ex:Alice, ex:Carol ; sh:property [ sh:path ex:bCode ; sh:pattern "^B" ; # starts with 'B' sh:flags "i" ; # Ignore case ] . ``` ```turtle ex:Bob ex:bCode "b101" . ex:Alice ex:bCode "B102" . ex:Carol ex:bCode "C103" . ``` -------------------------------- ### SHACL sh:xone Constraint Example (Turtle) Source: https://www.w3.org/TR/shacl/index This example demonstrates the use of the sh:xone constraint to enforce that a 'Person' node must have either a 'fullName' or both 'firstName' and 'lastName', but not both conditions simultaneously. It includes the shape definition and example data instances. ```turtle ex:XoneConstraintExampleShape a sh:NodeShape ; sh:targetClass ex:Person ; sh:xone ( [ sh:property [ sh:path ex:fullName ; sh:minCount 1 ; ] ] [ sh:property [ sh:path ex:firstName ; sh:minCount 1 ; ] ; sh:property [ sh:path ex:lastName ; sh:minCount 1 ; ] ] ) . ex:Bob a ex:Person ; ex:firstName "Robert" ; ex:lastName "Coin" . ex:Carla a ex:Person ; ex:fullName "Carla Miller" . ex:Dory a ex:Person ; ex:firstName "Dory" ; ex:lastName "Dunce" ; ex:fullName "Dory Dunce" . ``` -------------------------------- ### Example RDF Triple Declarations Source: https://www.w3.org/TR/shacl/index Illustrates example RDF triples, highlighting focus nodes that are either valid or invalid according to SHACL validation rules. These are typically used to represent data instances. ```n-triples ex:Bob a ex:Person . ex:Alice a ex:Person . ``` -------------------------------- ### SHACL sh:and Constraint Example Source: https://www.w3.org/TR/shacl/index Illustrates sh:and for conjunction, ensuring a focus node conforms to multiple shapes. This example requires ex:property to have at least one value (from ex:SuperShape) and at most one value (from the inline shape). ```turtle ex:SuperShape a sh:NodeShape ; sh:property [ sh:path ex:property ; sh:minCount 1 ; ] . ex:ExampleAndShape a sh:NodeShape ; sh:targetNode ex:ValidInstance, ex:InvalidInstance ; sh:and ( ex:SuperShape [ sh:path ex:property ; sh:maxCount 1 ; ] ) . ex:ValidInstance ex:property "One" . # Invalid: more than one property ex:InvalidInstance ex:property "One" ; ex:property "Two" . ``` -------------------------------- ### SHACL sh:or Constraint Example (Node) Source: https://www.w3.org/TR/shacl/index Demonstrates sh:or for disjunction at the node level, requiring a focus node to satisfy at least one of the specified shapes. This example ensures ex:Bob has either an ex:firstName or an ex:givenName. ```turtle ex:OrConstraintExampleShape a sh:NodeShape ; sh:targetNode ex:Bob ; sh:or ( [ sh:path ex:firstName ; sh:minCount 1 ; ] [ sh:path ex:givenName ; sh:minCount 1 ; ] ) . ex:Bob ex:firstName "Robert" . ``` -------------------------------- ### SHACL Data Graph Example Source: https://www.w3.org/TR/shacl/index This code snippet represents a sample data graph with three instances of the 'ex:Person' class, showcasing different property values and types. ```turtle ex:Alice a ex:Person ; ex:ssn "987-65-432A" . ex:Bob a ex:Person ; ex:ssn "123-45-6789" ; ex:ssn "124-35-6789" . ex:Calvin a ex:Person ; ex:birthDate "1971-07-07"^^xsd:date ; ex:worksFor ex:UntypedCompany . ``` -------------------------------- ### SHACL sh:class Constraint - Data Example Source: https://www.w3.org/TR/shacl/index Example RDF data illustrating the usage of sh:class. It shows instances of 'ex:PostalAddress' and a node that lacks this type, which would fail validation against the ex:ClassExampleShape. ```turtle ex:Alice a ex:Person . ex:Bob ex:address [ a ex:PostalAddress ; ex:city ex:Berlin ] . ex:Carol ex:address [ ex:city ex:Cairo ] . ``` -------------------------------- ### SHACL Property Path Examples and SPARQL 1.1 Equivalents Source: https://www.w3.org/TR/shacl/index Illustrates various SHACL property paths and their corresponding SPARQL 1.1 property path equivalents, demonstrating the mapping for different path types. ```text SPARQL Property path: ex:parent SHACL Property path: ex:parent SPARQL Property path: ^ex:parent SHACL Property path: [ sh:inversePath ex:parent ] SPARQL Property path: ex:parent/ex:firstName SHACL Property path: ( ex:parent ex:firstName ) SPARQL Property path: rdf:type/rdfs:subClassOf* SHACL Property path: ( rdf:type [ sh:zeroOrMorePath rdfs:subClassOf ] ) SPARQL Property path: ex:father|ex:mother SHACL Property path: [ sh:alternativePath ( ex:father ex:mother ) ] ``` -------------------------------- ### SHACL JSON-LD Fragment Example Source: https://www.w3.org/TR/shacl/index This JSON-LD fragment represents the same SHACL shape definition as the Turtle example, illustrating how SHACL shapes can be expressed in JSON-LD. ```json { "@id" : "ex:PersonShape", "@type" : "NodeShape", "targetClass" : "ex:Person", "property" : [ { "path" : "ex:ssn", "maxCount" : 1, "datatype" : "xsd:string" , "pattern" : "^\\d{3}-\\d{2}-\\d{4}$" }, { "path" : "ex:worksFor", "class" : "ex:Company", "nodeKind" : "sh:IRI" } ], "closed" : true, "ignoredProperties" : [ "rdf:type" ] } ``` -------------------------------- ### SHACL Validation Report Example Source: https://www.w3.org/TR/shacl/index This SHACL validation report details the violations found when applying the 'ex:PersonShape' to the example data graph. It highlights specific issues with 'ex:ssn', 'ex:worksFor', and 'ex:birthDate' properties for different focus nodes. ```turtle [\ta sh:ValidationReport ; sh:conforms false ; sh:result [\t a sh:ValidationResult ; sh:resultSeverity sh:Violation ; sh:focusNode ex:Alice ; sh:resultPath ex:ssn ; sh:value "987-65-432A" ; sh:sourceConstraintComponent sh:RegexConstraintComponent ; sh:sourceShape ... blank node _:b1 on ex:ssn above ... ; ] , [\t a sh:ValidationResult ; sh:resultSeverity sh:Violation ; sh:focusNode ex:Bob ; sh:resultPath ex:ssn ; sh:sourceConstraintComponent sh:MaxCountConstraintComponent ; sh:sourceShape ... blank node _:b1 on ex:ssn above ... ; ] , [\t a sh:ValidationResult ; sh:resultSeverity sh:Violation ; sh:focusNode ex:Calvin ; sh:resultPath ex:worksFor ; sh:value ex:UntypedCompany ; sh:sourceConstraintComponent sh:ClassConstraintComponent ; sh:sourceShape ... blank node _:b2 on ex:worksFor above ... ; ] , [\t a sh:ValidationResult ; sh:resultSeverity sh:Violation ; sh:focusNode ex:Calvin ; sh:resultPath ex:birthDate ; sh:value "1971-07-07"^^xsd:date ; sh:sourceConstraintComponent sh:ClosedConstraintComponent ; sh:sourceShape sh:PersonShape ; ] ] . ``` -------------------------------- ### SHACL ASK-based Validator Example Source: https://www.w3.org/TR/shacl/index Declares a constraint component using a SPARQL ASK query for individual value node testing. It defines a parameter for the language tag and uses `sh:SPARQLAskValidator`. ```turtle ex:LanguageConstraintComponentUsingASK a sh:ConstraintComponent ; rdfs:label "Language constraint component" ; sh:parameter [ sh:path ex:lang ; sh:datatype xsd:string ; sh:minLength 2 ; sh:name "language" ; sh:description "The language tag, e.g. \"de\"." ; ] ; sh:labelTemplate "Values are literals with language \"{$lang}\"" ; sh:validator ex:hasLang . ex:hasLang a sh:SPARQLAskValidator ; sh:message "Values are literals with language \"{?lang}\"" ; sh:ask """ ASK { FILTER (isLiteral($value) && langMatches(lang($value), $lang)) } """ . ``` -------------------------------- ### SHACL Expression Constraint Example Source: https://www.w3.org/TR/shacl/-af An example of a SHACL expression constraint that uses SHACL functions to verify a condition. It checks if the combined length of first and last names is less than 30. ```SHACL ex:FilterExampleShape a sh:NodeShape ; sh:expression [ ex:lessThan ( [ ex:strlen ( [ ex:concat ( [ sh:path ex:firstName] [ sh:path ex:lastName ] ) ] ) ] 30 ); ] . ``` -------------------------------- ### SHACL Shapes Graph Example Source: https://www.w3.org/TR/shacl/-js This snippet shows an example of a shapes graph representation using Turtle syntax, as used within SHACL documents. It defines a simple triple pattern. ```turtle # This box represents a shapes graph

. ``` -------------------------------- ### SHACL Path Expression Examples Source: https://www.w3.org/TR/shacl/-af Illustrates SHACL path expressions used to access properties, comparable to SPARQL basic graph patterns. One example shows direct property access, while another uses sh:nodes to specify a target for the path. ```SHACL [ sh:path ex:firstName ] . ``` ```SPARQL { $this ex:firstName ?result . } ``` ```SHACL [ sh:nodes ex:children ; sh:path rdfs:label ; ] . ``` ```SPARQL { $this ex:children ?a . ?a rdfs:label ?result . } ``` -------------------------------- ### SHACL Validation Report for Example Source: https://www.w3.org/TR/shacl/-js This RDF describes the outcome of applying the SHACL shape to the example data. It shows a validation report indicating a violation for 'ex:InvalidCountry'. ```turtle [ a sh:ValidationReport ; sh:conforms false ; sh:result [ a sh:ValidationResult ; sh:resultSeverity sh:Violation ; sh:focusNode ex:InvalidCountry ; sh:resultMessage "Values are literals with German language tag." ; sh:resultPath ex:germanLabel ; sh:value "Spain"@en ; sh:sourceConstraint _:b1 ; sh:sourceConstraintComponent sh:JSConstraintComponent ; sh:sourceShape ex:LanguageExampleShape ; ] ] . ``` -------------------------------- ### Example SPARQL-based Target Declaration (Turtle) Source: https://www.w3.org/TR/shacl/-af This example demonstrates a SHACL node shape utilizing a SPARQL-based target to identify all individuals born in the USA. It defines prefixes and a SELECT query to retrieve individuals who are instances of 'ex:Person' and have 'ex:bornIn' property pointing to 'ex:USA'. ```turtle ex: sh:declare [ sh:prefix "ex" ; sh:namespace ; ] . ex:USCitizenShape a sh:NodeShape ; sh:target [ a sh:SPARQLTarget ; sh:prefixes ex: ; sh:select """ SELECT ?this WHERE { ?this a ex:Person . ?this ex:bornIn ex:USA . } """ ; ] ; ... ``` -------------------------------- ### SHACL sh:datatype Constraint - Data Example Source: https://www.w3.org/TR/shacl/index Example RDF data demonstrating the sh:datatype constraint. It includes a correctly typed literal, a string literal without a datatype (which might fail if the constraint requires a specific datatype), and a literal with a compatible but not identical datatype (xsd:int vs xsd:integer). ```turtle ex:Alice ex:age "23"^^xsd:integer . ex:Bob ex:age "twenty two" . ex:Carol ex:age "23"^^xsd:int . ``` -------------------------------- ### SHACL Shapes Graph Example Source: https://www.w3.org/TR/shacl/index This SHACL shapes graph defines constraints for the 'ex:Person' class, including rules for the 'ex:ssn' and 'ex:worksFor' properties, as well as closed world assumptions. ```turtle ex:PersonShape a sh:NodeShape ; sh:targetClass ex:Person ; sh:property [ sh:path ex:ssn ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:pattern "^\\d{3}-\\d{2}-\\d{4}$" ; ] ; sh:property [ sh:path ex:worksFor ; sh:class ex:Company ; sh:nodeKind sh:IRI ; ] ; sh:closed true ; sh:ignoredProperties ( rdf:type ) . ``` -------------------------------- ### SHACL sh:nodeKind Constraint - Data Example Source: https://www.w3.org/TR/shacl/index Example RDF data showing the use of the 'ex:knows' property. One instance correctly uses an IRI ('ex:Alice'), while another uses a literal ('"Bob"'), which would violate the sh:nodeKind sh:IRI constraint. ```turtle ex:Bob ex:knows ex:Alice . ex:Alice ex:knows "Bob" . ``` -------------------------------- ### SHACL sh:equals Constraint Example Source: https://www.w3.org/TR/shacl/index This SHACL property shape demonstrates the use of sh:equals to ensure that the set of values for ex:firstName is identical to the set of values for ex:givenName for the focus node ex:Bob. The provided example data shows matching values for both properties. ```turtle ex:EqualExampleShape a sh:NodeShape ; sh:targetNode ex:Bob ; sh:property [ sh:path ex:firstName ; sh:equals ex:givenName ; ] . ex:Bob ex:firstName "Bob" ; ex:givenName "Bob" . ``` -------------------------------- ### Example SHACL Validation Report (Conforms) Source: https://www.w3.org/TR/shacl/index This RDF graph represents a successful SHACL validation report. It indicates that the data graph conforms to the shapes graph, with 'sh:conforms' set to 'true'. ```turtle [ a sh:ValidationReport ; sh:conforms true ; ] . ``` -------------------------------- ### SHACL Data Graph Example Source: https://www.w3.org/TR/shacl/-js This snippet illustrates the representation of a data graph using Turtle syntax, which is the data SHACL shapes are validated against. ```turtle # This box represents a data graph. ``` -------------------------------- ### SHACL SELECT-based Validator Example Source: https://www.w3.org/TR/shacl/index Declares a constraint component using a SPARQL SELECT query to validate literal values based on language tags. It uses `sh:SPARQLSelectValidator` and defines a parameter for the language. ```turtle ex:LanguageConstraintComponentUsingSELECT a sh:ConstraintComponent ; rdfs:label "Language constraint component" ; sh:parameter [ sh:path ex:lang ; sh:datatype xsd:string ; sh:minLength 2 ; sh:name "language" ; sh:description "The language tag, e.g. \"de\"." ; ] ; sh:labelTemplate "Values are literals with language \"{$lang}\"" ; sh:propertyValidator [ a sh:SPARQLSelectValidator ; sh:message "Values are literals with language \"{?lang}\"" ; sh:select """ SELECT DISTINCT $this ?value WHERE { $this $PATH ?value . FILTER (!isLiteral(?value) || !langMatches(lang(?value), $lang)) } """ ] . ``` -------------------------------- ### Illustrate SHACL Property Shape Syntax Variations Source: https://www.w3.org/TR/shacl/index Provides examples of SHACL property shapes, showcasing different ways to define constraints on properties. This includes using simple IRIs, sequences of IRIs, and defining shapes with descriptions and minimum counts. ```N-Triples ex:ExampleNodeShapeWithPropertyShapes a sh:NodeShape ; sh:property [ sh:path ex:email ; sh:name "e-mail" ; sh:description "We need at least one email value" ; sh:minCount 1 ; ] ; sh:property [ sh:path (ex:knows ex:email) ; sh:name "Friend's e-mail" ; sh:description "We need at least one email for everyone you know" ; sh:minCount 1 ; ] . ex:ExamplePropertyShape a sh:PropertyShape ; sh:path ex:email ; sh:description "We need at least one email value" ; sh:minCount 1 . ``` -------------------------------- ### SHACL String Length Constraints Example Source: https://www.w3.org/TR/shacl/index An example SHACL shape defining constraints for a 'password' property. It uses both sh:minLength and sh:maxLength to enforce that the password string must be between 8 and 10 characters long. Includes example data demonstrating violations. ```turtle ex:PasswordExampleShape a sh:NodeShape ; sh:targetNode ex:Bob, ex:Alice ; sh:property [ sh:path ex:password ; sh:minLength 8 ; sh:maxLength 10 ; ] . ``` ```turtle ex:Bob ex:password "123456789" . ex:Alice ex:password "1234567890ABC" . ``` -------------------------------- ### Example Data for JavaScript Rule Source: https://www.w3.org/TR/shacl/-js Provides sample data for a rectangle, including its width and height properties, which can be used to test the `computeArea` JavaScript rule. ```turtle ex:ExampleRectangle a ex:Rectangle ; ex:width 7 ; ex:height 8 . ``` -------------------------------- ### SHACL JavaScript Code Block Example Source: https://www.w3.org/TR/shacl/-js This snippet demonstrates the basic syntax for a JavaScript code block within the context of SHACL extensions. It is intended to be executed by SHACL engines that support this JavaScript API. ```javascript // This box contains JavaScript code ``` -------------------------------- ### SHACL Output Results Graph Example Source: https://www.w3.org/TR/shacl/-js This snippet demonstrates the structure of an output results graph, typically used to report the outcomes of SHACL validation. ```turtle # This box represents an output results graph ``` -------------------------------- ### SHACL sh:not Constraint Example Source: https://www.w3.org/TR/shacl/index Demonstrates the use of sh:not to specify that a focus node cannot conform to a given shape. In this example, ex:InvalidInstance1 cannot have any value for ex:property, enforced by sh:minCount 1 within the negated shape. ```turtle ex:NotExampleShape a sh:NodeShape ; sh:targetNode ex:InvalidInstance1 ; sh:not [ a sh:PropertyShape ; sh:path ex:property ; sh:minCount 1 ; ] . ex:InvalidInstance1 ex:property "Some value" . ``` -------------------------------- ### SHACL sh:nodeKind Constraint - Shape Definition Example Source: https://www.w3.org/TR/shacl/index A SHACL shape example that uses sh:nodeKind to enforce that all objects of the 'ex:knows' property must be IRIs. This is useful for ensuring relationships point to identified entities. ```turtle ex:NodeKindExampleShape a sh:NodeShape ; sh:targetObjectsOf ex:knows ; sh:nodeKind sh:IRI . ``` -------------------------------- ### JavaScript Rule Execution Instructions Source: https://www.w3.org/TR/shacl/-js Details the execution process for JavaScript rules. It mandates that specified JavaScript libraries must be loaded first. For each focus node, the designated JavaScript function is invoked, and its return value (if an Array) is processed to infer new triples. ```text Prior to execution, ensure that all JavaScript libraries for the rule (specified via `sh:jsLibrary`) have been executed. For each focus node, execute the JavaScript function specified as the value of `sh:jsFunctionName` at the rule in the shapes graph, using the focus node as the first (and only) argument into the function. If the result `R` of the function call is an Array then for each member `O` of this Array apply the instructions below. 1. **If O is an Array:** Infer a new triple with subject `O[0]`, predicate `O[1]` and object `O[2]`. 2. **If O is another Object:** Infer a new triple with subject `O.subject`, predicate `O.predicate` and object `O.object`. ``` -------------------------------- ### SHACL sh:class Constraint - Shape Definition Example Source: https://www.w3.org/TR/shacl/index An example SHACL shape defining a constraint using sh:class. It targets specific nodes and enforces that values associated with the 'ex:address' path must be of type 'ex:PostalAddress'. ```turtle ex:ClassExampleShape a sh:NodeShape ; sh:targetNode ex:Bob, ex:Alice, ex:Carol ; sh:property [ sh:path ex:address ; sh:class ex:PostalAddress ; ] . ``` -------------------------------- ### Example SHACL Node Expression: Display Labels of Filtered Customers Source: https://www.w3.org/TR/shacl/-af This example declares a node expression that retrieves display labels for customer values conforming to a specific shape. It demonstrates nesting of expressions, using sh:filterShape, sh:nodes, and a SHACL function. ```turtle [ exdisplayLabel ( [ sh:filterShape ex:GoodCustomerShape ; sh:nodes [ sh:path ex:customer ] ; ] ) ] . ``` -------------------------------- ### SHACL Property Shape Configuration Example Source: https://www.w3.org/TR/shacl/index This SHACL snippet demonstrates the use of sh:name, sh:description, sh:order, sh:group, and sh:defaultValue within property shapes. It defines shapes for a person's name and address, organizing them into groups and specifying display order and labels. The `sh:name` property can include language tags for localized labels. ```turtle ex:PersonFormShape a sh:NodeShape ; sh:property [ sh:path ex:firstName ; sh:name "first name" ; sh:description "The person's given name(s)" ; sh:order 0 ; sh:group ex:NameGroup ; ] ; sh:property [ sh:path ex:lastName ; sh:name "last name" ; sh:description "The person's last name" ; sh:order 1 ; sh:group ex:NameGroup ; ] ; sh:property [ sh:path ex:streetAddress ; sh:name "street address" ; sh:description "The street address including number" ; sh:order 11 ; sh:group ex:AddressGroup ; ] ; sh:property [ sh:path ex:locality ; sh:name "locality" ; sh:description "The suburb, city or town of the address" ; sh:order 12 ; sh:group ex:AddressGroup ; ] ; sh:property [ sh:path ex:postalCode ; sh:name "postal code" ; sh:name "zip code"@en-US ; sh:description "The postal code of the locality" ; sh:order 13 ; sh:group ex:AddressGroup ; ] . ex:NameGroup a sh:PropertyGroup ; sh:order 0 ; rdfs:label "Name" . ex:AddressGroup a sh:PropertyGroup ; sh:order 1 ; rdfs:label "Address" . ```