Move from nodes to related nodes. Operations that return exactly one
node per input node – html_parent(), html_next_sibling() and
html_previous_sibling() – are aligned: the result has the same
length as x, with a missing node where there is no answer, so that
results line up with their inputs. Operations that can return many nodes
per input – html_children(), html_ancestors() and
html_template_content() – return the set of all results, without
duplicates, in document order.
Usage
html_children(x, elements_only = TRUE)
html_parent(x)
html_ancestors(x)
html_next_sibling(x, elements_only = TRUE)
html_previous_sibling(x, elements_only = TRUE)
html_template_content(x)
html_root(x)
html_document(x)Value
html_children(),html_ancestors(),html_template_content(): azuhtml_nodesetin document order.html_ancestors()does not include the document node.html_parent(),html_next_sibling(),html_previous_sibling(): azuhtml_nodesetas long asx, with missing nodes where there is no parent or sibling.html_root(): azuhtml_nodesetof length one: the<html>element of a document, or the fragment node of a fragment.html_document(): thezuhtml_documentthat ownsx.
Details
A <template> element's contents are its children here, but they are
inert: searches and text extraction do not descend into them.
html_template_content() returns them explicitly.
Nodesets
A zuhtml_nodeset holds node IDs that only mean something in the
document that owns them. Subsetting ([, [[), rev(), rep(),
unique() and length<- keep the owner; length<- pads with missing
nodes. c(), [<- and [[<- accept only nodes of the same document
(and, for assignment, NA for a missing node), and raise a
zuhtml_input_error otherwise.
== and != between two nodesets of the same document compare node
identity, element by element: two <p>a</p> elements are different
nodes. Comparing nodes of different documents, or a nodeset with
anything else, is a zuhtml_input_error. match(), %in%,
setdiff() and the other set operations compare nodes too, so a node is
never found among another document's nodes. sort(), order() and
xtfrm() follow document order.
Every other operator (arithmetic, < and the other orderings, !,
&), the math functions, sum(), max(), range() and the rest of
the Summary group, mean(), median() and diff() are refused with a
zuhtml_input_error, and so is rank(), which needs >. Use
as.integer() to work with the IDs as numbers.
See also
Other navigation:
html_closest(),
html_elements()
Examples
doc <- html_parse("<ul><li>One</li><li>Two <b>bold</b></li></ul>")
body <- html_children(html_root(doc))[2]
items <- html_children(html_children(body))
items
#> <zuhtml_nodeset[2]>
#> [1] <li>
#> [2] <li>
html_parent(items)
#> <zuhtml_nodeset[2]>
#> [1] <ul>
#> [2] <ul>
html_next_sibling(items)
#> <zuhtml_nodeset[2]>
#> [1] <li>
#> [2] <missing>
html_ancestors(html_children(items[2]))
#> <zuhtml_nodeset[4]>
#> [1] <html>
#> [2] <body>
#> [3] <ul>
#> [4] <li>
# Template contents are reached explicitly:
tmpl <- html_parse("<template><p>inert</p></template>")
html_template_content(html_children(html_children(html_root(tmpl))[1]))
#> <zuhtml_nodeset[1]>
#> [1] <p>