Skip to content

sjp/selectr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5f6e50b · May 9, 2024

History

96 Commits
Nov 17, 2023
May 9, 2024
Mar 22, 2022
Nov 20, 2019
Aug 17, 2016
Sep 3, 2020
Sep 3, 2020
Jul 18, 2013
Apr 29, 2020
Nov 20, 2019
Mar 22, 2022
Jul 18, 2013
Mar 17, 2018
Nov 24, 2023

Repository files navigation

selectr

License (3-Clause BSD) GitHub Actions CRAN version codecov Downloads per month

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)
xpath <- css_to_xpath("#selectr")
xpath
#> [1] "descendant-or-self::*[@id = 'selectr']"

Installation

Install the release version from CRAN

install.packages("selectr")

Install the development version from GitHub

# install.packages("devtools")
devtools::install_github("sjp/selectr")

Overview

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().

Examples

Here is a simple example to demonstrate how to query an XML or xml2 document with querySelector().

library(selectr)
xmlText <- '<foo><bar><baz id="first"/></bar><baz id="second"/></foo>'

library(XML)
doc <- xmlParse(xmlText)
querySelector(doc, "baz")
#> <baz id="first"/>
querySelectorAll(doc, "baz")
#> [[1]]
#> <baz id="first"/>
#>
#> [[2]]
#> <baz id="second"/>
#>
#> attr(,"class")
#> [1] "XMLNodeSet"

library(xml2)
doc <- read_xml(xmlText)
querySelector(doc, "baz")
#> {xml_node}
#> <baz id="first">
querySelectorAll(doc, "baz")
#> {xml_nodeset (2)}
#> [1] <baz id="first"/>
#> [2] <baz id="second"/>

About

Translate CSS Selectors to XPath Expressions

Resources

License

Citation

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published