Skip to content

Commit a449ffa

Browse files
committed
Allow avoiding de-duplication in xml_parent
1 parent 685bfff commit a449ffa

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

Diff for: NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# xml2 (development version)
22

3+
* `xml_parent()` now supports passing `deduplicate = TRUE` to avoid de-duplication of nodes in the returned nodeset.
4+
35
* `xml_find_all.xml_nodeset()` gains a `flatten` argument to control whether to return a single nodeset or a list of nodesets (#311, @jakejh)
46

57
* `write_xml()` and `write_html()` now return NULL invisibly, as they did prior to version 1.3.0 (#307)

Diff for: R/classes.R

+4-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ xml_nodeset <- function(nodes = list(), deduplicate = TRUE) {
8181
#' @param nodes A list (possible nested) of external pointers to nodes
8282
#' @return a nodeset
8383
#' @noRd
84-
make_nodeset <- function(nodes, doc) {
84+
make_nodeset <- function(nodes, doc, ...) {
8585
nodes <- unlist(nodes, recursive = FALSE)
8686

87-
xml_nodeset(lapply(nodes, xml_node, doc = doc))
87+
xml_nodeset(lapply(nodes, xml_node, doc = doc), ...)
8888
}
8989

9090
#' @export
@@ -147,7 +147,7 @@ nodeset_apply.xml_missing <- function(x, fun, ...) {
147147
}
148148

149149
#' @export
150-
nodeset_apply.xml_nodeset <- function(x, fun, ...) {
150+
nodeset_apply.xml_nodeset <- function(x, fun, ..., deduplicate = TRUE) {
151151
if (length(x) == 0)
152152
return(xml_nodeset())
153153

@@ -159,7 +159,7 @@ nodeset_apply.xml_nodeset <- function(x, fun, ...) {
159159
res[!is_missing] <- lapply(x[!is_missing], function(x) fun(x$node, ...))
160160
}
161161

162-
make_nodeset(res, x[[1]]$doc)
162+
make_nodeset(res, x[[1]]$doc, deduplicate = deduplicate)
163163
}
164164

165165
#' @export

Diff for: R/xml_children.R

+13-9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @param search For `xml_child`, either the child number to return (by
1414
#' position), or the name of the child node to return. If there are multiple
1515
#' child nodes with the same name, the first will be returned
16+
#' @param ... Additional options passed to internal functions.
1617
#' @return A node or nodeset (possibly empty). Results are always de-duplicated.
1718
#' @export
1819
#' @examples
@@ -24,6 +25,9 @@
2425
#' # Note the each unique node only appears once in the output
2526
#' xml_parent(xml_children(x))
2627
#'
28+
#' # But you avoid this deduplication if needed
29+
#' xml_parent(xml_children(x), deduplicate = FALSE)
30+
#'
2731
#' # Mixed content
2832
#' x <- read_xml("<foo> a <b/> c <d>e</d> f</foo>")
2933
#' # Childen gets the elements, contents gets all node types
@@ -37,8 +41,8 @@
3741
#' xml_child(x)
3842
#' xml_child(x, 2)
3943
#' xml_child(x, "baz")
40-
xml_children <- function(x) {
41-
nodeset_apply(x, function(x) .Call(node_children, x, TRUE))
44+
xml_children <- function(x, ...) {
45+
nodeset_apply(x, function(x) .Call(node_children, x, TRUE), ...)
4246
}
4347

4448
#' @export
@@ -65,8 +69,8 @@ xml_contents <- function(x) {
6569

6670
#' @export
6771
#' @rdname xml_children
68-
xml_parents <- function(x) {
69-
nodeset_apply(x, function(x) .Call(node_parents, x))
72+
xml_parents <- function(x, ...) {
73+
nodeset_apply(x, function(x) .Call(node_parents, x), ...)
7074
}
7175

7276
#' @export
@@ -77,23 +81,23 @@ xml_siblings <- function(x) {
7781

7882
#' @export
7983
#' @rdname xml_children
80-
xml_parent <- function(x) {
84+
xml_parent <- function(x, ...) {
8185
UseMethod("xml_parent")
8286
}
8387

8488
#' @export
85-
xml_parent.xml_missing <- function(x) {
89+
xml_parent.xml_missing <- function(x, ...) {
8690
xml_missing()
8791
}
8892

8993
#' @export
90-
xml_parent.xml_node <- function(x) {
94+
xml_parent.xml_node <- function(x, ...) {
9195
xml_node(.Call(node_parent, x$node), x$doc)
9296
}
9397

9498
#' @export
95-
xml_parent.xml_nodeset <- function(x) {
96-
nodeset_apply(x, function(x) .Call(node_parent, x))
99+
xml_parent.xml_nodeset <- function(x, ...) {
100+
nodeset_apply(x, function(x) .Call(node_parent, x), ...)
97101
}
98102

99103

Diff for: man/xml_children.Rd

+8-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)