Repository navigation
Closed
Labels
A:file-formatParsing/Writing of file formats/protocols
Description
Describe the bug
Because of the way XML is read into a table, it is impossible to get an element without a list index in the cellpath. The list index is dependant upon the sibling elements in the source XML, which means that nu code referring to an XML element may break when run on similar XML input where the target element has different siblings.
How to reproduce
junk.xml
<root>
<junk>
<foo type="string" value="the_quick_brown_fox" />
<bar type="int" value="99" />
<baz type="string" value="gum" />
<quux type="int" value="42" />
</junk>
</root>With the above file junk.xml,
❯ open junk.xml | get root.children.junk.children.0.bar
╭───┬───────────────────────────────────────────────╮
│ 0 │ │
│ 1 │ ╭───┬────────────────┬──────────────────────╮ │
│ │ │ # │ children │ attributes │ │
│ │ ├───┼────────────────┼──────────────────────┤ │
│ │ │ 0 │ [list 0 items] │ ╭───┬──────┬───────╮ │ │
│ │ │ │ │ │ # │ type │ value │ │ │
│ │ │ │ │ ├───┼──────┼───────┤ │ │
│ │ │ │ │ │ 0 │ int │ 99 │ │ │
│ │ │ │ │ ╰───┴──────┴───────╯ │ │
│ │ ╰───┴────────────────┴──────────────────────╯ │
│ 2 │ │
│ 3 │ │
╰───┴───────────────────────────────────────────────╯You would have to then get the item at index 1 in the list to get the actual element.
Expected behavior
I expected the nu representation of an XML document to be a tree of records, which would be much more straightforward to work with.
Screenshots
No response
Configuration
| key | value |
|---|---|
| version | 0.73.0 |
| branch | |
| commit_hash | |
| build_os | linux-x86_64 |
| build_target | x86_64-unknown-linux-gnu |
| rust_version | rustc 1.65.0 |
| cargo_version | cargo 1.65.0 |
| pkg_version | 0.73.0 |
| build_time | 1980-01-01 00:00:00 +00:00 |
| build_rust_channel | release |
| features | database, default, trash, which, zip |
| installed_plugins |
Additional context
The full table for the children of <junk>.
$ open junk.xml | get root.children.junk.children
╭───────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ 0 │ ╭───┬───────────────────────────────────────────────────────────────┬───────────────────────────────────────────────┬─────────────────────────────────────────────────┬───────────────────────────────────────────────╮ │
│ │ │ # │ foo │ bar │ baz │ quux │ │
│ │ ├───┼───────────────────────────────────────────────────────────────┼───────────────────────────────────────────────┼─────────────────────────────────────────────────┼───────────────────────────────────────────────┤ │
│ │ │ 0 │ ╭───┬────────────────┬──────────────────────────────────────╮ │ ❎ │ ❎ │ ❎ │ │
│ │ │ │ │ # │ children │ attributes │ │ │ │ │ │
│ │ │ │ ├───┼────────────────┼──────────────────────────────────────┤ │ │ │ │ │
│ │ │ │ │ 0 │ [list 0 items] │ ╭───┬────────┬─────────────────────╮ │ │ │ │ │ │
│ │ │ │ │ │ │ │ # │ type │ value │ │ │ │ │ │ │
│ │ │ │ │ │ │ ├───┼────────┼─────────────────────┤ │ │ │ │ │ │
│ │ │ │ │ │ │ │ 0 │ string │ the_quick_brown_fox │ │ │ │ │ │ │
│ │ │ │ │ │ │ ╰───┴────────┴─────────────────────╯ │ │ │ │ │ │
│ │ │ │ ╰───┴────────────────┴──────────────────────────────────────╯ │ │ │ │ │
│ │ │ 1 │ ❎ │ ╭───┬────────────────┬──────────────────────╮ │ ❎ │ ❎ │ │
│ │ │ │ │ │ # │ children │ attributes │ │ │ │ │
│ │ │ │ │ ├───┼────────────────┼──────────────────────┤ │ │ │ │
│ │ │ │ │ │ 0 │ [list 0 items] │ ╭───┬──────┬───────╮ │ │ │ │ │
│ │ │ │ │ │ │ │ │ # │ type │ value │ │ │ │ │ │
│ │ │ │ │ │ │ │ ├───┼──────┼───────┤ │ │ │ │ │
│ │ │ │ │ │ │ │ │ 0 │ int │ 99 │ │ │ │ │ │
│ │ │ │ │ │ │ │ ╰───┴──────┴───────╯ │ │ │ │ │
│ │ │ │ │ ╰───┴────────────────┴──────────────────────╯ │ │ │ │
│ │ │ 2 │ ❎ │ ❎ │ ╭───┬────────────────┬────────────────────────╮ │ ❎ │ │
│ │ │ │ │ │ │ # │ children │ attributes │ │ │ │
│ │ │ │ │ │ ├───┼────────────────┼────────────────────────┤ │ │ │
│ │ │ │ │ │ │ 0 │ [list 0 items] │ ╭───┬────────┬───────╮ │ │ │ │
│ │ │ │ │ │ │ │ │ │ # │ type │ value │ │ │ │ │
│ │ │ │ │ │ │ │ │ ├───┼────────┼───────┤ │ │ │ │
│ │ │ │ │ │ │ │ │ │ 0 │ string │ gum │ │ │ │ │
│ │ │ │ │ │ │ │ │ ╰───┴────────┴───────╯ │ │ │ │
│ │ │ │ │ │ ╰───┴────────────────┴────────────────────────╯ │ │ │
│ │ │ 3 │ ❎ │ ❎ │ ❎ │ ╭───┬────────────────┬──────────────────────╮ │ │
│ │ │ │ │ │ │ │ # │ children │ attributes │ │ │
│ │ │ │ │ │ │ ├───┼────────────────┼──────────────────────┤ │ │
│ │ │ │ │ │ │ │ 0 │ [list 0 items] │ ╭───┬──────┬───────╮ │ │ │
│ │ │ │ │ │ │ │ │ │ │ # │ type │ value │ │ │ │
│ │ │ │ │ │ │ │ │ │ ├───┼──────┼───────┤ │ │ │
│ │ │ │ │ │ │ │ │ │ │ 0 │ int │ 42 │ │ │ │
│ │ │ │ │ │ │ │ │ │ ╰───┴──────┴───────╯ │ │ │
│ │ │ │ │ │ │ ╰───┴────────────────┴──────────────────────╯ │ │
│ │ ╰───┴───────────────────────────────────────────────────────────────┴───────────────────────────────────────────────┴─────────────────────────────────────────────────┴───────────────────────────────────────────────╯ │
╰───────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯Metadata
Metadata
Assignees
Labels
A:file-formatParsing/Writing of file formats/protocols
Activity
webbedspace commentedon Jan 5, 2023
Hmm, yeah, I noticed that
to xmlmakes thechildrenattributes of nodes be unwieldy tables with holes everywhere, and I think it's a bit naff myself.fdncred commentedon Jan 5, 2023
XML has been the worst for years. I'd love to have someone fix it so that it works like the other "from/to format" commands.
NotLebedev commentedon Jan 31, 2023
If nobody works on this issue I can do it. Fixed a couple bugs related to format conversions lately, so I think I can do it.
aidalgol commentedon Jan 31, 2023
I have a tab open to this issue to get to eventually, but I don't know when that will be. It would be my first contribution to Nushell, though, so it may be better for someone already familiar with the codebase to pick up.
fdncred commentedon Jan 31, 2023
@NotLebedev @aidalgol you should feel free to submit PRs for this type of cleanup. Maybe you could work together? This usually happens by someone putting up a draft PR and then there's discussion in the PR.
NotLebedev commentedon Jan 31, 2023
@fdncred I am all for collaboration. I will start some work on this issue
NotLebedev commentedon Feb 1, 2023
So I thought this through and nailed format a bit better.
Consider this example:
This would translate to somewhat unpleasnat:

I suggest it is instead translated into:

This changes how one would access elements and require a bit more verbose approach. For example to get

b->c->attr2:This is a bit more verbose, however this should not require user to guess index as in old format. This guessing requires usage of

get -iwhich yields roughly the same length of query (and still requires to filter array for empty elements):Another problem with old format is example as follows:
Currently it results in



where children is not even a table and can not be worked on in intended way. Instead I suggest this conversion:
Note that tag and attrs are null (see nuon dump below) which allows to use
wherethe same way as in examples aboveEBNF grammar for this suggestion:
fdncred commentedon Feb 1, 2023
This is an interesting take on it. It seems like it would be more queryable via nushell this way.
rgwood commentedon Feb 1, 2023
Very interesting. Thanks for the detailed proposal @NotLebedev.
My first reaction is that that would be a nice improvement.
sophiajt commentedon Feb 8, 2023
@aidalgol - We were brainstorming some alternate ways we could decode xml into nushell on discord here: https://canary.discord.com/channels/601130461678272522/615329862395101194/1072774585671491668
My hope is that we can come up with a solution that decodes pretty cleanly, is easy to read and work with, and which isn't ambiguous so it can still be encoded to get the same output.
It'd be good to get your input since you've already been looking into the complexities of working with xml.
sophiajt commentedon Feb 8, 2023
For folks who land here who may not have easy access to the discord, here's one example that's linked aboved:
Could possibly be handled as:

(with the caveat that the duplicate
<dependencies>tag should possibly decoded into an array instead of a record.New xml format (#7947)
kronberger-droid commentedon Nov 13, 2026
The XML format got reworked in #7947 into
{tag, attributes, content}records.Elements are selected by tag now, e.g. with
where tag == baror withxaccess [root junk bar]fromstd/xml.So the index no longer depends on the siblings.