Skip to content

Fix ElementSerializer producing invalid xmlns:= and duplicate namespace declarations#255

Merged
Bergmann89 merged 1 commit intoBergmann89:masterfrom
AZX-PBC-OSS:fix/element-xmlns-roundtrip
Mar 16, 2026
Merged

Fix ElementSerializer producing invalid xmlns:= and duplicate namespace declarations#255
Bergmann89 merged 1 commit intoBergmann89:masterfrom
AZX-PBC-OSS:fix/element-xmlns-roundtrip

Conversation

@rcbevans
Copy link
Copy Markdown
Contributor

@rcbevans rcbevans commented Mar 9, 2026

Fixes #254

Key changes

SerializeHelper::write_xmlns (xsd-parser-types/src/quick_xml/serialize.rs):

  • Normalize Some(NamespacePrefix("")) to None at the top of write_xmlns, so the default namespace is correctly matched regardless of whether it was registered by generated code (None) or from Element::namespaces (empty byte slice). This also prevents add_namespace_entry from producing the invalid xmlns: attribute name.

ElementSerializer::next_item (xsd-parser-types/src/xml/element.rs):

  • Normalize empty prefix to None before calling write_xmlns in the namespaces loop
  • Filter xmlns attributes from element.attributes when a corresponding entry exists in element.namespaces, preventing double emission. Attributes added only via .attribute() without a matching .namespace() entry are preserved.

Tests added

Test Scenario
roundtrip_with_default_namespace Default namespace + prefixed namespace round-trip
roundtrip_without_namespaces Plain elements, no xmlns
roundtrip_with_prefixed_namespace_only Only prefixed namespace, no default
roundtrip_default_namespace_undeclaration xmlns="" undeclares default namespace (XML Namespaces 1.0 §6.2)
roundtrip_prefix_rebinding Child rebinds prefix to different URI (XML Namespaces 1.0 §6.1)
roundtrip_inherited_namespace_not_redeclared Inherited namespaces not re-emitted on child elements
attribute_only_xmlns_is_preserved Xmlns via .attribute() only (no .namespace()) preserved

Verification

cargo test -p xsd-parser-types --features quick-xml --lib xml::element::tests

@rcbevans rcbevans force-pushed the fix/element-xmlns-roundtrip branch from 34e56e9 to 4725555 Compare March 9, 2026 23:36
@Bergmann89
Copy link
Copy Markdown
Owner

Hey @rcbevans,

Thanks for providing this fix. I have limited time this week, so it may take me until the weekend to complete the review.

…ations in ElementSerializer

Normalize empty namespace prefix to None in both ElementSerializer and
SerializeHelper::write_xmlns so the default namespace (xmlns=) is
correctly recognized regardless of whether it was registered by
generated code (None) or from Element::namespaces (empty byte slice).

Filter xmlns attributes from Element::attributes during serialization
when a corresponding entry exists in Element::namespaces, preventing
double emission. Attributes added only via .attribute() without a
matching .namespace() entry are preserved.
@rcbevans rcbevans force-pushed the fix/element-xmlns-roundtrip branch from 4725555 to eed96f8 Compare March 10, 2026 21:42
@Bergmann89 Bergmann89 merged commit fcb171d into Bergmann89:master Mar 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ElementSerializer produces invalid xmlns:= and duplicate namespace declarations

2 participants