Skip to content

xml_length() on empty nodeset #404

Description

@mgirlich

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

Activity

  1. hadley commented on Oct 30, 2023

    @hadley
    Member

    Agreed

  2. added
    bugan unexpected problem or unintended behavior
    on Oct 30, 2023
  3. discoleo commented on Nov 3, 2023

    @discoleo

    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?

  4. hadley commented on Nov 3, 2023

    @hadley
    Member

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions