Skip to content

Values in XML table representation cannot be referenced by tag and attribute names alone #7682

Closed

Description

@aidalgol

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    │ │ │                 │
│                   │ │   │                                                               │                                               │                                                 │ │   │                │ ╰───┴──────┴───────╯ │ │                 │
│                   │ │   │                                                               │                                               │                                                 │ ╰───┴────────────────┴──────────────────────╯ │                 │
│                   │ ╰───┴───────────────────────────────────────────────────────────────┴───────────────────────────────────────────────┴─────────────────────────────────────────────────┴───────────────────────────────────────────────╯                 │
╰───────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Activity

  1. webbedspace commented on Jan 5, 2023

    @webbedspace
    Contributor

    Hmm, yeah, I noticed that to xml makes the children attributes of nodes be unwieldy tables with holes everywhere, and I think it's a bit naff myself.

  2. fdncred commented on Jan 5, 2023

    @fdncred
    Contributor

    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.

  3. added
    A:file-format
    on Jan 5, 2023
  4. NotLebedev commented on Jan 31, 2023

    @NotLebedev
    Contributor

    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.

    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.

  5. aidalgol commented on Jan 31, 2023

    @aidalgol
    Author

    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.

    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.

  6. fdncred commented on Jan 31, 2023

    @fdncred
    Contributor

    @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.

  7. NotLebedev commented on Jan 31, 2023

    @NotLebedev
    Contributor

    @fdncred I am all for collaboration. I will start some work on this issue

  8. NotLebedev commented on Feb 1, 2023

    @NotLebedev
    Contributor

    So I thought this through and nailed format a bit better.
    Consider this example:

    <b>
      <a attr0="qwe" attr1="asd"></a>
      <c attr2="whatever"></c>
    </b>

    This would translate to somewhat unpleasnat:
    image

    I suggest it is instead translated into:
    image

    This changes how one would access elements and require a bit more verbose approach. For example to get b->c->attr2:
    image

    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 -i which yields roughly the same length of query (and still requires to filter array for empty elements):
    image

    Another problem with old format is example as follows:

    <b>
      <t0></t0>
      Some text
      <t1></t1>
    </b>

    Currently it results in
    image
    where children is not even a table and can not be worked on in intended way. Instead I suggest this conversion:
    image
    Note that tag and attrs are null (see nuon dump below) which allows to use where the same way as in examples above
    image

    EBNF grammar for this suggestion:

    nu-xml ::= entity
    entity ::= "{tag: " tag-name " attrs: {"{attr-name":" attr-value}"} children: [" {entity|text} "]}"
    text ::= "{tag: null attrs: null children: "text-value"}"
    attr-name ::= #'[A-Za-z][A-Za-z0-9_+]*'
    tag-name ::= #'[A-Za-z][A-Za-z0-9_+]*'
    attr-value ::= #'[A-Za-z][A-Za-z0-9_+]*'
    text-value ::= '"'#'[A-Za-z][A-Za-z0-9_+\s]*''"'
  9. fdncred commented on Feb 1, 2023

    @fdncred
    Contributor

    This is an interesting take on it. It seems like it would be more queryable via nushell this way.

  10. rgwood commented on Feb 1, 2023

    @rgwood
    Contributor

    Very interesting. Thanks for the detailed proposal @NotLebedev.

    My first reaction is that that would be a nice improvement.

  11. sophiajt commented on Feb 8, 2023

    @sophiajt
    Contributor

    @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.

  12. sophiajt commented on Feb 8, 2023

    @sophiajt
    Contributor

    For folks who land here who may not have easy access to the discord, here's one example that's linked aboved:

    <package>
        <name>foo</name>
        <version>1.0.0</version>
        <dependencies platform="windows">
            <winapi path="./crates/my-winapi-fork">1.0.0</winapi>
        </dependencies>
        <dependencies>
            <miette dev="true">2.0.0</miette>
        </dependencies>
    </package>

    Could possibly be handled as:
    image
    (with the caveat that the duplicate <dependencies> tag should possibly decoded into an array instead of a record.

  13. added a commit that references this issue on Mar 11, 2023
  14. kronberger-droid commented on Nov 13, 2026

    @kronberger-droid
    Contributor

    The XML format got reworked in #7947 into {tag, attributes, content} records.
    Elements are selected by tag now, e.g. with where tag == bar or with xaccess [root junk bar] from std/xml.
    So the index no longer depends on the siblings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A:file-format

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions