Skip to content

Commit 55898d1

Browse files
authored
Update generate_sitemap.py
1 parent 39dbacc commit 55898d1

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

generate_sitemap.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ def fetch_sitemap(url):
3636
return response.text
3737

3838
def 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

4448
def 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)

0 commit comments

Comments
 (0)