@@ -251,18 +251,11 @@ def other_namespaces(self) -> dict:
251
251
"""
252
252
253
253
nmsp_dict : dict [str , str ] = {}
254
- if self .namespaces and self .prefix is None :
255
- nmsp_dict = {
256
- "xmlns" : n # noqa: RUF011
257
- for p , n in self .namespaces .items ()
258
- if p != ""
259
- }
260
-
261
- if self .namespaces and self .prefix :
254
+ if self .namespaces :
262
255
nmsp_dict = {
263
- "xmlns" : n # noqa: RUF011
256
+ f "xmlns{ p if p == '' else f': { p } ' } " : n
264
257
for p , n in self .namespaces .items ()
265
- if p == ""
258
+ if n != self . prefix_uri [ 1 : - 1 ]
266
259
}
267
260
268
261
return nmsp_dict
@@ -365,16 +358,16 @@ def build_tree(self) -> bytes:
365
358
elem_row = self .build_attribs (d , elem_row )
366
359
self .build_elems (d , elem_row )
367
360
368
- self .out_xml = tostring (self .root , method = "xml" , encoding = self .encoding )
361
+ self .out_xml = tostring (
362
+ self .root ,
363
+ method = "xml" ,
364
+ encoding = self .encoding ,
365
+ xml_declaration = self .xml_declaration ,
366
+ )
369
367
370
368
if self .pretty_print :
371
369
self .out_xml = self .prettify_tree ()
372
370
373
- if self .xml_declaration :
374
- self .out_xml = self .add_declaration ()
375
- else :
376
- self .out_xml = self .remove_declaration ()
377
-
378
371
if self .stylesheet is not None :
379
372
raise ValueError (
380
373
"To use stylesheet, you need lxml installed and selected as parser."
@@ -395,8 +388,10 @@ def get_prefix_uri(self) -> str:
395
388
uri = f"{{{ self .namespaces [self .prefix ]} }}"
396
389
except KeyError :
397
390
raise KeyError (f"{ self .prefix } is not included in namespaces" )
398
- else :
391
+ elif "" in self . namespaces :
399
392
uri = f'{{{ self .namespaces ["" ]} }}'
393
+ else :
394
+ uri = ""
400
395
401
396
return uri
402
397
@@ -418,31 +413,6 @@ def prettify_tree(self) -> bytes:
418
413
419
414
return dom .toprettyxml (indent = " " , encoding = self .encoding )
420
415
421
- def add_declaration (self ) -> bytes :
422
- """
423
- Add xml declaration.
424
-
425
- This method will add xml declaration of working tree. Currently,
426
- xml_declaration is supported in etree starting in Python 3.8.
427
- """
428
- decl = f'<?xml version="1.0" encoding="{ self .encoding } "?>\n '
429
-
430
- return (
431
- self .out_xml
432
- if self .out_xml .startswith (b"<?xml" )
433
- else decl .encode (self .encoding ) + self .out_xml
434
- )
435
-
436
- def remove_declaration (self ) -> bytes :
437
- """
438
- Remove xml declaration.
439
-
440
- This method will remove xml declaration of working tree. Currently,
441
- pretty_print is not supported in etree.
442
- """
443
-
444
- return self .out_xml .split (b"?>" )[- 1 ].strip ()
445
-
446
416
447
417
class LxmlXMLFormatter (BaseXMLFormatter ):
448
418
"""
@@ -513,8 +483,10 @@ def get_prefix_uri(self) -> str:
513
483
uri = f"{{{ self .namespaces [self .prefix ]} }}"
514
484
except KeyError :
515
485
raise KeyError (f"{ self .prefix } is not included in namespaces" )
516
- else :
486
+ elif "" in self . namespaces :
517
487
uri = f'{{{ self .namespaces ["" ]} }}'
488
+ else :
489
+ uri = ""
518
490
519
491
return uri
520
492
0 commit comments