Skip to content

Commit 84b740e

Browse files
authored
Merge pull request #116 from strictdoc-project/stanislaw/develop
helpers/lxml: make lxml_stringify_node a standalone method
2 parents 9f329a9 + fae1c62 commit 84b740e

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

reqif/helpers/lxml.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,7 @@
77

88

99
def lxml_dump_node(node):
10-
output = ""
11-
node_no_ns_tag = etree.QName(node).localname
12-
output += f"<{node_no_ns_tag}"
13-
for attribute, attribute_value in node.attrib.items():
14-
output += f' {attribute}="{lxml_escape_for_html(attribute_value)}"'
15-
if node.text is not None or len(node.getchildren()) > 0:
16-
output += ">"
17-
if len(node.nsmap) > 0:
18-
output += lxml_stringify_namespaced_children(node)
19-
else:
20-
output += lxml_stringify_children(node)
21-
output += f"</{node_no_ns_tag}>"
22-
else:
23-
output += "/>"
24-
if node.tail is not None:
25-
output += lxml_escape_for_html(node.tail)
26-
return output
10+
return lxml_stringify_node(node)
2711

2812

2913
# This code is taken from Python 3.7. The addition is escaping of the tab
@@ -103,6 +87,30 @@ def _lxml_stringify_reqif_ns_node(node):
10387
return string
10488

10589

90+
def lxml_stringify_node(node):
91+
nskey = None
92+
if len(node.nsmap) > 0:
93+
nskey = next(iter(node.nsmap.keys()))
94+
output = ""
95+
node_no_ns_tag = etree.QName(node).localname
96+
tag = f"{nskey}:{node_no_ns_tag}" if node.tag[0] == "{" else node.tag
97+
output += f"<{tag}"
98+
for attribute, attribute_value in node.attrib.items():
99+
output += f' {attribute}="{lxml_escape_for_html(attribute_value)}"'
100+
if node.text is not None or len(node.getchildren()) > 0:
101+
output += ">"
102+
if node.text is not None:
103+
output += lxml_escape_for_html(node.text)
104+
for child in node.getchildren():
105+
output += lxml_stringify_node(child)
106+
output += f"</{tag}>"
107+
else:
108+
output += "/>"
109+
if node.tail is not None:
110+
output += lxml_escape_for_html(node.tail)
111+
return output
112+
113+
106114
# https://stackoverflow.com/a/28173933/598057
107115
def lxml_stringify_children(node):
108116
return "".join(

0 commit comments

Comments
 (0)