selectr is a package which makes working with HTML and XML documents
easier. It does this by performing translation of CSS selectors into
XPath expressions so that you can query XML
and
xml2
documents easily.
library(selectr)
<- css_to_xpath("#selectr")
xpath
xpath#> [1] "descendant-or-self::*[@id = 'selectr']"
install.packages("selectr")
# install.packages("devtools")
::install_github("sjp/selectr") devtools
The key functions in selectr are:
Translate a CSS selector into an XPath expression with
css_to_xpath()
.
Query an XML
or xml2
document with
querySelector()
and its variants.
Find the first matching node with
querySelector()
.
Find all matching nodes with
querySelectorAll()
.
Find the first matching node in a namespaced document with
querySelectorNS()
.
Find all matching nodes in a namespaced document with
querySelectorAllNS()
.
Here is a simple example to demonstrate how to query an
XML
or xml2
document with
querySelector()
.
library(selectr)
<- '<foo><bar><baz id="first"/></bar><baz id="second"/></foo>'
xmlText
library(XML)
<- xmlParse(xmlText)
doc querySelector(doc, "baz")
#> <baz id="first"/>
querySelectorAll(doc, "baz")
#> [[1]]
#> <baz id="first"/>
#>
#> [[2]]
#> <baz id="second"/>
#>
#> attr(,"class")
#> [1] "XMLNodeSet"
library(xml2)
<- read_xml(xmlText)
doc querySelector(doc, "baz")
#> {xml_node}
#> <baz id="first">
querySelectorAll(doc, "baz")
#> {xml_nodeset (2)}
#> [1] <baz id="first"/>
#> [2] <baz id="second"/>