Skip to content

Commit b56c55b

Browse files
authored
Merge pull request #5 from dapper91/dev
- default namespace support added.
2 parents 9099115 + 28c5bf7 commit b56c55b

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ Changelog
22
=========
33

44

5-
0.2.0 (2021-08-19)
5+
0.2.1 (2022-10-06)
6+
------------------
7+
8+
- default namespace support added.
9+
10+
11+
0.2.0 (2022-08-19)
612
------------------
713

814
- generic models support
915
- namespace inheritance bug fixed.
1016

1117

12-
0.1.0 (2021-08-17)
18+
0.1.0 (2022-08-17)
1319
------------------
1420

1521
- Initial release

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
pydantic xml extension
2121

2222

23+
## Installation
24+
25+
```commandline
26+
pip install pydantic-xml[lxml]
27+
```
28+
29+
* **lxml** - use `lxml` instead of standard `xml.etree.ElementTree` library
30+
2331
## Quickstart
2432

2533
`pydantic-xml` is a pydantic extension implementing model xml serialization/deserialization.
@@ -270,6 +278,26 @@ print(company)
270278

271279
### Namespace resolution:
272280

281+
#### Default namespace
282+
283+
To parse xml documents with a declared default namespace add it to `nsmap` with an empty key.
284+
Look at the following example:
285+
286+
```xml
287+
<headquarters xmlns="http://www.test.com/hq">
288+
<country>US</country>
289+
<state>California</state>
290+
<city>Hawthorne</city>
291+
</headquarters>
292+
```
293+
294+
```python
295+
class Headquarters(BaseXmlModel, tag='headquarters', nsmap={'': 'http://www.test.com/hq'}):
296+
country: str = element()
297+
state: str = element()
298+
city: str = element()
299+
```
300+
273301
**TBD**
274302

275303

pydantic_xml/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def from_alias(cls, tag: str, ns: Optional[str] = None, nsmap: Optional[NsMap] =
4545
:return: qualified name
4646
"""
4747

48-
return QName(tag=tag, ns=nsmap.get(ns) if ns and nsmap else None)
48+
return QName(tag=tag, ns=nsmap.get(ns or '') if nsmap else None)
4949

5050
@property
5151
def uri(self) -> str:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydantic-xml"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
description = "pydantic xml serialization/deserialization extension"
55
authors = ["Dmitry Pershin <dapper1291@gmail.com>"]
66
license = "Unlicense"

tests/test_namespaces.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@
66
from pydantic_xml import BaseXmlModel, attr, element, wrapped
77

88

9+
def test_default_namespaces():
10+
class TestSubMode2(BaseXmlModel):
11+
element: str = element()
12+
13+
class TestSubModel1(BaseXmlModel):
14+
submodel2: TestSubMode2 = element(nsmap={'': 'http://test2.org'})
15+
16+
class TestModel(BaseXmlModel, tag='model'):
17+
submodel1: TestSubModel1 = element(nsmap={'': 'http://test1.org'})
18+
19+
xml = '''
20+
<model>
21+
<submodel1 xmlns="http://test1.org">
22+
<submodel2 xmlns="http://test2.org">
23+
<element>value</element>
24+
</submodel2>
25+
</submodel1>
26+
</model>
27+
'''
28+
29+
actual_obj = TestModel.from_xml(xml)
30+
expected_obj = TestModel(
31+
submodel1=TestSubModel1(submodel2=TestSubMode2(element='value')),
32+
)
33+
34+
assert actual_obj == expected_obj
35+
36+
actual_xml = actual_obj.to_xml()
37+
assert_xml_equal(actual_xml, xml)
38+
39+
940
@pytest.mark.parametrize(
1041
'model_ns, element_ns, expected_model_ns, expected_element_ns',
1142
[

0 commit comments

Comments
 (0)