File tree Expand file tree Collapse file tree 1 file changed +7
-3
lines changed Expand file tree Collapse file tree 1 file changed +7
-3
lines changed Original file line number Diff line number Diff line change @@ -36,10 +36,14 @@ def fetch_sitemap(url):
3636 return response .text
3737
3838def prettify_xml (element ):
39- """Prettify and return a string representation of the XML."""
39+ """Prettify and return a string representation of the XML without XML declaration ."""
4040 rough_string = ET .tostring (element , encoding = 'utf-8' )
4141 reparsed = minidom .parseString (rough_string )
42- return reparsed .toprettyxml (indent = " " )
42+ pretty = reparsed .toprettyxml (indent = " " )
43+ # Remove the XML declaration
44+ lines = pretty .split ('\n ' )
45+ lines = [line for line in lines if line .strip () and not line .strip ().startswith ('<?xml' )]
46+ return '\n ' .join (lines )
4347
4448def encode_url (url ):
4549 """Encode the URL to make it XML-safe and RFC-compliant."""
@@ -140,7 +144,7 @@ def main():
140144
141145 new_root .append (url_entry )
142146
143- # Save prettified XML to file
147+ # Save prettified XML to file without XML declaration
144148 beautified_xml = prettify_xml (new_root )
145149 with open ("sitemap.xml" , "w" , encoding = "utf-8" ) as f :
146150 f .write (beautified_xml )
You can’t perform that action at this time.
0 commit comments