8 |
9 | Cargo checking
10 |
11 | This checks the cargo manifest on weight, size, and vehicles on number of wheels.
12 | By default it checks on weight only.
13 | A second paragraph to test if this is processed correctly.
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 | Weight check
23 |
24 | info
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 | Volume check
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | Just a check on the banana's.
51 |
52 | Banana is a fruit.
53 | And the weight of the banana shall be 1.
54 |
55 |
56 |
57 |
58 |
59 |
60 | Volume not correct ( vs at ).
61 | Testing name with path:
62 |
63 |
64 | We report an item with a volume greater than allowed.
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | XML model processing instruction does not include foobar.
74 |
75 |
76 |
77 |
78 |
79 |
86 |
87 |
88 |
89 |
90 |
91 | The id attribute does not starts with "id_".
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 | The root node does not have an ID.
101 |
102 |
103 | The namespace for "test" should be "http://www.test.com".
104 |
105 |
106 |
107 |
108 |
109 |
110 | Check for all the vehicles if they are in the right category.
111 |
112 |
113 |
114 |
115 |
116 | Check for all the fruits if they are in the right category.
117 |
118 |
119 |
120 |
121 |
122 | Check if items are in the right category ($pv_category).
123 |
124 |
125 | The item is in the wrong category ($pv_category).
126 | Extra data
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 | Only check the cargo items for weight.
135 | Check for weights
136 |
137 |
138 |
139 |
140 | Only check the cargo items for volume.
141 |
142 |
143 |
144 |
145 | Only check the cargo for the right category.
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
--------------------------------------------------------------------------------
/scripts/demo_custom_functions.py:
--------------------------------------------------------------------------------
1 | """This script shows how to use custom Python functions inside your Schematron schema's.
2 |
3 | The general idea is that you either overwrite an existing query binding language, or define a new query binding
4 | with your custom functions loaded. Your custom functions will then be attached to the query parser defined for that
5 | specific query language.
6 |
7 | As an example. Suppose you have a small custom function named `custom-func()`, and you want to use it in your
8 | Schematron Schema. Your Schema is defined using `queryBinding="xpath31"` and you wish to extend this with your
9 | custom function. For clarity, you want to call your new query binding language "xpath31-custom". In your Schematron
10 | schema you then use `queryBinding="xpath31-custom"`, and in your queries you can use the `custom-func()`. For
11 | PySchematron to know about this function, you must define it and add it to the library. This module shows how.
12 |
13 | There are three ways of interacting with the PySchematron direct-mode validator. The most simple is by using the
14 | functional interface defined in the main module. Second, you can use a generalized API which might be extended in the
15 | future with an XSLT methodology. Finally, you can use the full-blown direct-mode classes and methods. The latter is the
16 | most complicated but gives the most control. Either of these though enables adding custom functions.
17 | """
18 |
19 | __author__ = 'Robbert Harms'
20 | __date__ = '2024-04-03'
21 | __maintainer__ = 'Robbert Harms'
22 | __email__ = 'robbert@xkls.nl'
23 | __licence__ = 'LGPL v3'
24 |
25 | from pathlib import Path
26 |
27 | from elementpath import ElementNode
28 | from lxml import etree
29 | from lxml.etree import _ElementTree
30 |
31 | from pyschematron import DirectModeSchematronValidatorFactory, validate_document
32 | from pyschematron.direct_mode.schematron.parsers.xml.parser import SchemaParser
33 | from pyschematron.direct_mode.xml_validation.queries.factories import ExtendableQueryProcessorFactory
34 | from pyschematron.direct_mode.xml_validation.queries.xpath import (XPathQueryProcessor, XPath31QueryParser,
35 | SimpleCustomXPathFunction)
36 | from pyschematron.direct_mode.xml_validation.results.svrl_builder import DefaultSVRLReportBuilder
37 | from pyschematron.direct_mode.xml_validation.validators import SimpleSchematronXMLValidator
38 | from pyschematron.utils import load_xml_document
39 |
40 |
41 | def get_example_schema() -> _ElementTree:
42 | """Get the example Schema for the examples.
43 |
44 | In this Schema, we defined a new query binding language `queryBinding="xpath31-custom"` which we will
45 | also need to add in PySchematron. Note also the use of `custom-func()`.
46 |
47 | Returns:
48 | The loaded Schema.
49 | """
50 | schematron = '''
51 |