77
88
99def lxml_dump_node (node ):
10- return etree .tostring (node , method = "xml" ).decode ("utf8" )
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
1127
1228
1329# This code is taken from Python 3.7. The addition is escaping of the tab
@@ -45,19 +61,24 @@ def lxml_escape_title(string: str) -> str:
4561# FIXME: Would be great to find a better solution for this.
4662def lxml_stringify_namespaced_children (node , namespace_tag = None ) -> str :
4763 if namespace_tag is None :
48- assert (
49- len (node .nsmap ) > 0
50- ), f"This method must be called on a namespaced tag:\n { lxml_dump_node (node )} " # noqa: E501
64+ assert len (node .nsmap ) > 0 , (
65+ f"This method must be called on a namespaced tag. "
66+ f"Tag: { node } , line: { node .sourceline } ."
67+ )
5168 nskey = next (iter (node .nsmap .keys ()))
5269 else :
5370 nskey = namespace_tag
5471
5572 def _lxml_stringify_reqif_ns_node (node ):
5673 assert node is not None
57-
5874 output = ""
5975 node_no_ns_tag = etree .QName (node ).localname
60- output += f"<{ nskey } :{ node_no_ns_tag } "
76+ tag = (
77+ f"{ nskey } :{ node_no_ns_tag } "
78+ if node .tag [0 ] == "{" or namespace_tag is not None
79+ else node .tag
80+ )
81+ output += f"<{ tag } "
6182 for attribute , attribute_value in node .attrib .items ():
6283 output += f' { attribute } ="{ lxml_escape_for_html (attribute_value )} "'
6384 if node .text is not None or len (node .getchildren ()) > 0 :
@@ -66,7 +87,7 @@ def _lxml_stringify_reqif_ns_node(node):
6687 output += lxml_escape_for_html (node .text )
6788 for child in node .getchildren ():
6889 output += _lxml_stringify_reqif_ns_node (child )
69- output += f"</{ nskey } : { node_no_ns_tag } >"
90+ output += f"</{ tag } >"
7091 else :
7192 output += "/>"
7293
0 commit comments