Skip to content

Commit

Permalink
Handle xml_root, xml_ns for xml_nodeset objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jimhester committed May 20, 2016
1 parent fc049f2 commit ff324fc
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
9 changes: 8 additions & 1 deletion R/xml_children.R
Expand Up @@ -99,7 +99,14 @@ xml_length.xml_nodeset <- function(x, only_elements = TRUE) {
#' @export
#' @rdname xml_children
xml_root <- function(x) {
stopifnot(inherits(x, "xml_node") || inherits(x, "xml_document"))
stopifnot(inherits(x, c("xml_node", "xml_document", "xml_nodeset")))

if (inherits(x, "xml_nodeset")) {
if (length(x) == 0) {
return(NULL)
} else {
return(xml_root(x[[1]]))
}
}
xml_document(x$doc)
}
4 changes: 4 additions & 0 deletions R/xml_namespaces.R
Expand Up @@ -37,6 +37,10 @@
#' str(as_list(x))
#' str(as_list(x, ns))
xml_ns <- function(x) {
if (length(x) == 0) {
return(character())
}

stopifnot(inherits(x, "xml_document"))
doc <- x$doc
x <- doc_namespaces(doc)
Expand Down
6 changes: 1 addition & 5 deletions R/xml_text.R
Expand Up @@ -41,10 +41,6 @@ xml_text.xml_nodeset <- function(x, trim = FALSE) {
UseMethod("xml_text<-")
}

# TODO: Should this only be called on TEXT nodes? If it is used on non-text nodes it will remove child nodes from the tree.
#
# https://github.com/GNOME/libxml2/blob/e28939036281969477d3913a51c001bb7635fe54/doc/examples/xpath2.c#L163-L179

#' @export
`xml_text<-.xml_nodeset` <- function(x, value) {
# We need to do the modification in reverse order as the modification can
Expand All @@ -59,7 +55,7 @@ xml_text.xml_nodeset <- function(x, trim = FALSE) {
#' @export
`xml_text<-.xml_node` <- function(x, value) {
if (xml_type(x) != "text") {
text_child <- xml_find_one(x, ".//text()[1]")
text_child <- xml_find_one(x, ".//text()[1]", ns = character())
if (inherits(text_child, "xml_missing")) {
node_append_content(x$node, value)
} else {
Expand Down

0 comments on commit ff324fc

Please sign in to comment.