doclang 0.7.0__tar.gz → 0.7.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {doclang-0.7.0 → doclang-0.7.2}/CHANGELOG.md +10 -0
- {doclang-0.7.0/doclang.egg-info → doclang-0.7.2}/PKG-INFO +11 -7
- {doclang-0.7.0 → doclang-0.7.2}/README.md +6 -4
- {doclang-0.7.0 → doclang-0.7.2}/doclang/README.md +60 -1
- doclang-0.7.2/doclang/__init__.py +15 -0
- doclang-0.7.2/doclang/backends/__init__.py +1 -0
- doclang-0.7.2/doclang/backends/_svrl.py +25 -0
- doclang-0.7.0/doclang/schematron_validation.py → doclang-0.7.2/doclang/backends/saxonche.py +30 -60
- {doclang-0.7.0 → doclang-0.7.2}/doclang/cli.py +4 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang/doclang.sch +4 -2
- {doclang-0.7.0 → doclang-0.7.2}/doclang/doclang.xsd +1 -1
- doclang-0.7.2/doclang/schematron.py +58 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang/validation.py +26 -14
- {doclang-0.7.0 → doclang-0.7.2/doclang.egg-info}/PKG-INFO +11 -7
- {doclang-0.7.0 → doclang-0.7.2}/doclang.egg-info/SOURCES.txt +5 -2
- doclang-0.7.2/doclang.egg-info/requires.txt +5 -0
- {doclang-0.7.0 → doclang-0.7.2}/pyproject.toml +8 -4
- doclang-0.7.0/doclang/__init__.py +0 -6
- doclang-0.7.0/doclang.egg-info/requires.txt +0 -3
- {doclang-0.7.0 → doclang-0.7.2}/LICENSE +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/MANIFEST.in +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang/_packaging.py +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang/_schemas.py +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang/packaging.py +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang/utils.py +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang/version.py +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang/xsd_validation.py +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang.egg-info/dependency_links.txt +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang.egg-info/entry_points.txt +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/doclang.egg-info/top_level.txt +0 -0
- {doclang-0.7.0 → doclang-0.7.2}/setup.cfg +0 -0
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
## [v0.7.2](https://github.com/doclang-project/doclang/releases/tag/v0.7.2)
|
|
2
|
+
|
|
3
|
+
* expand lxml version support (#158)
|
|
4
|
+
|
|
5
|
+
## [v0.7.1](https://github.com/doclang-project/doclang/releases/tag/v0.7.1)
|
|
6
|
+
|
|
7
|
+
* extend version sync to versioned fixture (#156)
|
|
8
|
+
* add pluggable Schematron validation backends (#155)
|
|
9
|
+
* **BREAKING CHANGE**: `saxonche` is no longer installed by default; install `doclang[schematron-saxon]` (or pass a custom `schematron=` backend) for Schematron validation.
|
|
10
|
+
|
|
1
11
|
## [v0.7.0](https://github.com/doclang-project/doclang/releases/tag/v0.7.0)
|
|
2
12
|
|
|
3
13
|
* add archive packaging (#151)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: doclang
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.2
|
|
4
4
|
Summary: DocLang reference toolkit
|
|
5
5
|
Author: DocLang Project
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -17,14 +17,16 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.11
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
21
|
Classifier: Topic :: Software Development :: Libraries
|
|
21
22
|
Classifier: Topic :: Text Processing :: Markup :: XML
|
|
22
23
|
Requires-Python: >=3.10
|
|
23
24
|
Description-Content-Type: text/markdown
|
|
24
25
|
License-File: LICENSE
|
|
25
|
-
Requires-Dist: lxml>=
|
|
26
|
-
Requires-Dist: saxonche>=12.9.0
|
|
26
|
+
Requires-Dist: lxml>=4.8.0
|
|
27
27
|
Requires-Dist: typer>=0.15.1
|
|
28
|
+
Provides-Extra: schematron-saxon
|
|
29
|
+
Requires-Dist: saxonche>=12.9.0; extra == "schematron-saxon"
|
|
28
30
|
Dynamic: license-file
|
|
29
31
|
|
|
30
32
|
<p align="center">
|
|
@@ -55,10 +57,14 @@ directory.
|
|
|
55
57
|
|
|
56
58
|
## Reference Toolkit
|
|
57
59
|
|
|
58
|
-
|
|
60
|
+
The commands below illustrate basic scenarios. For advanced installation and usage options
|
|
61
|
+
(minimal install, platform notes, custom Schematron backends, Python API), see the
|
|
62
|
+
[toolkit README](https://github.com/doclang-project/doclang/blob/main/doclang/README.md).
|
|
63
|
+
|
|
64
|
+
### Installation
|
|
59
65
|
|
|
60
66
|
```bash
|
|
61
|
-
pip install doclang
|
|
67
|
+
pip install "doclang[schematron-saxon]"
|
|
62
68
|
```
|
|
63
69
|
|
|
64
70
|
### Validation
|
|
@@ -73,8 +79,6 @@ doclang validate -n my_document.dclg
|
|
|
73
79
|
doclang pack my_document.dclg
|
|
74
80
|
```
|
|
75
81
|
|
|
76
|
-
For more details, see the [doclang/README.md](https://github.com/doclang-project/doclang/blob/main/doclang/README.md).
|
|
77
|
-
|
|
78
82
|
## Citation
|
|
79
83
|
|
|
80
84
|
If you use DocLang in academic or technical work, please cite the specification:
|
|
@@ -26,10 +26,14 @@ directory.
|
|
|
26
26
|
|
|
27
27
|
## Reference Toolkit
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
The commands below illustrate basic scenarios. For advanced installation and usage options
|
|
30
|
+
(minimal install, platform notes, custom Schematron backends, Python API), see the
|
|
31
|
+
[toolkit README](https://github.com/doclang-project/doclang/blob/main/doclang/README.md).
|
|
32
|
+
|
|
33
|
+
### Installation
|
|
30
34
|
|
|
31
35
|
```bash
|
|
32
|
-
pip install doclang
|
|
36
|
+
pip install "doclang[schematron-saxon]"
|
|
33
37
|
```
|
|
34
38
|
|
|
35
39
|
### Validation
|
|
@@ -44,8 +48,6 @@ doclang validate -n my_document.dclg
|
|
|
44
48
|
doclang pack my_document.dclg
|
|
45
49
|
```
|
|
46
50
|
|
|
47
|
-
For more details, see the [doclang/README.md](https://github.com/doclang-project/doclang/blob/main/doclang/README.md).
|
|
48
|
-
|
|
49
51
|
## Citation
|
|
50
52
|
|
|
51
53
|
If you use DocLang in academic or technical work, please cite the specification:
|
|
@@ -4,10 +4,23 @@ Official Python toolkit for working with DocLang — CLI commands and library AP
|
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
+
Recommended — full validation (XSD + Schematron):
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install "doclang[schematron-saxon]"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Minimal install without Schematron (packaging, XSD-only validation, or platforms where
|
|
14
|
+
`saxonche` has no wheel — e.g. Windows on ARM64, ppc64le, s390x):
|
|
15
|
+
|
|
7
16
|
```bash
|
|
8
17
|
pip install doclang
|
|
9
18
|
```
|
|
10
19
|
|
|
20
|
+
On a minimal install, pass `xsd_only=True` for XSD-only validation, or supply a custom
|
|
21
|
+
`schematron=` backend. Default `validate()` expects a Schematron backend and raises
|
|
22
|
+
`SchematronBackendNotFound` when none is available.
|
|
23
|
+
|
|
11
24
|
## CLI
|
|
12
25
|
|
|
13
26
|
### Validation
|
|
@@ -71,6 +84,50 @@ except ValidationError as exc:
|
|
|
71
84
|
print(f"{exc.schematron_errors=}")
|
|
72
85
|
```
|
|
73
86
|
|
|
87
|
+
### Custom Schematron backends
|
|
88
|
+
|
|
89
|
+
DocLang does not bundle a single Schematron runtime in the core package. Instead,
|
|
90
|
+
`validate()` accepts an optional Schematron engine via the `schematron` parameter.
|
|
91
|
+
When omitted, the default Saxon/C backend is used (requires `doclang[schematron-saxon]`).
|
|
92
|
+
|
|
93
|
+
To plug in your own engine, implement the `SchematronValidator` protocol and return
|
|
94
|
+
`SchematronViolation` objects — one per failed rule:
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
from pathlib import Path
|
|
98
|
+
|
|
99
|
+
from doclang import SchematronViolation, validate
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class MySchematronValidator:
|
|
103
|
+
def validate(
|
|
104
|
+
self,
|
|
105
|
+
xml_path: Path,
|
|
106
|
+
*,
|
|
107
|
+
schema_path: Path,
|
|
108
|
+
allow_empty_namespace: bool = False,
|
|
109
|
+
) -> list[SchematronViolation]:
|
|
110
|
+
# Run your engine against schema_path (bundled doclang.sch) and xml_path.
|
|
111
|
+
# Map failures to SchematronViolation(location=..., message=...).
|
|
112
|
+
...
|
|
113
|
+
|
|
114
|
+
validate("my_document.dclg", schematron=MySchematronValidator())
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Notes for implementers:
|
|
118
|
+
|
|
119
|
+
- `schema_path` points at the bundled `doclang.sch` rules (XPath 3.1 / `queryBinding="xslt3"`).
|
|
120
|
+
- Set `allow_empty_namespace=True` when the XML may lack the DocLang namespace; the
|
|
121
|
+
default Saxon backend injects it before validation — custom backends should do the same
|
|
122
|
+
if they operate on file paths rather than pre-processed trees.
|
|
123
|
+
- Use `xsd_only=True` on `validate()` when your application does not need Schematron at all.
|
|
124
|
+
|
|
125
|
+
**Example:** [pyschematron](https://pypi.org/project/pyschematron/) is a pure-Python
|
|
126
|
+
Schematron evaluator you can wrap in a `SchematronValidator` and install separately in
|
|
127
|
+
your own project (doclang does not ship or endorse it as a dependency). Similar adapters
|
|
128
|
+
can be built for any engine that evaluates ISO Schematron or consumes a precompiled XSLT
|
|
129
|
+
stylesheet derived from `doclang.sch`.
|
|
130
|
+
|
|
74
131
|
### Packaging
|
|
75
132
|
|
|
76
133
|
```python
|
|
@@ -106,7 +163,9 @@ Additional business rules that XSD cannot express, using XSLT 3.0 and XPath 3.1:
|
|
|
106
163
|
</sch:pattern>
|
|
107
164
|
```
|
|
108
165
|
|
|
109
|
-
The
|
|
166
|
+
The bundled Saxon/C backend transpiles these rules to XSLT 3.0 at validation time.
|
|
167
|
+
Alternative backends may evaluate the same `.sch` file directly or use a precompiled
|
|
168
|
+
`.xsl` artifact if their runtime requires it.
|
|
110
169
|
|
|
111
170
|
## XSD Validation with VS Code
|
|
112
171
|
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""DocLang reference toolkit."""
|
|
2
|
+
|
|
3
|
+
from doclang.packaging import PackagingError, pack
|
|
4
|
+
from doclang.schematron import SchematronBackendNotFound, SchematronValidator, SchematronViolation
|
|
5
|
+
from doclang.validation import ValidationError, validate
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"PackagingError",
|
|
9
|
+
"SchematronBackendNotFound",
|
|
10
|
+
"SchematronValidator",
|
|
11
|
+
"SchematronViolation",
|
|
12
|
+
"ValidationError",
|
|
13
|
+
"pack",
|
|
14
|
+
"validate",
|
|
15
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Optional Schematron validation backends."""
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""SVRL parsing helpers for Schematron backends."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from lxml import etree
|
|
6
|
+
|
|
7
|
+
from doclang.schematron import SchematronViolation
|
|
8
|
+
|
|
9
|
+
_SVRL_NS = "http://purl.oclc.org/dsdl/svrl"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _failed_assert_to_violation(assert_elem: etree._Element) -> SchematronViolation:
|
|
13
|
+
text_elem = assert_elem.find(f"{{{_SVRL_NS}}}text")
|
|
14
|
+
message = text_elem.text if text_elem is not None and text_elem.text else "No message"
|
|
15
|
+
return SchematronViolation(
|
|
16
|
+
location=assert_elem.get("location"),
|
|
17
|
+
message=message,
|
|
18
|
+
rule_id=assert_elem.get("id"),
|
|
19
|
+
test=assert_elem.get("test"),
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _svrl_failed_asserts_to_violations(svrl_root: etree._Element) -> list[SchematronViolation]:
|
|
24
|
+
failed_asserts = svrl_root.findall(f".//{{{_SVRL_NS}}}failed-assert")
|
|
25
|
+
return [_failed_assert_to_violation(elem) for elem in failed_asserts]
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
"""
|
|
2
|
-
Schematron validation
|
|
2
|
+
Saxon/C (saxonche) Schematron validation backend.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
on-the-fly using XSLT 3.0 / XPath 3.1.
|
|
4
|
+
Transpiles .sch files to XSLT on-the-fly using XSLT 3.0 / XPath 3.1.
|
|
6
5
|
"""
|
|
7
6
|
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
8
9
|
import tempfile
|
|
9
10
|
from pathlib import Path
|
|
10
|
-
from typing import Union
|
|
11
11
|
|
|
12
12
|
from lxml import etree
|
|
13
|
-
from saxonche import PySaxonProcessor
|
|
14
13
|
|
|
15
|
-
from doclang.
|
|
14
|
+
from doclang.backends._svrl import _svrl_failed_asserts_to_violations
|
|
15
|
+
from doclang.schematron import SchematronViolation, _require_saxonche_backend
|
|
16
16
|
from doclang.utils import _ensure_namespace
|
|
17
17
|
|
|
18
18
|
# ISO Schematron transpiler - converts .sch to XSLT 3.0
|
|
@@ -103,27 +103,16 @@ _ISO_SCHEMATRON_TRANSPILER = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
103
103
|
"""
|
|
104
104
|
|
|
105
105
|
|
|
106
|
-
def _transpile_schematron_to_xslt(sch_file, verbose=False):
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
Args:
|
|
111
|
-
sch_file: Path to Schematron file
|
|
112
|
-
verbose: Print progress messages
|
|
106
|
+
def _transpile_schematron_to_xslt(sch_file: Path, *, verbose: bool = False) -> str:
|
|
107
|
+
_require_saxonche_backend()
|
|
108
|
+
from saxonche import PySaxonProcessor
|
|
113
109
|
|
|
114
|
-
Returns:
|
|
115
|
-
str: Generated XSLT stylesheet as string
|
|
116
|
-
"""
|
|
117
110
|
if verbose:
|
|
118
111
|
print(f"Transpiling Schematron: {sch_file}")
|
|
119
112
|
|
|
120
113
|
with PySaxonProcessor(license=False) as proc:
|
|
121
114
|
xslt_proc = proc.new_xslt30_processor()
|
|
122
|
-
|
|
123
|
-
# Compile the transpiler
|
|
124
115
|
xslt_executable = xslt_proc.compile_stylesheet(stylesheet_text=_ISO_SCHEMATRON_TRANSPILER)
|
|
125
|
-
|
|
126
|
-
# Transform Schematron to XSLT
|
|
127
116
|
result = xslt_executable.transform_to_string(source_file=str(sch_file))
|
|
128
117
|
|
|
129
118
|
if not result:
|
|
@@ -132,76 +121,57 @@ def _transpile_schematron_to_xslt(sch_file, verbose=False):
|
|
|
132
121
|
return result
|
|
133
122
|
|
|
134
123
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
allow_empty_namespace: bool = False,
|
|
138
|
-
verbose: bool = False,
|
|
139
|
-
) -> list:
|
|
140
|
-
"""Validate XML against the bundled DocLang Schematron rules.
|
|
124
|
+
class SaxoncheValidator:
|
|
125
|
+
"""Schematron validator backed by Saxon/C (saxonche)."""
|
|
141
126
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
127
|
+
def validate(
|
|
128
|
+
self,
|
|
129
|
+
xml_path: Path,
|
|
130
|
+
*,
|
|
131
|
+
schema_path: Path,
|
|
132
|
+
allow_empty_namespace: bool = False,
|
|
133
|
+
verbose: bool = False,
|
|
134
|
+
) -> list[SchematronViolation]:
|
|
135
|
+
_require_saxonche_backend()
|
|
136
|
+
from saxonche import PySaxonProcessor
|
|
148
137
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
138
|
+
if verbose:
|
|
139
|
+
print(f"Using Schematron file: {schema_path}")
|
|
140
|
+
|
|
141
|
+
with open(xml_path, "rb") as f:
|
|
152
142
|
xml_doc = etree.parse(f)
|
|
153
143
|
|
|
154
144
|
if allow_empty_namespace:
|
|
155
145
|
xml_doc = _ensure_namespace(xml_doc)
|
|
156
146
|
|
|
157
|
-
# Write to temporary file for Saxon processing
|
|
158
|
-
# Note: delete=True (default) but we need to close before Saxon can read
|
|
159
147
|
with tempfile.NamedTemporaryFile(mode="wb", suffix=".xml", delete=True) as tmp:
|
|
160
148
|
xml_doc.write(tmp, encoding="utf-8", xml_declaration=True)
|
|
161
|
-
tmp.flush()
|
|
149
|
+
tmp.flush()
|
|
162
150
|
tmp_xml_path = tmp.name
|
|
163
151
|
|
|
164
|
-
# File is still open but flushed, Saxon can read it
|
|
165
152
|
with PySaxonProcessor(license=False) as proc:
|
|
166
153
|
if verbose:
|
|
167
154
|
print(f"Using XSLT processor version: {proc.version}")
|
|
168
155
|
|
|
169
|
-
# Get XSLT 3.0 processor (supports XPath 3.1)
|
|
170
156
|
xslt_proc = proc.new_xslt30_processor()
|
|
171
157
|
|
|
172
|
-
# Transpile Schematron to XSLT
|
|
173
158
|
if verbose:
|
|
174
159
|
print("Transpiling Schematron to XSLT 3.0...")
|
|
175
160
|
|
|
176
|
-
xslt_text = _transpile_schematron_to_xslt(
|
|
161
|
+
xslt_text = _transpile_schematron_to_xslt(schema_path, verbose=verbose)
|
|
177
162
|
|
|
178
|
-
# Compile the generated XSLT stylesheet
|
|
179
163
|
if verbose:
|
|
180
164
|
print("Compiling generated XSLT...")
|
|
181
165
|
|
|
182
166
|
xslt_executable = xslt_proc.compile_stylesheet(stylesheet_text=xslt_text)
|
|
183
167
|
|
|
184
|
-
# Transform XML
|
|
185
168
|
if verbose:
|
|
186
169
|
print("Executing Schematron validation...")
|
|
187
170
|
|
|
188
171
|
result = xslt_executable.transform_to_string(source_file=tmp_xml_path)
|
|
189
172
|
|
|
190
|
-
|
|
191
|
-
if result:
|
|
192
|
-
result_doc = etree.fromstring(result.encode("utf-8"))
|
|
193
|
-
failed_asserts = result_doc.findall(".//{http://purl.oclc.org/dsdl/svrl}failed-assert")
|
|
194
|
-
return failed_asserts
|
|
195
|
-
else:
|
|
196
|
-
# No output means validation passed
|
|
173
|
+
if not result:
|
|
197
174
|
return []
|
|
198
175
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
except Exception as e:
|
|
202
|
-
if verbose:
|
|
203
|
-
print(f"✗ Error during Schematron validation: {e}")
|
|
204
|
-
import traceback
|
|
205
|
-
|
|
206
|
-
traceback.print_exc()
|
|
207
|
-
raise
|
|
176
|
+
result_doc = etree.fromstring(result.encode("utf-8"))
|
|
177
|
+
return _svrl_failed_asserts_to_violations(result_doc)
|
|
@@ -14,6 +14,7 @@ import typer
|
|
|
14
14
|
from doclang._schemas import _bundled_schema_paths
|
|
15
15
|
from doclang.packaging import PackagingError
|
|
16
16
|
from doclang.packaging import pack as pack_document
|
|
17
|
+
from doclang.schematron import SchematronBackendNotFound
|
|
17
18
|
from doclang.utils import _VERSION
|
|
18
19
|
from doclang.validation import ValidationError
|
|
19
20
|
from doclang.validation import validate as validate_document
|
|
@@ -77,6 +78,9 @@ def validate(
|
|
|
77
78
|
xsd_only=xsd_only,
|
|
78
79
|
schematron_only=schematron_only,
|
|
79
80
|
)
|
|
81
|
+
except SchematronBackendNotFound as exc:
|
|
82
|
+
typer.echo(f"Error: {exc}", err=True)
|
|
83
|
+
raise typer.Exit(1)
|
|
80
84
|
except ValidationError as exc:
|
|
81
85
|
results: dict[str, Any] = {
|
|
82
86
|
"xsd": {"valid": not exc.xsd_errors, "errors": exc.xsd_errors},
|
|
@@ -56,7 +56,8 @@
|
|
|
56
56
|
<sch:let name="cell-tokens" value="dl:fcel | dl:ecel | dl:ched | dl:rhed | dl:corn | dl:srow | dl:lcel | dl:ucel | dl:xcel"/>
|
|
57
57
|
|
|
58
58
|
<!-- Count cells in first row (before first nl) -->
|
|
59
|
-
<sch:let name="first-
|
|
59
|
+
<sch:let name="first-nl" value="dl:nl[1]"/>
|
|
60
|
+
<sch:let name="first-row-cells" value="count($cell-tokens[following-sibling::dl:nl[1] is $first-nl])"/>
|
|
60
61
|
|
|
61
62
|
<!-- Check that all subsequent rows have the same number of cells -->
|
|
62
63
|
<sch:assert test="every $nl in dl:nl[position() > 1] satisfies
|
|
@@ -109,7 +110,8 @@
|
|
|
109
110
|
|
|
110
111
|
<sch:pattern id="xref-thread-defined">
|
|
111
112
|
<sch:rule context="dl:xref">
|
|
112
|
-
<sch:
|
|
113
|
+
<sch:let name="thread-id" value="@thread_id"/>
|
|
114
|
+
<sch:assert test="exists(//dl:thread[@thread_id = $thread-id])">
|
|
113
115
|
Element xref references thread_id="<sch:value-of select="@thread_id"/>" but no thread element defines that id.
|
|
114
116
|
</sch:assert>
|
|
115
117
|
</sch:rule>
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Schematron validation plug-in interface."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Protocol, runtime_checkable
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True)
|
|
11
|
+
class SchematronViolation:
|
|
12
|
+
"""A single Schematron rule failure."""
|
|
13
|
+
|
|
14
|
+
message: str
|
|
15
|
+
location: str | None = None
|
|
16
|
+
rule_id: str | None = None
|
|
17
|
+
test: str | None = None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@runtime_checkable
|
|
21
|
+
class SchematronValidator(Protocol):
|
|
22
|
+
"""Validate DocLang XML against a Schematron schema."""
|
|
23
|
+
|
|
24
|
+
def validate(
|
|
25
|
+
self,
|
|
26
|
+
xml_path: Path,
|
|
27
|
+
*,
|
|
28
|
+
schema_path: Path,
|
|
29
|
+
allow_empty_namespace: bool = False,
|
|
30
|
+
) -> list[SchematronViolation]:
|
|
31
|
+
"""Return rule violations; an empty list means validation passed."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class SchematronBackendNotFound(ImportError):
|
|
35
|
+
"""Raised when Schematron validation is requested but no backend is available."""
|
|
36
|
+
|
|
37
|
+
def __init__(self) -> None:
|
|
38
|
+
super().__init__(
|
|
39
|
+
"Schematron validation requires a backend. Install doclang[schematron-saxon] "
|
|
40
|
+
"or pass schematron=YourValidator() to validate()."
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _require_saxonche_backend() -> None:
|
|
45
|
+
"""Raise SchematronBackendNotFound when the optional saxonche dependency is missing."""
|
|
46
|
+
try:
|
|
47
|
+
import saxonche # noqa: F401
|
|
48
|
+
except ImportError as exc:
|
|
49
|
+
raise SchematronBackendNotFound() from exc
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _default_schematron_validator() -> SchematronValidator:
|
|
53
|
+
_require_saxonche_backend()
|
|
54
|
+
try:
|
|
55
|
+
from doclang.backends.saxonche import SaxoncheValidator
|
|
56
|
+
except ImportError as exc:
|
|
57
|
+
raise SchematronBackendNotFound() from exc
|
|
58
|
+
return SaxoncheValidator()
|
|
@@ -3,23 +3,25 @@
|
|
|
3
3
|
from pathlib import Path
|
|
4
4
|
from typing import Any, Union
|
|
5
5
|
|
|
6
|
-
from doclang.
|
|
6
|
+
from doclang._schemas import _bundled_sch_path
|
|
7
|
+
from doclang.schematron import (
|
|
8
|
+
SchematronBackendNotFound,
|
|
9
|
+
SchematronValidator,
|
|
10
|
+
SchematronViolation,
|
|
11
|
+
_default_schematron_validator,
|
|
12
|
+
)
|
|
7
13
|
from doclang.xsd_validation import _validate_xsd
|
|
8
14
|
|
|
9
|
-
__all__ = ["ValidationError", "validate"]
|
|
15
|
+
__all__ = ["SchematronBackendNotFound", "ValidationError", "validate"]
|
|
10
16
|
|
|
11
|
-
_SVRL_NS = "http://purl.oclc.org/dsdl/svrl"
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
def _failed_asserts_to_errors(failed_asserts: list) -> list[dict[str, Any]]:
|
|
18
|
+
def _violations_to_errors(violations: list[SchematronViolation]) -> list[dict[str, Any]]:
|
|
15
19
|
return [
|
|
16
20
|
{
|
|
17
|
-
"location":
|
|
18
|
-
"message":
|
|
19
|
-
if assert_elem.find(f"{{{_SVRL_NS}}}text") is not None
|
|
20
|
-
else "No message",
|
|
21
|
+
"location": violation.location or "unknown",
|
|
22
|
+
"message": violation.message,
|
|
21
23
|
}
|
|
22
|
-
for
|
|
24
|
+
for violation in violations
|
|
23
25
|
]
|
|
24
26
|
|
|
25
27
|
|
|
@@ -61,10 +63,17 @@ def validate(
|
|
|
61
63
|
allow_empty_namespace: bool = False,
|
|
62
64
|
xsd_only: bool = False,
|
|
63
65
|
schematron_only: bool = False,
|
|
66
|
+
schematron: SchematronValidator | None = None,
|
|
64
67
|
) -> None:
|
|
65
68
|
"""Validate a DocLang XML file using the bundled reference XSD and Schematron rules.
|
|
66
69
|
|
|
70
|
+
By default both XSD and Schematron validation run. Pass ``schematron`` to use a
|
|
71
|
+
custom Schematron backend; when omitted, the default Saxon/C backend is used
|
|
72
|
+
(requires ``doclang[schematron-saxon]``).
|
|
73
|
+
|
|
67
74
|
Raises :class:`ValidationError` on failure.
|
|
75
|
+
Raises :class:`SchematronBackendNotFound` when Schematron validation is requested
|
|
76
|
+
but no backend is available.
|
|
68
77
|
"""
|
|
69
78
|
path = Path(xml_file)
|
|
70
79
|
xsd_errors: list[dict[str, Any]] = []
|
|
@@ -74,14 +83,17 @@ def validate(
|
|
|
74
83
|
xsd_errors = _validate_xsd(path, allow_empty_namespace=allow_empty_namespace)
|
|
75
84
|
|
|
76
85
|
if not xsd_only:
|
|
86
|
+
validator = schematron or _default_schematron_validator()
|
|
77
87
|
try:
|
|
78
|
-
|
|
88
|
+
violations = validator.validate(
|
|
79
89
|
path,
|
|
90
|
+
schema_path=_bundled_sch_path(),
|
|
80
91
|
allow_empty_namespace=allow_empty_namespace,
|
|
81
|
-
verbose=False,
|
|
82
92
|
)
|
|
83
|
-
if
|
|
84
|
-
schematron_errors =
|
|
93
|
+
if violations:
|
|
94
|
+
schematron_errors = _violations_to_errors(violations)
|
|
95
|
+
except SchematronBackendNotFound:
|
|
96
|
+
raise
|
|
85
97
|
except Exception as exc:
|
|
86
98
|
schematron_errors = [{"error": str(exc)}]
|
|
87
99
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: doclang
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.2
|
|
4
4
|
Summary: DocLang reference toolkit
|
|
5
5
|
Author: DocLang Project
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -17,14 +17,16 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.11
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
21
|
Classifier: Topic :: Software Development :: Libraries
|
|
21
22
|
Classifier: Topic :: Text Processing :: Markup :: XML
|
|
22
23
|
Requires-Python: >=3.10
|
|
23
24
|
Description-Content-Type: text/markdown
|
|
24
25
|
License-File: LICENSE
|
|
25
|
-
Requires-Dist: lxml>=
|
|
26
|
-
Requires-Dist: saxonche>=12.9.0
|
|
26
|
+
Requires-Dist: lxml>=4.8.0
|
|
27
27
|
Requires-Dist: typer>=0.15.1
|
|
28
|
+
Provides-Extra: schematron-saxon
|
|
29
|
+
Requires-Dist: saxonche>=12.9.0; extra == "schematron-saxon"
|
|
28
30
|
Dynamic: license-file
|
|
29
31
|
|
|
30
32
|
<p align="center">
|
|
@@ -55,10 +57,14 @@ directory.
|
|
|
55
57
|
|
|
56
58
|
## Reference Toolkit
|
|
57
59
|
|
|
58
|
-
|
|
60
|
+
The commands below illustrate basic scenarios. For advanced installation and usage options
|
|
61
|
+
(minimal install, platform notes, custom Schematron backends, Python API), see the
|
|
62
|
+
[toolkit README](https://github.com/doclang-project/doclang/blob/main/doclang/README.md).
|
|
63
|
+
|
|
64
|
+
### Installation
|
|
59
65
|
|
|
60
66
|
```bash
|
|
61
|
-
pip install doclang
|
|
67
|
+
pip install "doclang[schematron-saxon]"
|
|
62
68
|
```
|
|
63
69
|
|
|
64
70
|
### Validation
|
|
@@ -73,8 +79,6 @@ doclang validate -n my_document.dclg
|
|
|
73
79
|
doclang pack my_document.dclg
|
|
74
80
|
```
|
|
75
81
|
|
|
76
|
-
For more details, see the [doclang/README.md](https://github.com/doclang-project/doclang/blob/main/doclang/README.md).
|
|
77
|
-
|
|
78
82
|
## Citation
|
|
79
83
|
|
|
80
84
|
If you use DocLang in academic or technical work, please cite the specification:
|
|
@@ -11,7 +11,7 @@ doclang/cli.py
|
|
|
11
11
|
doclang/doclang.sch
|
|
12
12
|
doclang/doclang.xsd
|
|
13
13
|
doclang/packaging.py
|
|
14
|
-
doclang/
|
|
14
|
+
doclang/schematron.py
|
|
15
15
|
doclang/utils.py
|
|
16
16
|
doclang/validation.py
|
|
17
17
|
doclang/version.py
|
|
@@ -21,4 +21,7 @@ doclang.egg-info/SOURCES.txt
|
|
|
21
21
|
doclang.egg-info/dependency_links.txt
|
|
22
22
|
doclang.egg-info/entry_points.txt
|
|
23
23
|
doclang.egg-info/requires.txt
|
|
24
|
-
doclang.egg-info/top_level.txt
|
|
24
|
+
doclang.egg-info/top_level.txt
|
|
25
|
+
doclang/backends/__init__.py
|
|
26
|
+
doclang/backends/_svrl.py
|
|
27
|
+
doclang/backends/saxonche.py
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "doclang"
|
|
7
|
-
version = "0.7.
|
|
7
|
+
version = "0.7.2" # DO NOT EDIT MANUALLY, updated automatically
|
|
8
8
|
description = "DocLang reference toolkit"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -20,15 +20,18 @@ classifiers = [
|
|
|
20
20
|
"Programming Language :: Python :: 3.11",
|
|
21
21
|
"Programming Language :: Python :: 3.12",
|
|
22
22
|
"Programming Language :: Python :: 3.13",
|
|
23
|
+
"Programming Language :: Python :: 3.14",
|
|
23
24
|
"Topic :: Software Development :: Libraries",
|
|
24
25
|
"Topic :: Text Processing :: Markup :: XML",
|
|
25
26
|
]
|
|
26
27
|
dependencies = [
|
|
27
|
-
"lxml>=
|
|
28
|
-
"saxonche>=12.9.0",
|
|
28
|
+
"lxml>=4.8.0",
|
|
29
29
|
"typer>=0.15.1",
|
|
30
30
|
]
|
|
31
31
|
|
|
32
|
+
[project.optional-dependencies]
|
|
33
|
+
schematron-saxon = ["saxonche>=12.9.0"]
|
|
34
|
+
|
|
32
35
|
[project.urls]
|
|
33
36
|
Homepage = "https://www.doclang.ai/"
|
|
34
37
|
Documentation = "https://github.com/doclang-project/doclang/blob/main/README.md"
|
|
@@ -49,6 +52,7 @@ ci = [
|
|
|
49
52
|
"ruff>=0.14.11",
|
|
50
53
|
"pytest~=8.3",
|
|
51
54
|
"python-docx>=1.2.0",
|
|
55
|
+
"saxonche>=12.9.0",
|
|
52
56
|
]
|
|
53
57
|
dev = [
|
|
54
58
|
{ include-group = "ci" },
|
|
@@ -63,7 +67,7 @@ package = true
|
|
|
63
67
|
default-groups = ["dev"]
|
|
64
68
|
|
|
65
69
|
[tool.setuptools]
|
|
66
|
-
packages = ["doclang"]
|
|
70
|
+
packages = ["doclang", "doclang.backends"]
|
|
67
71
|
|
|
68
72
|
[tool.ruff]
|
|
69
73
|
target-version = "py310"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|