1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-06-30 07:50:43 +00:00

[PR #12148/5004c9f7 backport][stable-12] xml: preserve DOCTYPE declaration when writing XML files (#12248)

xml: preserve DOCTYPE declaration when writing XML files (#12148)

* fix(xml): preserve DOCTYPE declaration when writing XML files

Pass `doctype=tree.docinfo.doctype` to all `ElementTree.write()` calls
so lxml does not silently drop the DOCTYPE on serialization. Also replace
`etree.tostring()` with BytesIO+write() in the diff and xmlstring paths
for consistency.

Fixes #2762



* test(xml): add integration test for DOCTYPE preservation



* feat(changelog): add fragment for xml DOCTYPE fix (#12148)



---------


(cherry picked from commit 5004c9f70f)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
patchback[bot] 2026-06-13 17:33:13 +02:00 committed by GitHub
parent 8babb3e3e9
commit b4e3d32e5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 99 additions and 8 deletions

View file

@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE foo SYSTEM "foo.dtd">
<foo value="0"/>

View file

@ -0,0 +1,3 @@
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
SPDX-License-Identifier: GPL-3.0-or-later
SPDX-FileCopyrightText: Ansible Project

View file

@ -0,0 +1,3 @@
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE foo SYSTEM "foo.dtd">
<foo value="1"/>

View file

@ -0,0 +1,3 @@
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
SPDX-License-Identifier: GPL-3.0-or-later
SPDX-FileCopyrightText: Ansible Project

View file

@ -78,6 +78,7 @@
- include_tasks: test-print-match.yml
- include_tasks: test-xmlstring.yml
- include_tasks: test-children-elements-xml.yml
- include_tasks: test-preserve-doctype.yml
# Unicode tests
- include_tasks: test-add-children-elements-unicode.yml

View file

@ -0,0 +1,46 @@
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: Setup test fixture
copy:
src: fixtures/ansible-xml-doctype.xml
dest: /tmp/ansible-xml-doctype.xml
- name: Set '/foo/@value' to '1' (file has DOCTYPE)
xml:
path: /tmp/ansible-xml-doctype.xml
xpath: /foo
attribute: value
value: "1"
register: set_attribute_doctype
- name: Add trailing newline
shell: echo "" >> /tmp/ansible-xml-doctype.xml
- name: Compare to expected result
copy:
src: results/test-preserve-doctype.xml
dest: /tmp/ansible-xml-doctype.xml
check_mode: true
diff: true
register: comparison
- name: Test expected result (DOCTYPE is preserved)
assert:
that:
- set_attribute_doctype is changed
- comparison is not changed
- name: Set '/foo/@value' to '1' again (idempotency)
xml:
path: /tmp/ansible-xml-doctype.xml
xpath: /foo
attribute: value
value: "1"
register: set_attribute_doctype_idempotent
- name: Test idempotency
assert:
that:
- set_attribute_doctype_idempotent is not changed