Skip to content

Commit

Permalink
Add parameter to to_xml() to allow skipping rendering of namespaces. …
Browse files Browse the repository at this point in the history
…Used by ezEML when displaying XML in complex text editing.
  • Loading branch information
jon-ide committed Jan 18, 2024
1 parent 3d851b9 commit b75853d
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/metapype/model/metapype_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def from_xml(xml: str, clean: bool = True, collapse: bool = False, literals: tup
return root


def to_xml(node: Node, parent: Node = None, level: int = 0) -> str:
def to_xml(node: Node, parent: Node = None, level: int = 0, skip_ns: bool = False) -> str:
xml = ""
spacing = " "
indent = spacing * level
Expand All @@ -279,13 +279,14 @@ def to_xml(node: Node, parent: Node = None, level: int = 0) -> str:
if len(node.attributes) > 0:
attributes += " ".join([f"{k}=\"{v}\"" for k, v in node.attributes.items()])

if parent is None:
if len(node.nsmap) > 0:
attributes += " " + " ".join([f"xmlns:{k}=\"{v}\"" for k, v in node.nsmap.items()])
elif node.nsmap != parent.nsmap:
nsmap = _nsp_unique(node.nsmap, parent.nsmap)
if len(nsmap) > 0:
attributes += " " + " ".join([f"xmlns:{k}=\"{v}\"" for k, v in nsmap.items()])
if not skip_ns:
if parent is None:
if len(node.nsmap) > 0:
attributes += " " + " ".join([f"xmlns:{k}=\"{v}\"" for k, v in node.nsmap.items()])
elif node.nsmap != parent.nsmap:
nsmap = _nsp_unique(node.nsmap, parent.nsmap)
if len(nsmap) > 0:
attributes += " " + " ".join([f"xmlns:{k}=\"{v}\"" for k, v in nsmap.items()])

if len(node.extras) > 0:
attributes += " " + " ".join([f"{k}=\"{v}\"" for k, v in node.extras.items()])
Expand Down

0 comments on commit b75853d

Please sign in to comment.