Repository navigation
from xml: add --namespaces to keep namespace prefixes and xmlns - #19649
Conversation
`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]>
There was a problem hiding this comment.
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
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]>
|
Thanks for the detailed review! I pushed 19a3005 with all three:
|
There was a problem hiding this comment.
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
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
|
Thanks for catching this, and for the suggested fixes! I pushed e0ae46d:
|
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
|
Thanks |



Description
Fixes #11523.
from xmlkept only local names and droppedxmlnsdeclarations, sofrom xml | to xmllost namespace information:This adds a
--namespacesflag, as suggested in the issue discussion. With it, names that belong to a prefixed namespace keep their prefix (dc:creator,xml:space), andxmlns/xmlns:*declarations are added toattributeson the element that declares them. Elements in the default namespace stay unprefixed. The record shape is the same (tag/attributes/content) andto xmlneeds no change. Without the flag the output is unchanged.User-facing changes (Release notes)
from xmlhas a new--namespacesflag. It keeps namespace prefixes in tag and attribute names (for exampledc:creator,xml:space) and addsxmlns/xmlns:<prefix>keys toattributes, sofrom xml --namespaces | to xmlkeeps the namespaces of a document. The default output is unchanged.from xmlnow saysattributesinstead ofattrs.Additional notes
crates/nu-command/src/formats/from/xml.rsand round-trip tests incrates/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 fortest_examples.cargo fmt --all --check,cargo nuclippy,typos,cargo test -p nu-command --all-targets(1016 + 5422 passed) and the nu-std tests.--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.xmlns:xmldeclaration is not shown because roxmltree does not expose it.