| 1 |
# Work with PROV-XML |
| 2 |
|
| 3 |
[PROV-XML](https://www.w3.org/TR/prov-xml/) support needs the optional `lxml` dependency: |
| 4 |
|
| 5 |
```bash |
| 6 |
python -m pip install "prov[xml]" |
| 7 |
``` |
| 8 |
|
| 9 |
## Serialize to a file |
| 10 |
|
| 11 |
```python |
| 12 |
import prov.model as pm |
| 13 |
|
| 14 |
document = pm.ProvDocument() |
| 15 |
document.set_default_namespace("http://example.org/") |
| 16 |
document.entity("e1") |
| 17 |
|
| 18 |
document.serialize("document.xml", format="xml") |
| 19 |
``` |
| 20 |
|
| 21 |
## Serialize to a string |
| 22 |
|
| 23 |
```python |
| 24 |
xml_str = document.serialize(format="xml") |
| 25 |
print(xml_str) |
| 26 |
``` |
| 27 |
|
| 28 |
## Force xsd:type attributes |
| 29 |
|
| 30 |
By default `xsi:type` is only written for `prov:type`, `prov:location`, and `prov:value`, |
| 31 |
matching the PROV-XML spec examples. Pass `force_types=True` to write it for every |
| 32 |
non-`prov:` attribute too: |
| 33 |
|
| 34 |
```python |
| 35 |
xml_str_typed = document.serialize(format="xml", force_types=True) |
| 36 |
``` |
| 37 |
|
| 38 |
## Deserialize from a file or stream |
| 39 |
|
| 40 |
```python |
| 41 |
loaded = pm.ProvDocument.deserialize("document.xml", format="xml") |
| 42 |
assert loaded == document |
| 43 |
``` |
| 44 |
|
| 45 |
## Deserialize from a string |
| 46 |
|
| 47 |
```python |
| 48 |
loaded = pm.ProvDocument.deserialize(content=xml_str, format="xml") |
| 49 |
``` |
| 50 |
|
| 51 |
## Auto-detect the format with `prov.read()` |
| 52 |
|
| 53 |
`prov.read()` tries each registered deserializer in turn — PROV-JSON, then PROV-O/RDF, then |
| 54 |
PROV-N, then PROV-XML — treating any failure from a candidate as "not this format" and |
| 55 |
moving on to the next one, stopping at the first deserializer that both succeeds and |
| 56 |
produces a non-empty document. Valid PROV-XML therefore auto-detects, whether the source is |
| 57 |
a file path or raw content: |
| 58 |
|
| 59 |
```python |
| 60 |
import prov |
| 61 |
|
| 62 |
loaded = prov.read("document.xml") # no format given |
| 63 |
assert loaded == document |
| 64 |
``` |
| 65 |
|
| 66 |
A seekable stream source (an open file object, `io.StringIO`, `io.BytesIO`, ...) is rewound |
| 67 |
between auto-detection attempts, so it also auto-detects; a non-seekable stream is consumed |
| 68 |
by the first candidate, so pass `format="xml"` explicitly for those. |
| 69 |
|
| 70 |
Passing `format="xml"` explicitly skips the trial-and-error and gives the real traceback if |
| 71 |
the content is not valid PROV-XML: |
| 72 |
|
| 73 |
```python |
| 74 |
loaded = prov.read("document.xml", format="xml") |
| 75 |
assert loaded == document |
| 76 |
``` |
| 77 |
|
| 78 |
## Common errors |
| 79 |
|
| 80 |
Malformed XML raises `lxml`'s own syntax error, not a `prov`-specific exception: |
| 81 |
|
| 82 |
```python |
| 83 |
try: |
| 84 |
pm.ProvDocument.deserialize(content="not xml", format="xml") |
| 85 |
except Exception as e: |
| 86 |
print(f"{type(e).__name__}: {e}") |
| 87 |
``` |
| 88 |
|
| 89 |
```text |
| 90 |
XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1 (<string>, line 1) |
| 91 |
``` |