Skip to content

from xml: add --namespaces to keep namespace prefixes and xmlns - #19649

Merged
fdncred merged 4 commits into
nushell:mainfrom
cemaliplt:from-xml-namespaces
Nov 24, 2026
Merged

fdncred merged 4 commits into
nushell:mainfrom
cemaliplt:from-xml-namespaces

Conversation

@cemaliplt

Copy link
Copy Markdown
Contributor

Description

Fixes #11523.

from xml kept only local names and dropped xmlns declarations, so from xml | to xml lost namespace information:

'<svg xmlns="http://www.w3.org/2000/svg" width="1"><text xml:space="preserve">a</text></svg>' | from xml | to xml
# before: <svg width="1"><text space="preserve">a</text></svg>

This adds a --namespaces flag, as suggested in the issue discussion. With it, names that belong to a prefixed namespace keep their prefix (dc:creator, xml:space), and xmlns / xmlns:* declarations are added to attributes on the element that declares them. Elements in the default namespace stay unprefixed. The record shape is the same (tag / attributes / content) and to xml needs no change. Without the flag the output is unchanged.

'<svg xmlns="http://www.w3.org/2000/svg" width="1"><text xml:space="preserve">a</text></svg>' | from xml --namespaces | to xml
# <svg xmlns="http://www.w3.org/2000/svg" width="1"><text xml:space="preserve">a</text></svg>

User-facing changes (Release notes)

  • from xml has a new --namespaces flag. It keeps namespace prefixes in tag and attribute names (for example dc:creator, xml:space) and adds xmlns / xmlns:<prefix> keys to attributes, so from xml --namespaces | to xml keeps the namespaces of a document. The default output is unchanged.
  • The help text of from xml now says attributes instead of attrs.

Additional notes

  • Tests: unit tests in crates/nu-command/src/formats/from/xml.rs and round-trip tests in crates/nu-command/tests/format_conversions/xml.rs. The flag tests fail on main; the default-output tests pin the unchanged behaviour. There is also one example for test_examples.
  • Run on my machine: cargo fmt --all --check, cargo nuclippy, typos, cargo test -p nu-command --all-targets (1016 + 5422 passed) and the nu-std tests.
  • Without the flag the output is byte for byte the same as before on 304 XML files from my system (293 identical, 11 fail to parse the same way in both).
  • Timing (debug build, median of 5, files of 2-3 MB): without the flag it is within noise of main. With --namespaces, a file with a few namespaces takes about 10-25% longer, and a file with 200 declarations on the root takes about 3x (0.3 s to 1 s). I did not measure a release build.
  • Known differences with the flag: if several prefixes are bound to the same namespace, the prefix written back can differ from the original (same document); declarations are written before the other attributes; an explicit xmlns:xml declaration is not shown because roxmltree does not expose it.
  • I used AI assistance (Claude Code) while writing the code, the tests and this description. I read the diff, understand how it works, and ran the checks above myself.

`from xml` kept only local names and dropped `xmlns` declarations, so
`from xml | to xml` turned `<svg xmlns="..."><text xml:space="preserve">`
into `<svg><text space="preserve">` (nushell#11523). roxmltree does not list
`xmlns` in `attributes()`, so they were lost while parsing.

With `--namespaces`, names that belong to a prefixed namespace keep their
prefix (`dc:creator`, `xml:space`), and `xmlns` / `xmlns:*` declarations
are added to `attributes` on the element that declares them. Without the
flag the output is unchanged.

Verified: the issue example and an RSS feed with `dc:creator` round trip
through `from xml --namespaces | to xml`; default output is byte for byte
the same as before on 304 XML files; xml tests, the nu-std tests and the
nu-command suite pass.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

The issue's SVG and RSS examples round trip through from xml --namespaces | to xml, and the default output matches main. The main gap is that the flag rebuilds prefixes from the URI, so a document that binds one URI twice can come back with a different prefix than it wrote. Two help-text nits after that.

Findings: 0 High severity · 1 Medium severity · 2 Low severity

Open (3)

Notes from my clanker, checked on c93ef7f. Built the branch and ran each case on the branch binary and on main (1dc849d). CI's clippy, fmt, typos and tests are green on this head, so I didn't rerun them. For the prefix finding I built the fix as a local patch: every case in the prefix thread kept its source prefix, and cargo test -p nu-command --lib -- formats::from::xml (20 passed, 1 failed) and cargo test -p nu-command --test tests -- format_conversions::xml (14 passed, 1 failed) failed only the two tests that expect the dropped prefix.

The prefix was rebuilt from the namespace URI, so a document that binds
one URI twice (`xmlns` and `xmlns:svg`, or `xmlns` and `xmlns:p`) could
come back with a different prefix, and `where tag == 'xs:element'`
silently matched nothing. Names are now taken exactly as written in the
start tag through roxmltree's source positions, which also removes the
per-element URI scans.

The help text now describes the flag itself, and notes that a sub-record
keeps its prefixes but not the `xmlns` keys of its ancestors.

Verified: new tests fail before and pass after (the four cases from the
review, a newline in a start tag, a BOM, a DTD entity, non-ASCII names);
default output is unchanged on 304 XML files; xml tests, nuclippy, typos,
fmt, the nu-std tests and the nu-command suite pass.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@cemaliplt

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review! I pushed 19a3005 with all three:

  • Names now come from the source text, so svg:rect, xs:element, dc:t and q:b q:k keep the prefix they were written with. I added your four cases as tests, plus a newline inside a start tag, a BOM, a DTD entity and non-ASCII names.
  • The help text now describes the flag itself, and has the sub-record note.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

All three threads from my first review are fixed, and I resolved them. Two small things in the new name reading are left: an edge case in the name scan I suggested, and a debug print in the DTD test.

Findings: 0 High severity · 0 Medium severity · 2 Low severity

Open (2)

Notes from my clanker, checked on 19a3005. Built the branch, reran the first review's probe, and ran the new name reading on newlines, tabs, CRLF, a BOM, DTD entities and non-ASCII names. Built both suggestions as trials: the U+1680 fix changes none of those outputs, and the xml unit tests pass with the new test, 27 of 27. CI is green on this head, so I didn't rerun the full tests or clippy.

The `--namespaces` name scan stopped at any `char::is_whitespace()`, but XML
whitespace is only space, tab, CR and LF. U+1680 is a valid XML name character
and was cut: `<a\u{1680}b/>` came back as `a`. The scan now stops at those
four characters, `/` and `>`.

The DTD entity test no longer prints debug output; it now checks that an
element coming from an entity keeps its written name (`p:b`).

Verified: the new U+1680 test fails with the old scan (tag `a`) and passes
with the new one; the 28 xml unit tests pass.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VoTjkG6zFJUYFnoLSuBz28
@cemaliplt

Copy link
Copy Markdown
Contributor Author

Thanks for catching this, and for the suggested fixes! I pushed e0ae46d:

  • The name scan now stops only at XML whitespace (space, tab, CR, LF), so <a\u{1680}b/> keeps its full name. I added a test for it, and it fails with the old scan.
  • The DTD entity test no longer prints debug output, and it now checks that the element keeps its written name (p:b), using your version.

clippy::manual_pattern_char_comparison fails the lint job on the closure
form. Same behavior; the 28 xml unit tests pass.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VoTjkG6zFJUYFnoLSuBz28
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Nov 24, 2026
@fdncred
fdncred merged commit 1a76df9 into nushell:main Nov 24, 2026
14 checks passed
@fdncred

fdncred commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

from xml & to xml lose XML namespace information

2 participants