Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is NaN the right value for xml_find_num() of an absent XPath? #435

Open
MichaelChirico opened this issue Dec 19, 2023 · 0 comments
Open
Labels
bug an unexpected problem or unintended behavior xpath 🚶‍♂️

Comments

@MichaelChirico
Copy link
Contributor

Encountered when trying to convert some as.integer(xml_find_num()) usage. Consider:

x1 <- xml2::read_xml('<NODE line1="1">x</NODE>')
x2 <- xml2::read_xml('<NON-NODE line1="1">x</NON-NODE>')

xml2::xml_find_num(x1, "number(//NODE/@line1)")
# [1] 1
xml2::xml_find_num(x2, "number(//NODE/@line1)")
# [1] NaN
xml2::xml_find_int(x2, "number(//NODE/@line1)")
# Error in `xml2::xml_find_int()`:
# ! Element at path `number(//NODE/@line1)` must be a whole number, not `NaN`.
# Run `rlang::last_trace()` to see where the error occurred.
xml2::xml_find_first(x2, "//NODE/@line1")
# {xml_missing}
# <NA>

I would have expected NA to show up from xml_find_num() if xml_find_first() is returning xml_missing.

Either way, the inconsistency of xml_find_int() should be fixed. Since as.integer(NaN) is NA_integer_, shall we return that directly instead of looking at check_number_whole()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior xpath 🚶‍♂️
Projects
None yet
Development

No branches or pull requests

2 participants