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

New xml_find_int() to get integers from an XPath #412

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ S3method(xml_find_chr,xml_nodeset)
S3method(xml_find_first,xml_missing)
S3method(xml_find_first,xml_node)
S3method(xml_find_first,xml_nodeset)
S3method(xml_find_int,xml_missing)
S3method(xml_find_int,xml_node)
S3method(xml_find_int,xml_nodeset)
S3method(xml_find_lgl,xml_missing)
S3method(xml_find_lgl,xml_node)
S3method(xml_find_lgl,xml_nodeset)
Expand Down Expand Up @@ -149,6 +152,7 @@ export(xml_dtd)
export(xml_find_all)
export(xml_find_chr)
export(xml_find_first)
export(xml_find_int)
export(xml_find_lgl)
export(xml_find_num)
export(xml_find_one)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# xml2 (development version)

* `xml_find_int()` analogous to `xml_find_num()` for returning integers
matched by an XPath (#365, @michaelchirico).

* Now compatible with limxml2 2.12.0 and later (@KNnut).

* Fix format string issues detected in R-devel.
Expand Down
27 changes: 27 additions & 0 deletions R/xml_find.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,33 @@ xml_find_num.xml_missing <- function(x, xpath, ns = xml_ns(x)) {
numeric(0)
}

#' @export
#' @rdname xml_find_all
xml_find_int <- function(x, xpath, ns = xml_ns(x)) {
UseMethod("xml_find_int")
}

#' @export
xml_find_int.xml_node <- function(x, xpath, ns = xml_ns(x)) {
res <- .Call(xpath_search, x$node, x$doc, xpath, ns, Inf)
check_number_whole(res, arg = I(paste0("Element at path `", xpath, "`")))
as.integer(res)
}

#' @export
xml_find_int.xml_nodeset <- function(x, xpath, ns = xml_ns(x)) {
if (length(x) == 0) {
return(integer())
}

vapply(x, function(x) xml_find_int(x, xpath = xpath, ns = ns), integer(1))
}

#' @export
xml_find_int.xml_missing <- function(x, xpath, ns = xml_ns(x)) {
integer(0)
}

#' @export
#' @rdname xml_find_all
xml_find_chr <- function(x, xpath, ns = xml_ns(x)) {
Expand Down
3 changes: 3 additions & 0 deletions man/xml_find_all.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/testthat/_snaps/xml_find.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

Element at path `//z` must be a number, not a <xml_missing> object.

# xml_find_int errors with non integer results
hadley marked this conversation as resolved.
Show resolved Hide resolved

Code
xml_find_int(x, "//z")
Condition
Error in `xml_find_int()`:
! Element at path `//z` must be a whole number, not a <xml_missing> object.
Code
xml_find_int(x, "//y")
Condition
Error in `xml_find_int()`:
! Element at path `//y` must be a whole number, not a list.
Code
xml_find_int(x, "number(1.1)")
Condition
Error in `xml_find_int()`:
! Element at path `number(1.1)` must be a whole number, not the number 1.1.

# xml_find_chr errors with non character results

Element at path `//z` must be a single string, not a <xml_missing> object.
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-xml_find.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ test_that("xml_find_num returns a numeric result", {
expect_equal(xml_find_num(x, "1 div floor(-0)"), -Inf)
})

# Find int ---------------------------------------------------------------------
test_that("xml_find_int errors with non integer results", {
x <- read_xml("<x><y/><y/></x>")
expect_snapshot(error = TRUE, {
xml_find_int(x, "//z")
xml_find_int(x, "//y")
xml_find_int(x, "number(1.1)")
})
})

test_that("xml_find_int returns a integer result", {
x <- read_xml("<x><y>1</y><y/></x>")

expect_identical(xml_find_int(x, "1 div floor(-0.1)"), -1L)
expect_identical(xml_find_int(x, "number(//y)"), 1L)
expect_identical(xml_find_int(x, "string-length(string('abcd'))"), 4L)
})

# Find chr ---------------------------------------------------------------------
test_that("xml_find_chr errors with non character results", {
x <- read_xml("<x><y/><y/></x>")
Expand Down
Loading