fix(xml): namespace prefixes are dropped when reading - #559
Open
VXNCXNX wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reading namespaced XML drops every prefix and turns the
xmlns:declarationsinto ordinary attributes, so the output is not the document that went in.
soap="..."is not a valid replacement forxmlns:soap="...", so a SOAPenvelope round-tripped through dasel comes out broken. The same happens to
attributes:
<root xml:lang="en" xmlns:xsi="..."><a xsi:nil="true"/></root>became
<root lang="en" xsi="..."><a nil="true">.Cause
encoding/xmlresolves a prefix to a URI, givingName{Space: "http://...", Local: "Envelope"}. The reader used only.Local, sothe prefix was gone, and the
xmlns:soapdeclaration was carried through as anattribute whose name had also lost its
xmlnsprefix.The fix
Build a scoped prefix map from the
xmlnsdeclarations on each element, chainedto the enclosing scopes, and use it to re-qualify element and attribute names on
read. Covered: the default namespace stays unprefixed, the implicit
xml:prefixis known without declaration, and an undeclared prefix is preserved verbatim
rather than guessed at.
The writer already round-trips whatever name it is given, so this is a
reader-only change.
Verification
TestXMLNamespaceRoundTrip, five sub-cases.Reverting
scope.qualify(element.Name)toelement.Name.Localfails it:go test ./...passes across every package,go vetis clean, andgolangci-lint run ./parsing/xml/...reports 0 issues.gofumpt -l .flags around 50 pre-existing files, including ones I did nottouch, so I left formatting alone.
The part that needs your decision
This changes the JSON representation of namespaced XML, because the names are now
correct:
Anyone selecting a namespaced element by its unprefixed name today would need to
update their query. I think the new keys are the right data, since the old ones
described a document that did not exist, but it is a visible break and your call
rather than mine. Documents without namespaces are completely unaffected.
Disclosure: written with AI assistance (Claude Code). I produced the before and after by running binaries built from each tree against the documents shown, and ran the mutation check myself.