r-lib / xml2

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

`xml_length()` on empty nodeset #404

Open mgirlich opened 1 year ago

mgirlich commented 1 year ago

xml_length(xml_nodeset()) returns 0 but I feel this should rather be integer() which would also be consistent with e.g. xml_name().

library(xml2)

(ns1 <- xml2:::xml_nodeset())
#> {xml_nodeset (0)}
xml_length(ns1)
#> [1] 0
(ns2 <- xml2:::xml_nodeset(list(xml_missing()), deduplicate = FALSE))
#> {xml_nodeset (1)}
#> [1] <NA>
xml_length(ns2)
#> [1] 0

Created on 2023-08-29 with reprex v2.0.2

hadley commented 1 year ago

Agreed

discoleo commented 1 year ago

My mistake!

I misinterpreted it as length(node_set).

Old

I do not fully understand this issue:

xml_length: should return the length of the node-set, which should be 0 if there are no nodes. The length is always a numeric value, which can be 0, or NA if its unknown.

It is xml_missing which is ill-defined: does it count as a node?

hadley commented 1 year ago

xml_length() returns a vector the same length as its input. It's the length of each element of the nodeset, not the length of the nodeset.