Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
269 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Optional | ||
from xml.etree.ElementTree import canonicalize | ||
|
||
from pydantic_xml import BaseXmlModel, element | ||
|
||
|
||
class Company(BaseXmlModel): | ||
title: Optional[str] = element(default=None, nillable=True) | ||
|
||
|
||
xml_doc = ''' | ||
<Company> | ||
<title xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" /> | ||
</Company> | ||
''' | ||
|
||
company = Company.from_xml(xml_doc) | ||
|
||
assert company.title is None | ||
assert canonicalize(company.to_xml(), strip_text=True) == canonicalize(xml_doc, strip_text=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .element import SearchMode, XmlElement, XmlElementReader, XmlElementWriter | ||
from .utils import is_element_nill, make_element_nill |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from .element import XmlElementReader, XmlElementWriter | ||
|
||
XSI_NS = 'http://www.w3.org/2001/XMLSchema-instance' | ||
|
||
|
||
def is_element_nill(element: XmlElementReader) -> bool: | ||
if (is_nil := element.pop_attrib('{%s}nil' % XSI_NS)) and is_nil == 'true': | ||
return True | ||
else: | ||
return False | ||
|
||
|
||
def make_element_nill(element: XmlElementWriter) -> None: | ||
element.set_attribute('{%s}nil' % XSI_NS, 'true') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.