-
Notifications
You must be signed in to change notification settings - Fork 2
/
bookHtml010.html
2890 lines (2825 loc) · 513 KB
/
bookHtml010.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="hevea 2.23">
<link rel="stylesheet" type="text/css" href="book.css"><link rel="stylesheet" type="text/css" href="bookHtml.css">
<title>Shape Expressions</title>
</head>
<body>
<a href="bookHtml009.html"><img src="previous_motif.gif" alt="Previous"></a>
<a href="index.html"><img src="contents_motif.gif" alt="Up"></a>
<a href="bookHtml011.html"><img src="next_motif.gif" alt="Next"></a>
<hr>
<header>
<a href="http://book.validatingrdf.com">Validating RDF data</a>
<img src="cover.jpg"></img>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-112019120-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-112019120-1');
</script>
</header>
<h1 class="chapter" id="sec60">Chapter 4 Shape Expressions</h1>
<p>
<a id="ch040ShEx"></a>
<a id="ch4"></a></p><p><a id="hevea_default273"></a>
<a id="hevea_default274"></a>
<a id="hevea_default275"></a>
<a id="hevea_default276"></a>
<a id="hevea_default277"></a>
<a id="hevea_default278"></a>
Shape Expressions (ShEx) is a schema language for describing RDF graphs structures.
ShEx was originally developed in late 2013 to provide a human-readable syntax for OSLC Resource Shapes.
It added disjunctions, so it was more expressive than Resource Shapes.
Tokens in the language were adopted from Turtle [<a href="bookHtml018.html#EricSemantics2014">80</a>] and
SPARQL [<a href="bookHtml018.html#SPARQL11">44</a>] with tokens for grouping, repetition and wildcards from regular expression and RelaxNG Compact Syntax [<a href="bookHtml018.html#RelaxNG">100</a>].
The language was described in a paper [<a href="bookHtml018.html#EricSemantics2014">80</a>]
and codified in a June 2014 W3C member submission [<a href="bookHtml018.html#ShExSubmission14">92</a>] which included a primer and a semantics specification.
This was later deemed “ShEx 1.0”.</p><p><a id="hevea_default279"></a>
The W3C Data Shapes Working group started in September 2014 and quickly coalesced into two groups: the ShEx camp and the SHACL camp.
In 2016, the ShEx camp split from the Data Shapes Working Group to form a ShEx Community Group (CG).
In April of 2017, the ShEx CG released ShEx 2 with a primer, a semantic specification and a test-suite with implementation reports.</p><p>As of publication, the ShEx Community Group was starting work on ShEx 2.1 to add features like value comparison and unique keys.
See the ShEx Homepage <a href="http://shex.io/"><span class="c010">http://shex.io/</span></a> for the state of the art in ShEx.
A collection of ShEx schemas has also been started at <a href="https://github.com/shexSpec/schemas"><span class="c010">https://github.com/shexSpec/schemas</span></a>.</p>
<h2 class="section" id="sec61">4.1 Use of ShEx</h2>
<p>
<a id="ch4.sec1"></a></p><p><a id="hevea_default280"></a>
Strictly speaking, a ShEx schema defines a set of graphs.
This can be used for many purposes, including communicating data structures associated with some process or interface, generating or validating data, or driving user interface generation and navigation.
At the core of all of these use cases is the notion of conformance with schema.
Even one is using ShEx to create forms, the goal is to accept and present data which is valid with respect to a schema.</p><p>ShEx has several serialization formats:</p><ul class="itemize"><li class="li-itemize"><p><a id="hevea_default281"></a>
</p>a concise, human-readable compact syntax (ShExC);<p><a id="hevea_default282"></a> <a id="hevea_default283"></a>
</p></li><li class="li-itemize">a JSON-LD syntax (ShExJ) which serves as an abstract syntax; and <p><a id="hevea_default284"></a>
</p></li><li class="li-itemize">an RDF representation (ShExR) derived from the JSON-LD syntax.</li></ul><p>These are all isomorphic and most implementations can map from one to another.</p><p>Tools that derive schemas by inspection or translate them from other schema languages typically generate ShExJ.
Interactions with users, e.g., in specifications are almost always in the compact syntax ShExC.
<a id="hevea_default285"></a>
As a practical example, in HL7 FHIR, ShExJ schemas are automatically generated from other formats,
and presented to the end user using compact syntax.
See Section <a href="bookHtml012.html#ch070.FHIRRDFDevelopment">6.2.3</a> for more details. </p><p><a id="hevea_default286"></a>
ShExR allows to use RDF tools to manage schemas, e.g.,
doing a SPARQL query to find out whether an organization is using
<code><span class="c004">dc</span></code><code>:</code><code><span class="c006">creator</span></code> with a string,
a
<code><span class="c004">foaf</span></code><code>:</code><code><span class="c006">Person</span></code>, or even whether an organization is consistent about it.</p>
<h2 class="section" id="sec62">4.2 First Example</h2>
<p>
<a id="ch4.sec2"></a></p><p>Example <a href="#ch040%3AShExSimpleExample">26</a> below contains a very simple ShEx schema.
<a id="hevea_default287"></a>
<a id="hevea_default288"></a>
<a id="hevea_default289"></a> </p><ul class="itemize"><li class="li-itemize">The first three lines declare prefixes using the same syntax as SPARQL Turtle. </li><li class="li-itemize">Nest line defines a shape called
<code>:</code><code><span class="c006">User</span></code>. Nodes with that shape must satisfy the following constraints on their properties.</li><li class="li-itemize">They must have exactly one value for property
<code><span class="c004">schema</span></code><code>:</code><code><span class="c006">name</span></code> which must be a
<code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">string</span></code>.</li><li class="li-itemize">They can have an optional property
<code><span class="c004">schema</span></code><code>:</code><code><span class="c006">birthDate</span></code> with type
<code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">date</span></code>.</li><li class="li-itemize">They must have exactly one property
<code><span class="c004">schema</span></code><code>:</code><code><span class="c006">gender</span></code> whose value is
<code><span class="c004">schema</span></code><code>:</code><code><span class="c006">Male</span></code> or
<code><span class="c004">schema</span></code><code>:</code><code><span class="c006">Female</span></code> or some string.</li><li class="li-itemize">They can have zero or more properties
<code><span class="c004">schema</span></code><code>:</code><code><span class="c006">knows</span></code> whose value must be an IRI and conform to the
<code>:</code><code><span class="c006">User</span></code> shape.</li></ul><div class="example"><div class="theorem"><span class="c013">Example 26</span> <em>Simple ShEx Schema</em><em>
</em><a id="ch040:ShExSimpleExample"></a><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011"><span class="c003">PREFIX</span></span></em><em><span class="c011"> : </span></em><em><span class="c011"><span class="c008"><em><span class="c011"><http://example.org/></span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"><span class="c003">PREFIX</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">: </span></em><em><span class="c011"><span class="c008"><em><span class="c011"><http://schema.org/></span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"><span class="c003">PREFIX</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">: </span></em><em><span class="c011"><span class="c008"><em><span class="c011"><http://www.w3.org/2001/XMLSchema#></span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">date</span></span></em><em><span class="c011">? ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011"> [ </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Male</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Female</span></span></em><em><span class="c011"> ] </span></em><em><span class="c011"><span class="c003">OR</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">knows</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">IRI</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011">*</span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><p><em>All the nodes in the following RDF graph conform to </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> shape.</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alice</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Alice"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Female</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">knows</span></span></em><em><span class="c011"> :</span></em><em><span class="c011"><span class="c006">bob</span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">bob</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Male</span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Robert"</span></em></span></span></em><em><span class="c011">;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"1980-03-10"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">date</span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">carol</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Carol"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"unspecified"</span></em></span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">foaf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Carol"</span></em></span></span></em><em><span class="c011"> .</span></em></td></tr>
</table><p><em>The nodes </em><em>
</em><code><em>:</em></code><code><em><span class="c006">alice</span></em></code><em>, </em><em>
</em><code><em>:</em></code><code><em><span class="c006">bob</span></em></code><em> and </em><em>
</em><code><em>:</em></code><code><em><span class="c006">carol</span></em></code><em> have shape </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em>.</em></p><p><a href="https://goo.gl/P1hQzN"><em>Try example in Shaclex</em></a></p><ul class="itemize"><li class="li-itemize"><em>
</em><code><em>:</em></code><code><em><span class="c006">alice</span></em></code><em> conforms because it contains </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em> and </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">gender</span></em></code><em> with their corresponding values.
It does not contain the property </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">birthDate</span></em></code><em> but that property is optional, as indicated by ‘</em><em>
</em><code><em>?</em></code><em>‘.
It also has the property </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">knows</span></em></code><em> with the value </em><em>
</em><code><em>:</em></code><code><em><span class="c006">bob</span></em></code><em> which has </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> shape.</em></li><li class="li-itemize"><em>
</em><code><em>:</em></code><code><em><span class="c006">bob</span></em></code><em> conforms because it contains the properties and values of the </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> shape.
Note that the order in which triples are expressed in the example does not matter. These are parsed into an RDF graph and RDF graphs are unordered collections of triples.</em></li><li class="li-itemize"><em>
</em><code><em>:</em></code><code><em><span class="c006">carol</span></em></code><em> conforms because it has property </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em> with a </em><em>
</em><code><em><span class="c004">xsd</span></em></code><code><em>:</em></code><code><em><span class="c006">string</span></em></code><em> value,
</em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">gender</span></em></code><em> with another </em><em>
</em><code><em><span class="c004">xsd</span></em></code><code><em>:</em></code><code><em><span class="c006">string</span></em></code><em> value and an extra property
</em><em>
</em><code><em><span class="c004">foaf</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em>.</em><p><em>Notice that </em><em>
</em><code><em>:</em></code><code><em><span class="c006">carol</span></em></code><em> conforms even if it has other properties apart of those mentioned by the </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> shape definition (in this case </em><em>
</em><code><em><span class="c004">foaf</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em>).</em></p><p><em>ShEx shapes are open by default, which means that they constrain neither the existence nor the value of the properties not mentioned in the shape.
This behavior can be modified using the </em><em>
</em><code><em><span class="c006">CLOSED</span></em></code><em> qualifier as we will explain in Section </em><a href="#ch040%3ASecClosedShapes"><em>4.6.8</em></a><em>.</em></p></li></ul><p><em>Given the following RDF graph:</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">dave</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Dave"</span></em></span></span></em><em><span class="c011">; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"XYY"</span></em></span></span></em><em><span class="c011">; </span></em><em><span class="c011"><span class="c008"><em><span class="c011">#</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> 1980 . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># 1980 is not an xsd:date *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">emily</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Emily"</span></em></span></span></em><em><span class="c011">, </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Emilee"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Female</span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># too many schema:names *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">frank</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">foaf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Frank"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011">: </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Male</span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># missing schema:name *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">grace</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Grace"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Male</span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># </span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">knows</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c006">_</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">x</span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># \_:x is not an IRI *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">harold</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Harold"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gender</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Male</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">knows</span></span></em><em><span class="c011"> :</span></em><em><span class="c011"><span class="c006">grace</span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># :grace does not conform to :User *)</span></em></span></span></em></td></tr>
</table><p><em>If we try to validate the nodes in the following graph against the shape </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em>, the validator would fail for all of the nodes:</em></p><ul class="itemize"><li class="li-itemize"><em>
</em><code><em>:</em></code><code><em><span class="c006">dave</span></em></code><em> fails because the value of </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">birthDate</span></em></code><em> is </em><em>
</em><code><em>1980</em></code><em> (an integer) which is not an </em><em>
</em><code><em><span class="c004">xsd</span></em></code><code><em>:</em></code><code><em><span class="c006">date</span></em></code><em>.</em></li><li class="li-itemize"><em>
</em><code><em>:</em></code><code><em><span class="c006">emily</span></em></code><em> fails because it has two values for property </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em>.
Unless otherwise specified, the default cardinality is “exactly one” (which can also be written as “</em><em>
</em><code><em>{1}</em></code><em>” or “</em><em>
</em><code><em>{1,1}</em></code><em>”).</em></li><li class="li-itemize"><em>
</em><code><em>:</em></code><code><em><span class="c006">frank</span></em></code><em> fails because it does not have the property </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em>.</em></li><li class="li-itemize"><em>
</em><code><em>:</em></code><code><em><span class="c006">grace</span></em></code><em> fails because the value of </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">knows</span></em></code><em> is a blank node and there is a node constraint saying that it must be an IRI.</em></li><li class="li-itemize"><em>
</em><code><em>:</em></code><code><em><span class="c006">harold</span></em></code><em> fails because the value of </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">knows</span></em></code><em> is </em><em>
</em><code><em>:</em></code><code><em><span class="c006">grace</span></em></code><em> and </em><em>
</em><code><em>:</em></code><code><em><span class="c006">grace</span></em></code><em> does not conform to the </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> shape.</em></li></ul><p><a href="https://goo.gl/PJ1bDB"><em>Try example in Shaclex</em></a></p></div></div>
<h2 class="section" id="sec63">4.3 ShEx implementations</h2>
<p> <a id="Ch04ShexImplementations"></a></p><p>At the time of this writing, we are aware of the following implementations of ShEx.</p><ul class="itemize"><li class="li-itemize"><p><a id="hevea_default290"></a>
<a id="hevea_default291"></a>
</p>shex.js for Javascript/N3.js (Eric Prud’hommeaux) <a href="https://github.com/shexSpec/shex.js/"><span class="c010">https://github.com/shexSpec/shex.js/</span></a>;<p><a id="hevea_default292"></a>
<a id="hevea_default293"></a>
<a id="hevea_default294"></a>
</p></li><li class="li-itemize">Shaclex for Scala/Jena (Jose Emilio Labra Gayo) <a href="https://github.com/labra/shaclex/"><span class="c010">https://github.com/labra/shaclex/</span></a>;<p><a id="hevea_default295"></a>
<a id="hevea_default296"></a>
</p></li><li class="li-itemize">shex.rb for Ruby/RDF.rb (Gregg Kellogg) <a href="https://github.com/ruby-rdf/shex"><span class="c010">https://github.com/ruby-rdf/shex</span></a>;<p><a id="hevea_default297"></a>
<a id="hevea_default298"></a>
</p></li><li class="li-itemize">Java ShEx for Java/Jena (Iovka Boneva/University of Lille) <a href="https://gforge.inria.fr/projects/shex-impl/"><span class="c010">https://gforge.inria.fr/projects/shex-impl/</span></a>; and<p><a id="hevea_default299"></a>
</p></li><li class="li-itemize">ShExkell for Haskell (Sergio Iván Franco and Weso Research Group) <a href="https://github.com/weso/shexkell"><span class="c010">https://github.com/weso/shexkell</span></a>.</li></ul><p>There are also several online demos and tools that can be used to experiment with ShEx.</p><ul class="itemize"><li class="li-itemize">shex.js (<a href="http://rawgit.com/shexSpec/shex.js/master/doc/shex-simple.html"><span class="c010">http://rawgit.com/shexSpec/shex.js/master/doc/shex-simple.html</span></a>);</li><li class="li-itemize">Shaclex (<a href="http://shaclex.herokuapp.com"><span class="c010">http://shaclex.herokuapp.com</span></a>); and </li><li class="li-itemize">ShExValidata (for ShEx 1.0) (<a href="https://www.w3.org/2015/03/ShExValidata/"><span class="c010">https://www.w3.org/2015/03/ShExValidata/</span></a>).
</li></ul>
<h2 class="section" id="sec64">4.4 The Shape Expressions Language</h2>
<p>
<a id="ch040ShapeExpressions"></a>
<a id="ch4.sec3"></a></p>
<h3 class="subsection" id="sec65">4.4.1 Shape Expressions Compact Syntax</h3>
<p>
<a id="ch040ShExC"></a>
<a id="ch4.sec3.1"></a></p><p><a id="hevea_default300"></a>
<a id="hevea_default301"></a>
<a id="hevea_default302"></a>
<a id="hevea_default303"></a>
The ShEx compact syntax (ShExC) was designed to be read and edited by humans.
It follows some conventions which are similar to Turtle or SPARQL. </p><ul class="itemize"><li class="li-itemize"><p><a id="hevea_default304"></a>
<a id="hevea_default305"></a>
</p>
<code><span class="c006">PREFIX</span></code> and
<code><span class="c006">BASE</span></code> declarations follow the same convention as in Turtle.
In the rest of this chapter we will omit prefix declarations for brevity.<p><a id="hevea_default306"></a>
</p></li><li class="li-itemize">Comments start with a
<code><span class="c008"><code>#</code></span></code> and continue until the end of line.
<p><a id="hevea_default307"></a> <a id="hevea_default308"></a>
</p></li><li class="li-itemize">The keyword
<code><span class="c003">a</span></code> identifies the
<code><span class="c004">rdf</span></code><code>:</code><code><span class="c006">type</span></code> property.<p><a id="hevea_default309"></a> <a id="hevea_default310"></a>
</p></li><li class="li-itemize">Relative and absolute IRIs are enclosed by
<code><</code>
<code>></code> and prefixed names (a shorter way to write out IRIs) are written with prefix followed by a colon “:”.<p><a id="hevea_default311"></a>
</p></li><li class="li-itemize">Blank nodes are identified using
<code><span class="c006">_</span></code><code>:</code><code><span class="c006">label</span></code> notation.<p><a id="hevea_default312"></a> <a id="hevea_default313"></a>
</p></li><li class="li-itemize">Literals can be enclosed by the same quotation conventions (
<code><code>'</code></code>,
<code><code>"</code></code>,
<code><code>''</code></code><code><code>'</code></code>,
<code><code>""</code></code><code><code>"</code></code>) as in Turtle.</li><li class="li-itemize">Keywords (apart from
<code><span class="c003">a</span></code>) are not case sensitive.
Which means that
<code><span class="c006">MinInclusive</span></code> is the same as
<code><span class="c006">MININCLUSIVE</span></code>.
</li></ul><p><a id="hevea_default314"></a>
A ShExC document declares a ShEx schema.
A ShEx schema is a set of labeled shape expressions which are composed of node constraints and shapes.
These constrain the permissible values or graph structure around a node in an RDF graph.
<a id="hevea_default315"></a>
When we are considering a specific node, we call that node the
<code><span class="c006">focus</span></code><code> </code><code><span class="c006">node</span></code>.</p><p><a id="hevea_default316"></a> <a id="hevea_default317"></a> <a id="hevea_default318"></a>
The triples which have the focus node as a subject are called
<code><span class="c006">outgoing</span></code><code> </code><code><span class="c006">arcs</span></code>; those with the focus node as an object are called
<code><span class="c006">incoming</span></code><code> </code><code><span class="c006">arcs</span></code>.
(Typical RDF idioms call for constraints on outgoing arcs much more frequently than on incoming arcs.)
Together, the incoming and outgoing arcs are called the
<code><span class="c006">neighborhood</span></code> of that node.</p><p>Shape expression labels can be IRIs or blank nodes but only IRI labels can be referenced from outside the schema.
In the previous Example <a href="#ch040%3AShExSimpleExample">26</a>,
<code>:</code><code><span class="c006">User</span></code> is an IRI label.</p><p><a id="hevea_default319"></a>
Node constraints declare the shape of a focus node without looking at the arcs.
<a id="hevea_default320"></a>
<a id="hevea_default321"></a>
<a id="hevea_default322"></a>
<a id="hevea_default323"></a>
They can declare the kind of node (IRI, blank node or literal),
the datatype in case of literals,
describe it with XML Schema facets (e.g., min and max numeric values, string lengths, number of digits),
or enumerate a value set.
Figure <a href="#ch040FigureNodeConstraints">4.4.1</a> signals the node constraints that appear in Example <a href="#ch040%3AShExSimpleExample">26</a> which are:
<code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">string</span></code> and
<code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">date</span></code> (datatype constraints),
<code>[</code><code><span class="c004">schema</span></code><code>:</code><code><span class="c006">Male</span></code><code> </code><code><span class="c004">schema</span></code><code>:</code><code><span class="c006">Female</span></code><code>]</code> (a value set),
<code><span class="c006">IRI</span></code> (a node kind declaration) and
<code><span class="c006">@</span></code><code>:</code><code><span class="c006">User</span></code> (a value shape).
Node constraints will be described in more detail in Section <a href="#ch040NodeConstraints">4.5</a>.</p><blockquote class="figure"><div class="center"><hr class="c021"></div>
<div class="center">
<img src="ShEx_NodeConstraints.png">
</div>
<a id="ch040FigureNodeConstraints"></a>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Figure 4.1: Node constraints in a shape.</td></tr>
</table></div>
<div class="center"><hr class="c021"></div></blockquote><p><a id="hevea_default324"></a>
<a id="hevea_default325"></a>
<a id="hevea_default326"></a>
<a id="hevea_default327"></a>
<a id="hevea_default328"></a>
Triple constraints define the triples that appear in the neighborhood of a focus node.
They usually contain a property (or inverse property), a node constraint, and a cardinality declaration which is one by default.</p><p>For example,
<code><span class="c004">schema</span></code><code>:</code><code><span class="c006">name</span></code><code> </code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">string</span></code> is a triple constraint.
The
<code>:</code><code><span class="c006">User</span></code> shape from Example <a href="#ch040%3AShExSimpleExample">26</a>
was formed by four triple constraints.
Triple constraints will be described later in Section <a href="#ch040tripleconstraints">4.6.1</a>.</p><blockquote class="figure"><div class="center"><hr class="c021"></div>
<div class="center">
<img src="ShEx_TripleConstraints.png">
</div>
<a id="ch040FigureTripleConstraints"></a>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Figure 4.2: Triple constraints in a shape.</td></tr>
</table></div>
<div class="center"><hr class="c021"></div></blockquote><p><a id="hevea_default329"></a>
Triple constraints can be grouped using the semicolon operator
<code>;</code> to form
triple expressions.<sup><a id="text8" href="#note8">1</a></sup>
<a id="hevea_default330"></a>
<a id="hevea_default331"></a>
Shapes are enclosed by curly braces
<code>{</code>
<code>}</code> and contain triple expressions. </p><p><a id="hevea_default332"></a>
<a id="hevea_default333"></a>
<a id="hevea_default334"></a>
Shapes are the basic form of shape expressions, although more complex shape expressions can be formed by combining the logical operators
<code><span class="c006">AND</span></code>,
<code><span class="c006">OR</span></code> and
<code><span class="c006">NOT</span></code> which will be later described in Section <a href="#ch040.LogicalOperatorsShEx">4.6</a>.
Shape expressions are identified by shape expression labels.</p><blockquote class="figure"><div class="center"><hr class="c021"></div>
<div class="center">
<img src="ShEx_Shape.png">
</div>
<a id="ch040FigureShape"></a>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Figure 4.3: Shapes, shape expression labels and triple expressions.</td></tr>
</table></div>
<div class="center"><hr class="c021"></div></blockquote><p>Figure <a href="#ch040FigureShapeExpression">4.4.1</a> shows a compound shape expression formed by combining the shape reference
<code><span class="c006">@</span></code><code>:</code><code><span class="c006">User</span></code>
with a shape that contains a single triple constraint
<code>:</code><code><span class="c006">teaches</span></code><code> </code><code><span class="c006">@</span></code><code>:</code><code><span class="c006">Course</span></code> using the
<code><span class="c006">AND</span></code> operator.</p><p><a id="hevea_default335"></a>
The full ShEx BNF grammar is specified at <a href="http://shex.io/shex-semantics/\#shexc"><span class="c010">http://shex.io/shex-semantics/\#shexc</span></a>.</p><blockquote class="figure"><div class="center"><hr class="c021"></div>
<div class="center">
<img src="ShEx_ShapeExpressions.png">
</div>
<a id="ch040FigureShapeExpression"></a>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Figure 4.4: Shape expression and shape.</td></tr>
</table></div>
<div class="center"><hr class="c021"></div></blockquote>
<h3 class="subsection" id="sec66">4.4.2 Invoking Validation</h3>
<p>
<a id="ch040ValidationProcess"></a>
<a id="ch4.sec3.2"></a></p><p><a id="hevea_default336"></a>
In Example <a href="#ch040%3AShExSimpleExample">26</a>, we tested several RDF nodes (
<code>:</code><code><span class="c006">alice</span></code>,
<code>:</code><code><span class="c006">bob</span></code>, ...
<code>:</code><code><span class="c006">harold</span></code>) against the shape
<code>:</code><code><span class="c006">User</span></code>.</p><p>ShEx validation takes as input a schema, an RDF graph, and a shape map, and returns another shape map.</p><p><a id="hevea_default337"></a>
The input shape map (called <em>fixed shape map</em>) contains a list of
<code><span class="c006">nodeSelector@shapeLabel</span></code> associations separated by commas,
where
<code><span class="c006">nodeSelector</span></code> is an RDF node and
<code><span class="c006">shapeLabel</span></code> is a shape label.
Both use N-Triples notation.</p><p>A fixed map would look like:</p><table class="lstframe c014"><tr><td class="lstlisting"><span class="c011"><span class="c008"><span class="c011"><http://data.example/#alice></span></span></span><span class="c011"><span class="c006">@</span></span><span class="c011"><span class="c008"><span class="c011"><http://schema.example/#User></span></span></span><span class="c011">,</span><span class="c011">
</span><span class="c011"><span class="c008"><span class="c011"><http://data.example/#bob></span></span></span><span class="c011"><span class="c006">@</span></span><span class="c011"><span class="c008"><span class="c011"><http://schema.example/#User></span></span></span></td></tr>
</table><p>Although shape maps use absolute IRIs for RDF nodes and shape labels,
we will use prefixes to abbreviate them in our listings:</p><table class="lstframe c014"><tr><td class="lstlisting"><span class="c011">:</span><span class="c011"><span class="c006">alice@</span></span><span class="c011">:</span><span class="c011"><span class="c006">User</span></span><span class="c011">,</span><span class="c011">
</span><span class="c011">:</span><span class="c011"><span class="c006">bob@User</span></span></td></tr>
</table><p>Note that during evaluation, the processor may need to check the conformance of other nodes against other shapes. </p><div class="example"><div class="theorem"><span class="c013">Example 27</span> <em>Invoking validation example</em><p><em>If we define the following schema:</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">knows</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011">*</span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><p><em>and the RDF graph:</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alice</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Alice"</span></em></span></span></em><em><span class="c011">;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">knows</span></span></em><em><span class="c011"> :</span></em><em><span class="c011"><span class="c006">carol</span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">bob</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Robert"</span></em></span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">carol</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Carol"</span></em></span></span></em><em><span class="c011"> .</span></em></td></tr>
</table><p><em>when we invoke a ShEx processor with the fixed shape map: </em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alice@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">bob@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em></td></tr>
</table><p><em>the result shape map is:</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alice@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">bob@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">carol@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em></td></tr>
</table><p><em>The reason is that in order to check that </em><em>
</em><code><em>:</em></code><code><em><span class="c006">alice</span></em></code><em> conforms to </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em>, the processor must check that </em><em>
</em><code><em>:</em></code><code><em><span class="c006">carol</span></em></code><em> also conforms to </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> and hence, it adds the association </em><em>
</em><code><em>:</em></code><code><em><span class="c006">carol@</span></em></code><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> to the result shape map.</em></p><p><em>Figure </em><a href="#ch040.ShExValidatorProcess"><em>4.5</em></a><em> depicts the validation process.</em></p><blockquote class="figure"><div class="center"><hr class="c021"></div><em>
</em><div class="center"><em>
<img src="ShExValidator.png">
</em></div><em>
</em><div class="caption"><table class="c002 cellpading0"><tr><td class="c018"><em>Figure 4.5: Validation process which accepts a fixed shape map and
emits a result shape map.</em></td></tr>
</table></div><em>
</em><a id="ch040.ShExValidatorProcess"></a><em>
</em><div class="center"><hr class="c021"></div></blockquote><p><a href="https://goo.gl/tsLK7g"><em>Try example in Shaclex</em></a></p></div></div><p>There are many use case-dependent ways to compose a fixed shape map.
<a id="hevea_default338"></a> <a id="hevea_default339"></a>
ShEx defines a common one called <em>query shape map</em> which uses triple patterns to select nodes.
Triple patterns use curly braces and three values that represent the subject, predicate and object of a triple.
They can contain the value
<code><span class="c006">FOCUS</span></code> to identify the node we want to select and
<code><span class="c006">_</span></code> to indicate that we do not constrain some value.</p><div class="example"><div class="theorem"><span class="c013">Example 28</span> <em>Query map example</em><p><em>The following query map selects all subjects of </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em>, all objects of </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">knows</span></em></code><em> and nodes that have </em><em>
</em><code><em><span class="c004">rdf</span></em></code><code><em>:</em></code><code><em><span class="c006">type</span></em></code><em> with value </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">Person</span></em></code><em>.</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">{</span></em><em><span class="c011"><span class="c006">FOCUS</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c006">_</span></span></em><em><span class="c011">}</span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011">{</span></em><em><span class="c011"><span class="c006">_</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">knows</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c006">FOCUS</span></span></em><em><span class="c011">}</span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011">{</span></em><em><span class="c011"><span class="c006">FOCUS</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">rdf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">type</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Person</span></span></em><em><span class="c011">}</span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em></td></tr>
</table><p><a href="https://goo.gl/avv9DT"><em>Try example in Shaclex</em></a></p></div></div><p>Section <a href="#ch040ShapeMaps">4.9</a> describes fixed shape maps and query shape maps in greater detail.</p><p>In the previous example, validating
<code>:</code><code><span class="c006">alice</span></code> as a
<code>:</code><code><span class="c006">User</span></code> entailed validating
<code>:</code><code><span class="c006">carol</span></code> as a
<code>:</code><code><span class="c006">User</span></code>.
Unless the validation engine has some sort of state persistence,
it would be more efficient to validate once with a shape map like:</p><table class="lstframe c014"><tr><td class="lstlisting"><span class="c011">:</span><span class="c011"><span class="c006">alice@</span></span><span class="c011">:</span><span class="c011"><span class="c006">User</span></span><span class="c011">,:</span><span class="c011"><span class="c006">carol@</span></span><span class="c011">:</span><span class="c011"><span class="c006">User</span></span></td></tr>
</table><p>than to validate
<code>:</code><code><span class="c006">alice</span></code> and
<code>:</code><code><span class="c006">carol</span></code> separately.</p><p>Validating a shape map with multiple node/shape pairs allows the engine to leverage any pairs that it has already tested.</p>
<h3 class="subsection" id="sec67">4.4.3 Structure of Shape Expressions</h3>
<p>
<a id="ch040StructureofaShapeExpression"></a>
<a id="ch4.sec3.3"></a></p><p><a id="hevea_default340"></a> <a id="hevea_default341"></a> <a id="hevea_default342"></a> <a id="hevea_default343"></a>
In Section <a href="#ch040ShExC">4.4.1</a>, we described shape expressions as being composed of node constraints and shapes.
These can also be combined with the logical operators
<code><span class="c006">And</span></code>,
<code><span class="c006">Or</span></code> and
<code><span class="c006">Not</span></code>.
<code><span class="c006">And</span></code> and
<code><span class="c006">Or</span></code> expressions in turn contain two or more shape expressions.
When we refer to a
<code><span class="c006">shape</span></code><code> </code><code><span class="c006">expression</span></code>, we mean one of the following. </p><ul class="itemize"><li class="li-itemize"><p><a id="hevea_default344"></a>
</p>A node constraint, which constrains the set of allowed values of a node. <p><a id="hevea_default345"></a>
</p></li><li class="li-itemize">A shape, which constrains the neighborhood of a node.<p><a id="hevea_default346"></a>
</p></li><li class="li-itemize">An
<code><span class="c006">And</span></code> of two or more shape expressions (called
<code><span class="c006">ShapeAnd</span></code>).<p><a id="hevea_default347"></a>
</p></li><li class="li-itemize">An
<code><span class="c006">Or</span></code> of two or more shape expressions (called
<code><span class="c006">ShapeOr</span></code>).<p><a id="hevea_default348"></a>
</p></li><li class="li-itemize">A
<code><span class="c006">Not</span></code> of one shape expression (called
<code><span class="c006">ShapeNot</span></code>)<p><a id="hevea_default349"></a>
</p></li><li class="li-itemize">An external shape expression.</li></ul><p>This recursive structure forms a tree which has node constraints and shapes as leaves.
Figure <a href="#ch040.ShExModel">4.6</a> represents the ShEx data model.</p><blockquote class="figure"><div class="center"><hr class="c021"></div>
<div class="center">
<img src="ShExModel.png">
</div>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Figure 4.6: ShEx data model.</td></tr>
</table></div>
<a id="ch040.ShExModel"></a>
<div class="center"><hr class="c021"></div></blockquote><p><a id="hevea_default350"></a> <a id="hevea_default351"></a>
Node constraints and shapes are described in the following sections while
the logical operators are discussed in Section <a href="#ch040LogicalOperators">4.8</a> and
external shapes in Section <a href="#ch040.ExternalShapes">4.7.3</a>.</p><p><a id="hevea_default352"></a>
</p>
<h3 class="subsection" id="sec68">4.4.4 Start Shape Expression</h3>
<p>
<a id="ch040startExpr"></a>
<a id="ch4.sec3.4"></a></p><p>The shape expression might be selected by label or it might default to a special shape called the
<code><span class="c003">start</span></code> shape.</p><p><a id="hevea_default353"></a>
A schema can have one more shape expression called the
<code><span class="c003">start</span></code> expression.
This serves as <em>start here</em> advice from the schema author
and is useful when describing a graph with a single purpose.
<a id="hevea_default354"></a>
For instance, the medical data protocol FHIR
(see Section <a href="bookHtml012.html#ch060FHIR">6.2</a>) has specific schemas for resources like
<code><span class="c006">Patient</span></code>.</p><div class="example"><div class="theorem"><span class="c013">Example 29</span> <em>ShEx schema with start directive</em><p><em>Consider the following code:</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011"><span style="color:red">start</span></span></em><em><span class="c011"> = </span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011"><span class="c008"><em><span class="c011"><Patient></span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011"><span class="c008"><em><span class="c011"><Patient></span></em></span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> ...</span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em><em><span class="c011">
</span></em><em><span class="c011">...</span></em></td></tr>
</table><p><em>In the compact syntax, the directive </em><em>
</em><code><em><span class="c003">start</span></em></code><code><em> = </em></code><code><em><span class="c006">@</span></em></code><code><em><</em></code><code><em><span class="c006">Patient</span></em></code><code><em>></em></code><em> declares that the shape expression </em><em>
</em><code><em><</em></code><code><em><span class="c006">Patient</span></em></code><code><em>></em></code><em> will be used by default if a shape is not explicitly provided in the shapes map.</em></p></div></div><p>In shape maps, it is possible to declare that a node must be validated against the shape map by using the keyword
<code><span class="c006">START</span></code>.
For example, the following shape map:</p><table class="lstframe c014"><tr><td class="lstlisting"><span class="c011">:</span><span class="c011"><span class="c006">alice@START</span></span><span class="c011">,</span><span class="c011">
</span><span class="c011">:</span><span class="c011"><span class="c006">bob@</span></span><span class="c011"><span class="c008"><span class="c011"><Doctor></span></span></span></td></tr>
</table><p>would validate
<code>:</code><code><span class="c006">alice</span></code> against the start shape expression (in the previous example, it would be
<code><</code><code><span class="c006">Patient</span></code><code>></code>) and
<code>:</code><code><span class="c006">bob</span></code> against
<code><</code><code><span class="c006">Doctor</span></code><code>></code>.</p>
<h2 class="section" id="sec69">4.5 Node Constraints</h2>
<p>
<a id="ch040NodeConstraints"></a>
<a id="ch4.sec4"></a></p><p>Node constraints describe the allowed values of a node.
<a id="hevea_default355"></a>
<a id="hevea_default356"></a>
<a id="hevea_default357"></a>
<a id="hevea_default358"></a>
<a id="hevea_default359"></a>
These include specification of RDF node kind,
literal datatype, string and numeric facets, and value sets. </p><p>Node constraints can appear as a labeled shape expression or as part of triple constraints.</p><div class="example"><div class="theorem"><span class="c013">Example 30</span> <p><a id="hevea_default360"></a><em>
Any place one does </em>not<em> want a node constraint, can be marked with a period (</em><em>
</em><code><em><code><em>"."</em></code></em></code><em>).
This is analogous to the period which matches any character in regular expressions.
The following example lists the properties that a </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> must have but it does not specify any constraint in their values:</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> . ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alternateName</span></span></em><em><span class="c011"> . * ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> . ?</span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><p><em>Given the following RDF graph: </em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alice</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> 23 . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">bob</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Robert"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alternateName</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Bob"</span></em></span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Bobby"</span></em></span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c008"><em><span class="c011"><Bob></span></em></span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Unknown"</span></em></span></span></em><em><span class="c011"> .</span></em></td></tr>
</table><p><em>If we provide the shape map </em><em>
</em><code><em>:</em></code><code><em><span class="c006">alice@</span></em></code><code><em>:</em></code><code><em><span class="c006">User</span></em></code><code><em>,:</em></code><code><em><span class="c006">bob@</span></em></code><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> the ShEx processor would return that they both conform.</em></p></div></div><p><a id="hevea_default361"></a>
Node constraints usually appear as part of value expressions in triple constraints.</p><div class="example"><div class="theorem"><span class="c013">Example 31</span> <em>Node constraint in a value expression</em><p><em>The following example declares that nodes with shape </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> must have a property </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">url</span></em></code><em> whose value must be an </em><em>
</em><code><em><span class="c006">IRI</span></em></code><em>. </em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">url</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">IRI</span></span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table></div></div><p>Node constraints can also appear as top level shapes. </p><div class="example"><div class="theorem"><span class="c013">Example 32</span> <em>Node constraint as top-level shape</em><p><em>The following code defines two shapes, </em><em>
</em><code><em>:</em></code><code><em><span class="c006">HomePage</span></em></code><em> and </em><em>
</em><code><em>:</em></code><code><em><span class="c006">CanVoteAge</span></em></code><em>,
which are defined as node constraints.
The first one declares that nodes must be </em><em>
</em><code><em><span class="c006">IRI</span></em></code><em>s and the second one that they must be </em><em>
</em><code><em><span class="c004">xsd</span></em></code><code><em>:</em></code><code><em><span class="c006">integer</span></em></code><em> values greater than 18.</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">HomePage</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">IRI</span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">CanVoteAge</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">integer</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">MinInclusive</span></span></em><em><span class="c011"> 18</span></em></td></tr>
</table><p><em>If we provide a ShEx processor the shape map </em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011"><span class="c008"><em><span class="c011"><http://example.org/alice></span></em></span></span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">HomePage</span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011">23</span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">CanVoteAge</span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011">45</span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">HomePage</span></span></em><em><span class="c011">,</span></em><em><span class="c011">
</span></em><em><span class="c011">14</span></em><em><span class="c011"><span class="c006">@</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">CanVoteAge</span></span></em></td></tr>
</table><p><em>The result would be that the first two nodes are conformant while the last
two nodes are non-conformant.</em></p></div></div><p>It is also possible to combine top-level node constraints with more complex shapes.</p><div class="example"><div class="theorem"><span class="c013">Example 33</span> <em>Node constraint as top-level shape combined with complex shape</em><p><em>The following declaration of shape </em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> says that nodes conforming to shape
</em><em>
</em><code><em>:</em></code><code><em><span class="c006">User</span></em></code><em> must be </em><em>
</em><code><em><span class="c006">IRI</span></em></code><em>s and have a property </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em> with an </em><em>
</em><code><em><span class="c004">xsd</span></em></code><code><em>:</em></code><code><em><span class="c006">string</span></em></code><em> value. </em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">IRI</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c006">AND</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><p><a id="hevea_default362"></a><em>
In this case, the external </em><em>
</em><code><em><span class="c006">AND</span></em></code><em> can be omitted, so the previous shape is equivalent to:</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">IRI</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table></div></div><p>Table <a href="#ch040.NodeConstraintsTable">4.1</a> gives an overview of the main types of node constraints with some examples and a short description.</p><blockquote class="table"><div class="center"><hr class="c021"></div>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Table 4.1: Node constraints</td></tr>
</table></div>
<a id="ch040.NodeConstraintsTable"></a>
<div class="center">
<table class="c001 cellpadding1" border=1><tr><td class="c017"> <span class="c013">Name</span></td><td class="c019"><span class="c013">Description</span></td><td class="c019"><span class="c013">Examples
</span></td></tr>
<tr><td class="c017"> Anything</td><td class="c019">The value can be anything</td><td class="c019">
<code>.</code>
</td></tr>
<tr><td class="c017"> Datatype</td><td class="c019">The value must be an element of that datatype</td><td class="c019">
<code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">string</span></code> <br>
<code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">date</span></code> <br>
<code><span class="c004">cdt</span></code><code>:</code><code><span class="c006">distance</span></code> <br>
…</td></tr>
<tr><td class="c017"> Node kind</td><td class="c019">The value must have that kind</td><td class="c019">
<code><span class="c006">IRI</span></code> <br>
<code><span class="c006">BNode</span></code> <br>
<code><span class="c006">Literal</span></code> <br>
<code><span class="c006">NonLiteral</span></code>
</td></tr>
<tr><td class="c017"> Value set</td><td class="c019">The value must be an element of that set</td><td class="c019">
<code>[:</code><code><span class="c006">Male</span></code><code> :</code><code><span class="c006">Female</span></code><code>]</code>
</td></tr>
<tr><td class="c017"> Shape reference</td><td class="c019">The value must conform to
<code><</code><code><span class="c006">User</span></code><code>></code></td><td class="c019">
<code><span class="c006">@</span></code><code>:</code><code><span class="c006">User</span></code>
</td></tr>
</table>
</div>
<div class="center"><hr class="c021"></div></blockquote><p><a id="hevea_default363"></a>
</p>
<h3 class="subsection" id="sec70">4.5.1 Node kinds</h3>
<p>
<a id="ch040NodeKinds"></a>
<a id="ch4.sec4.1"></a></p><p>Node kinds describe the kind that a value must have.
There are four node kinds in ShEx:
<code><span class="c006">Literal</span></code>,
<code><span class="c006">IRI</span></code>,
<code><span class="c006">BNode</span></code>, and
<code><span class="c006">NonLiteral</span></code>
which follow the rules defined in RDF 1.1 for such terms.</p><blockquote class="table"><div class="center"><hr class="c021"></div>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Table 4.2: Node kinds</td></tr>
</table></div>
<a id="ch040.NodeKind"></a>
<div class="center">
<table class="c001 cellpadding1" border=1><tr><td class="c017"> <span class="c013">Value</span></td><td class="c019"><span class="c013">Description</span></td><td class="c019"><span class="c013">Examples
</span></td></tr>
<tr><td class="c017">
<code><span class="c006">Literal</span></code></td><td class="c019">Any RDF literal</td><td class="c019">
<code><code>"Alice"</code></code> <br>
<code><code>"Spain"</code></code><code><span class="c006">@en</span></code> <br>
<code>42</code> <br>
<code><span class="c003">true</span></code>
</td></tr>
<tr><td class="c017">
<code><span class="c006">IRI</span></code></td><td class="c019">Any RDF IRI</td><td class="c019">
<code><</code><code><span class="c006">http</span></code><code>://</code><code><span class="c006">example</span></code><code>.</code><code><span class="c004">org</span></code><code>/</code><code><span class="c006">Alice</span></code><code>></code> <br>
<code><span class="c004">ex</span></code><code>:</code><code><span class="c006">alice</span></code> <br>
<code>:</code><code><span class="c006">bob</span></code>
</td></tr>
<tr><td class="c017">
<code><span class="c006">BNode</span></code></td><td class="c019">Any blank node</td><td class="c019">
<code><span class="c006">_</span></code><code>:</code><code><span class="c006">x</span></code> <br>
<code>[]</code>
</td></tr>
<tr><td class="c017">
<code><span class="c006">NonLiteral</span></code></td><td class="c019">Any IRI or blank node</td><td class="c019">
<code><</code><code><span class="c006">http</span></code><code>://</code><code><span class="c006">example</span></code><code>.</code><code><span class="c004">org</span></code><code>/</code><code><span class="c006">alice</span></code><code>></code> <br>
<code><span class="c006">_</span></code><code>:</code><code><span class="c006">x</span></code>
</td></tr>
</table>
</div><div class="center"><hr class="c021"></div></blockquote><div class="example"><div class="theorem"><span class="c013">Example 34</span> <p><em>The following example declares that the value of property </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em> must be a literal and the value of </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">follows</span></em></code><em> must be an IRI.</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">Literal</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">follows</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">IRI</span></span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alice</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Alice"</span></em></span></span></em><em><span class="c011">; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :User </span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">follows</span></span></em><em><span class="c011"> :</span></em><em><span class="c011"><span class="c006">bob</span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">bob</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> :</span></em><em><span class="c011"><span class="c006">Bob</span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">follows</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c006">_</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">x</span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># :Bob is not a literal and \_:x is not an IRI *)</span></em></span></span></em></td></tr>
</table></div></div><p><a id="hevea_default364"></a>
</p>
<h3 class="subsection" id="sec71">4.5.2 Datatypes</h3>
<p>
<a id="ch040Datatypes"></a>
<a id="ch4.sec4.2"></a></p><p>Like most schema languages, ShEx includes datatype constraints which declare that a focus node must be a literal with
some specific datatype.
ShEx has special support for XML Schema datatypes [<a href="bookHtml018.html#XMLSchemaDatatypes">9</a>]
for which it checks that the lexical form also conforms to the expected datatype.</p><div class="example"><div class="theorem"><span class="c013">Example 35</span> <em>Simple datatypes</em><em> </em><a id="ch040SimpleDatatypes"></a><p><em>The following example declares the datatypes that must have the values of </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">name</span></em></code><em> and </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">birthDate</span></em></code><em> properties.</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">User</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">foaf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">age</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">integer</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">date</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">alice</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Alice"</span></em></span></span></em><em><span class="c011">; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">foaf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">age</span></span></em><em><span class="c011"> 36 ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"1981-07-10"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">date</span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">bob</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Robert"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :User </span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">foaf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">age</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"26"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">integer</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"1981-07-10"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">date</span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">carol</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> :</span></em><em><span class="c011"><span class="c006">Carol</span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">foaf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">age</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"14"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># :Carol is an IRI *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"2003-06-10"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">date</span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># and "14" a string *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">dave</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Dave"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :User</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">foaf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">age</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Unknown"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">integer</span></span></em><em><span class="c011">; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># invalid lexical forms *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">birthDate</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Unknown"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">date</span></span></em><em><span class="c011"> .</span></em></td></tr>
</table><p><a id="hevea_default365"></a><em>
As we said, for XML Schema datatypes,
ShEx also checks that the lexical form matches the expected datatype.
For example, the </em><em>
</em><code><em><span class="c004">foaf</span></em></code><code><em>:</em></code><code><em><span class="c006">age</span></em></code><em> of </em><em>
</em><code><em>:</em></code><code><em><span class="c006">dave</span></em></code><em> is </em><em>
</em><code><em><code><em>"Unknown"</em></code></em></code><code><em>^^</em></code><code><em><span class="c004">xsd</span></em></code><code><em>:</em></code><code><em><span class="c006">integer</span></em></code><em> and although it declares that </em><em>
</em><code><em><code><em>"Unknown"</em></code></em></code><em> is an integer
and some RDF parsers allow those declarations,
</em><em>
</em><code><em><code><em>"Unknown"</em></code></em></code><em> does not have the integer’s lexical form and the ShEx processor will complain.
The same happens for the value of </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">birthDate</span></em></code><em>.</em></p></div></div><div class="example"><div class="theorem"><span class="c013">Example 36</span> <em>Custom datatypes</em><p><a id="hevea_default366"></a><em>
</em><a id="hevea_default367"></a><em>
Although the most common use case is to use XML Schema datatypes,
RDF data can use other datatypes.
In the following example, a picture contains the properties </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">width</span></em></code><em> and </em><em>
</em><code><em><span class="c004">schema</span></em></code><code><em>:</em></code><code><em><span class="c006">height</span></em></code><em> using a hypothetical custom datatype for distances (</em><em>
</em><code><em><span class="c004">cdt</span></em></code><code><em>:</em></code><code><em><span class="c006">distance</span></em></code><em>).</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Picture</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">width</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">cdt</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">distance</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">height</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">cdt</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">distance</span></span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">gioconda</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Mona Lisa"</span></em></span></span></em><em><span class="c011">; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Passes as a :Picture </span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">width</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"21 in"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">cdt</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">distance</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">height</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"30 in"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">cdt</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">distance</span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">other</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Other picture"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># Fails as a :Picture </span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">width</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"21 in"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># expected cdt:distance *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">height</span></span></em><em><span class="c011"> 30 .</span></em></td></tr>
</table></div></div><div class="example"><div class="theorem"><span class="c013">Example 37</span> <em>Language-tagged literals</em><p><a id="hevea_default368"></a><em>
</em><a id="hevea_default369"></a><em>
The datatype </em><em>
</em><code><em><span class="c004">rdf</span></em></code><code><em>:</em></code><code><em><span class="c006">langString</span></em></code><em> identifies language-tagged literals (see </em><em>[</em><a href="bookHtml018.html#RDF11"><em>25, Section 3.3</em></a><em>]</em><em>), i.e., RDF literals that have a language tag.</em></p><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Country</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">rdf</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">langString</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">italy</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Italia"</span></em></span></span></em><em><span class="c011"><span class="c006">@es</span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011">#Passes as a :Country</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">france</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"France"</span></em></span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011">#Fails as a :Country </span></em></span></span></em></td></tr>
</table></div></div>
<h3 class="subsection" id="sec72">4.5.3 Facets on Literals</h3>
<p>
<a id="ch040facets"></a>
<a id="ch4.sec4.3"></a></p><p><a id="hevea_default370"></a>
<a id="hevea_default371"></a>
<a id="hevea_default372"></a>
XML Schema provides a useful library of string and numeric tests called <em>facets</em> [<a href="bookHtml018.html#XMLSchemaDatatypes">9</a>].
These facets are listed in Table <a href="#ch040.Facets">4.3</a> with a sample argument and some passing and failing values.</p><p><a id="hevea_default373"></a>
<a id="hevea_default374"></a>
<a id="hevea_default375"></a>
<a id="hevea_default376"></a>
<a id="hevea_default377"></a>
<a id="hevea_default378"></a>
<a id="hevea_default379"></a>
<a id="hevea_default380"></a>
<a id="hevea_default381"></a></p><blockquote class="table"><div class="center"><hr class="c021"></div>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Table 4.3: Facets on literals</td></tr>
</table></div>
<a id="ch040.Facets"></a>
<div class="center">
<table class="c001 cellpadding1" border=1><tr><td class="c019"> <span class="c013">Facet and <br>
argument</span></td><td class="c019"><span class="c013">Passing values</span></td><td class="c019"><span class="c013">Failing values
</span></td></tr>
<tr><td class="c019">
<code><span class="c006">MinInclusive</span></code><code> 1</code></td><td class="c019">
<code><code>"1"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">decimal</span></code>, <br>
<code>1</code>,
<code>2</code>,
<code>98</code>,
<code>99</code>,
<code>100</code></td><td class="c019">
<code><code>"1"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">string</span></code>, <br>
<code>-1</code>,
<code>0</code>
</td></tr>
<tr><td class="c019">
<code><span class="c006">MinExclusive</span></code><code> 1</code></td><td class="c019">
<code>2</code>,
<code>98</code>,
<code>99</code>,
<code>100</code></td><td class="c019">
<code>-1</code>,
<code>0</code>,
<code>1</code>
</td></tr>
<tr><td class="c019">
<code><span class="c006">MaxInclusive</span></code><code> 99</code></td><td class="c019">
<code>1</code>,
<code>2</code>,
<code>98</code>,
<code>99</code></td><td class="c019">
<code>100</code>
</td></tr>
<tr><td class="c019">
<code><span class="c006">MaxExclusive</span></code><code> 99</code></td><td class="c019">
<code>1</code>,
<code>2</code>,
<code>98</code></td><td class="c019">
<code>99</code>,
<code>100</code>
</td></tr>
<tr><td class="c019">
<code><span class="c006">TotalDigits</span></code><code> 3</code></td><td class="c019">
<code><code>"1"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">integer</span></code>, <br>
<code>9</code>,
<code>999</code>,
<code>0999</code>, <br>
<code>9.99</code>,
<code>99.9</code>,
<code>0.1020</code></td><td class="c019">
<code><code>"1"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">string</span></code>, <br>
<code>1000</code>,
<code>01000</code>, <br>
<code>1.1020</code>,
<code>.1021</code>,
<code>0.1021</code>
</td></tr>
<tr><td class="c019">
<code><span class="c006">FractionDigits</span></code><code> 3</code></td><td class="c019">
<code><code>"1"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">decimal</span></code>, <br>
<code>0.1</code>,
<code>0.1020</code>,
<code>1.1020</code></td><td class="c019">
<code><code>"1"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">integer</span></code>, <br>
<code>0.1021</code>,
<code>0.10212</code>
</td></tr>
<tr><td class="c019">
<code><span class="c006">Length</span></code><code> 3</code></td><td class="c019">
<code><code>"123"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">string</span></code>, <br>
<code><code>"123"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">integer</span></code>, <br>
<code><code>"abc"</code></code></td><td class="c019">
<code><code>"12"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">string</span></code>, <br>
<code><code>"12"</code></code><code>^^</code><code><span class="c004">xsd</span></code><code>:</code><code><span class="c006">integer</span></code>, <br>
<code><code>"ab"</code></code>,
<code><code>"abcd"</code></code>
</td></tr>
<tr><td class="c019">
<code><span class="c006">MinLength</span></code><code> 3</code></td><td class="c019">
<code><code>"abc"</code></code>,
<code><code>"abcd"</code></code></td><td class="c019">
<code><code>""</code></code>,
<code><code>"ab"</code></code>
</td></tr>
<tr><td class="c019">
<code><span class="c006">MaxLength</span></code><code> 3</code></td><td class="c019">
<code><code>""</code></code>,
<code><code>"ab"</code></code>,
<code><code>"abc"</code></code></td><td class="c019">
<code><code>"abcd"</code></code>,
<code><code>"abcde"</code></code>
</td></tr>
<tr><td class="c019">
<code>/^</code><code><span class="c006">ab</span></code><code>+/</code> <br>
Regex pattern</td><td class="c019">
<code><code>"ab"</code></code>,
<code><code>"abb"</code></code>,
<code><code>"abbcd"</code></code></td><td class="c019">
<code><code>""</code></code>,
<code><code>"a"</code></code>,
<code><code>"acd"</code></code>,
<code><code>"cab"</code></code> <br>
<code><code>"AB"</code></code>,
<code><code>"ABB"</code></code>,
<code><code>"ABBCD"</code></code>
</td></tr>
<tr><td class="c019">
<code>/^</code><code><span class="c006">ab</span></code><code>+/</code><code><span class="c006">i</span></code> <br>
Regex pattern <br>
with
<code><span class="c006">i</span></code> flag</td><td class="c019">
<code><code>"ab"</code></code>,
<code><code>"abb"</code></code>,
<code><code>"abbcd"</code></code> <br>
<code><code>"AB"</code></code>,
<code><code>"ABB"</code></code>,
<code><code>"ABBCD"</code></code></td><td class="c019">
<code><code>""</code></code>,
<code><code>"a"</code></code>,
<code><code>"acd"</code></code>
</td></tr>
</table>
</div>
<div class="center"><hr class="c021"></div></blockquote><div class="example"><div class="theorem"><span class="c013">Example 38</span> <em>
</em><em>
</em><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">Product</span></span></em><em><span class="c011"> {</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">string</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">MaxLength</span></span></em><em><span class="c011"> 10 ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">weight</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">decimal</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c003">MinInclusive</span></span></em><em><span class="c011"> 1 </span></em><em><span class="c011"><span class="c003">MaxInclusive</span></span></em><em><span class="c011"> 200 ;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">sku</span></span></em><em><span class="c011"> /^[</span></em><em><span class="c011"><span class="c003">A</span></span></em><em><span class="c011">-</span></em><em><span class="c011"><span class="c006">Z0</span></span></em><em><span class="c011">-9]{10,20}</span></em><em><span class="c011"><span class="c006">$</span></span></em><em><span class="c011">/ ;</span></em><em><span class="c011">
</span></em><em><span class="c011">}</span></em></td></tr>
</table><table class="lstframe c014"><tr><td class="lstlisting"><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">product1</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Product 1"</span></em></span></span></em><em><span class="c011">; </span></em><em><span class="c011"><span class="c008"><em><span class="c011">#Passes as a :Product</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">weight</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"23.0"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">decimal</span></span></em><em><span class="c011">;</span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">sku</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"A23456B234CBDF"</span></em></span></span></em><em><span class="c011"> .</span></em><em><span class="c011">
</span></em><em><span class="c011">
</span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">product2</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">name</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"Product 2"</span></em></span></span></em><em><span class="c011"> ; </span></em><em><span class="c011"><span class="c008"><em><span class="c011">#Fails as a :Product </span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">weight</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"245.5"</span></em></span></span></em><em><span class="c011">^^</span></em><em><span class="c011"><span class="c004">xsd</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">decimal</span></span></em><em><span class="c011"> ;</span></em><em><span class="c011"><span class="c008"><em><span class="c011"># schema:weight > 200 *)</span></em></span></span></em><em><span class="c011">
</span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c004">schema</span></span></em><em><span class="c011">:</span></em><em><span class="c011"><span class="c006">sku</span></span></em><em><span class="c011"> </span></em><em><span class="c011"><span class="c005"><em><span class="c011">"ABC"</span></em></span></span></em><em><span class="c011"> . </span></em><em><span class="c011"><span class="c008"><em><span class="c011"># schema:sku fails regex *)</span></em></span></span></em></td></tr>
</table><p><a href="https://goo.gl/bBrjbH"><em>Try example in Shaclex</em></a></p></div></div><p><a id="hevea_default382"></a>
<a id="hevea_default383"></a> <a id="RegularExpressions"></a>
The pattern constraint (‘
<code>/</code><code><span class="c006">regex</span></code><code>/</code>’) is based on the XPath regular expression function
<code><span class="c006">fn</span></code><code>:</code><code><span class="c006">matches</span></code><code>(</code><code><span class="c006">str</span></code><code>,</code><code><span class="c006">re</span></code><code>,</code><code><span class="c006">flags</span></code><code>)</code> which takes as parameters the string to match, the regular expression, and an optional flags parameter to modify the matching behavior.</p><p><a id="hevea_default384"></a>
XPath regular expressions are based on common conventions from other languages like Perl or other Unix tools like grep.
The regular expression language is a string composed of the characters to match and some characters which have special meaning called <em>meta-characters</em>.</p><ul class="itemize"><li class="li-itemize">
<code><span class="c006">x</span></code> matches the
<code><code>'x'</code></code> character.
</li><li class="li-itemize">
<code>\</code><code><span class="c006">u0078</span></code> matches the unicode codepoint U+78 (which is again
<code><code>'x'</code></code>).
</li><li class="li-itemize">
<code>.</code> matches any character.
</li><li class="li-itemize">
<code>[</code><code><span class="c006">vxz</span></code><code>]</code> declares a character class, and matches any of
<code><code>'v'</code></code>,
<code><code>'x'</code></code>, or
<code><code>'z'</code></code>.
</li><li class="li-itemize">
<code>\</code><code><span class="c006">d</span></code> is a pre-defined character class which matches any digit.
It is equivalent ot “
<code>[0-9]</code>”.
</li><li class="li-itemize">
<code>\</code><code><span class="c006">S</span></code> is a pre-defined character class which matches any space character (which also includes tabs and newlines). It is equivalent ot “
<code>[\</code><code><span class="c006">u0008</span></code><code>\</code><code><span class="c006">u000d</span></code><code>\</code><code><span class="c006">u000a</span></code><code>\</code><code><span class="c006">u0020</span></code><code>]</code>”.
</li></ul><p>Inside character classes, the symbol “
<code>^</code>” means negation and “
<code>-</code>” can be used to declare character ranges.
For instance, the character class
<code>[^</code><code><span class="c003">a</span></code><code>-</code><code><span class="c006">zA</span></code><code>-</code><code><span class="c006">Z</span></code><code>]</code> matches any non-letter.</p><p>Cardinality (repetition) operators can be used to specify how many characters are matched.
The possibilities are as follows. </p><ul class="itemize"><li class="li-itemize">
<code>?</code> represents zero or one values.
</li><li class="li-itemize">
<code>+</code> one or more values.
</li><li class="li-itemize">
<code>*</code> zero or more values.
</li><li class="li-itemize">
<code>{</code><code><span class="c006">m</span></code><code>,</code><code><span class="c006">n</span></code><code>}</code> between
<code><span class="c006">m</span></code> and
<code><span class="c006">n</span></code> values.</li></ul><p>Any string of characters must be matched in the order of its characters with the following alterations. </p><ul class="itemize"><li class="li-itemize">
<code>|</code> declares alternatives, e.g., “
<code>abc</code><code>|</code><code>def</code><code>|</code><code>ghi</code>” matches any of “
<code><span class="c006">abc</span></code>”, “
<code><span class="c006">def</span></code>”, “
<code><span class="c006">ghi</span></code>”.
</li><li class="li-itemize">
<code>^</code> matches the beginning of a string.
</li><li class="li-itemize">
<code>$</code> matches the end of a string.
</li><li class="li-itemize">“()” declares a group which is useful for cardinality and alternatives.
For example: “
<code>\^</code><code>ab</code><code>(</code><code>cd</code><code>|</code><code>ef</code><code>){2,}</code><code>gh</code>” matches “
<code><span class="c006">abcdcdcdghij</span></code>”.
</li></ul><p>All of the meta characters above will be treated as a literal (i.e., they match themselves) if they are prefixed with a
<code>\\</code> (backslash).</p><p>Table <a href="#ch040regexExamples">4.4</a> contains several examples of regular expression matches.</p><blockquote class="table"><div class="center"><hr class="c021"></div>
<div class="caption"><table class="c002 cellpading0"><tr><td class="c018">Table 4.4: Examples of regular expressions</td></tr>
</table></div>
<a id="ch040regexExamples"></a>
<div class="center">
<table class="c000 cellpadding1" border=1><tr><td class="c016"> <span class="c013">Regular Expression</span></td><td class="c016"><span class="c013">Some values that match</span></td><td class="c016"><span class="c013">Some values that don’t match
</span></td></tr>
<tr><td class="c016">
<code><span class="c006">P</span></code><code>\</code><code><span class="c006">d</span></code><code>{2,3}</code></td><td class="c016">
<code><span class="c006">P12</span></code>
<code><span class="c006">P234</span></code></td><td class="c016">
<code><span class="c006">A1</span></code>
<code><span class="c006">P2n</span></code>
<code><span class="c006">P1</span></code>
<code><span class="c006">P2233</span></code>
</td></tr>
<tr><td class="c016">
<code>(</code><code><span class="c006">pa</span></code><code>)*</code><code><span class="c006">b</span></code></td><td class="c016">
<code><span class="c006">b</span></code>
<code><span class="c006">pab</span></code>
<code><span class="c006">papab</span></code>
<code><span class="c006">papapab</span></code> …</td><td class="c016">
<code><span class="c006">pa</span></code>
<code><span class="c006">po</span></code>
</td></tr>
<tr><td class="c016">
<code>(</code><code><span class="c006">pa</span></code><code>)*</code><code><span class="c006">b</span></code></td><td class="c016">
<code><span class="c006">b</span></code>
<code><span class="c006">pab</span></code>
<code><span class="c006">papab</span></code>
<code><span class="c006">papapab</span></code> …</td><td class="c016">
<code><span class="c006">pa</span></code>
<code><span class="c006">po</span></code>
</td></tr>
<tr><td class="c016">
<code>[</code><code><span class="c003">a</span></code><code>-</code><code><span class="c006">z</span></code><code>]{2,3}</code></td><td class="c016">
<code><span class="c006">ab</span></code>
<code><span class="c006">abc</span></code></td><td class="c016">
<code><span class="c003">a</span></code>
<code><span class="c006">abcd</span></code>
<code>23</code>
</td></tr>
<tr><td class="c016">
<code>[</code><code><span class="c003">a</span></code><code>-</code><code><span class="c006">z</span></code><code>]{2,3}</code></td><td class="c016">
<code><span class="c006">ab</span></code>
<code><span class="c006">abc</span></code></td><td class="c016">
<code><span class="c003">a</span></code>
<code><span class="c006">abcd</span></code>
<code><span class="c006">x45</span></code>
<code>23</code>
</td></tr>
</table>
</div>
<div class="center"><hr class="c021"></div></blockquote><p>The flags string has the following possibilities. </p><ul class="itemize"><li class="li-itemize"><p><a id="hevea_default385"></a>
</p>
<code><span class="c006">i</span></code>: Case-insensitive mode. <p><a id="hevea_default386"></a>
</p></li><li class="li-itemize">
<code><span class="c006">m</span></code>: Multi-line mode.
If present, the
<code>^</code> character matches the start of any line (not only the start of the string)
and the <code>$</code> matches the end of any line (not only the end of the string).<p><a id="hevea_default387"></a>
</p></li><li class="li-itemize">
<code><span class="c006">s</span></code>: If present, the dot matches also newlines, otherwise it matches any character except newlines.
This mode is called single-line mode in Perl.<p><a id="hevea_default388"></a>
</p></li><li class="li-itemize">
<code><span class="c006">x</span></code>: Removes white space characters in the regular expression before matching.<p><a id="hevea_default389"></a>
</p></li><li class="li-itemize">
<code><span class="c006">q</span></code>: All meta characters are interpreted as literals, i.e., they match themselves in the input string.
<code><span class="c006">q</span></code> is compatible with the
<code><span class="c006">i</span></code> flag.
If it’s used with the
<code><span class="c006">m</span></code>,
<code><span class="c006">s</span></code> or
<code><span class="c006">x</span></code> flag, that flag is ignored.
</li></ul><p><a id="hevea_default390"></a>
</p>
<h3 class="subsection" id="sec73">4.5.4 Value Sets</h3>
<p>
<a id="ch4.sec4.4"></a>
<a id="ch040ValueSets"></a></p><p>A value set is a node constraint which enumerates the list of possible values that a focus node may have.
<a id="hevea_default391"></a>