xml2 icon indicating copy to clipboard operation
xml2 copied to clipboard

Duplicated names for elements in list after calling `as_list`

Open pepijn-devries opened this issue 6 months ago • 0 comments

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.

pepijn-devries avatar Dec 30 '23 23:12 pepijn-devries