|
7 | 7 |
|
8 | 8 |
|
9 | 9 | 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) |
27 | 11 |
|
28 | 12 |
|
29 | 13 | # 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): |
103 | 87 | return string |
104 | 88 |
|
105 | 89 |
|
| 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 | + |
106 | 114 | # https://stackoverflow.com/a/28173933/598057 |
107 | 115 | def lxml_stringify_children(node): |
108 | 116 | return "".join( |
|
0 commit comments