r-lib / xml2

Bindings to libxml2
https://xml2.r-lib.org/
Other
220 stars 82 forks source link

Duplicated names for elements in list after calling `as_list` #436

Open pepijn-devries opened 11 months ago

pepijn-devries commented 11 months ago

Thanks for your work on xml2!

Consider the following reprex where I have an xml document with multiple nodes of the same element (item):

a <- xml2::as_xml_document("<collection><item>foo</item><item>bar</item></collection>")
b <- xml2::as_list(a)

## This will only show "foo" but not "bar"
b$collection$item
#> [[1]]
#> [1] "foo"

## However both elements are there:
names(b$collection)
#> [1] "item" "item"
b$collection[[1]]
#> [[1]]
#> [1] "foo"
b$collection[[2]]
#> [[1]]
#> [1] "bar"

Created on 2023-12-30 with reprex v2.0.2

When the xml document in the reprex is converted to a list, the list elements will have duplicated names. Which I think is undesirable in R, and will cause all sorts of problems. Would it be possible to either:

  1. Throw a warning when as_list produces duplicated element names; or
  2. Repair the names such that they become unique.

Thanks for considering.