EVOLUTION-MANAGER
Edit File: querySelectorAll.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Find nodes that match a group of CSS selectors in an XML...</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for querySelectorAll {selectr}"><tr><td>querySelectorAll {selectr}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Find nodes that match a group of CSS selectors in an XML tree. </h2> <h3>Description</h3> <p>The purpose of these functions is to mimic the functionality of the <code>querySelector</code> and <code>querySelectorAll</code> functions present in Internet browsers. This is so we can succinctly query an XML tree for nodes matching a CSS selector. </p> <p>Namespaced functions <code>querySelectorNS</code> and <code>querySelectorAllNS</code> are also provided to search relative to a given namespace. </p> <h3>Usage</h3> <pre> querySelector(doc, selector, ns = NULL, ...) querySelectorAll(doc, selector, ns = NULL, ...) querySelectorNS(doc, selector, ns, prefix = "descendant-or-self::", ...) querySelectorAllNS(doc, selector, ns, prefix = "descendant-or-self::", ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>doc</code></td> <td> <p>The XML document or node to be evaluated against. </p> </td></tr> <tr valign="top"><td><code>selector</code></td> <td> <p>A selector used to query <code>doc</code>. This must be a single character string. </p> </td></tr> <tr valign="top"><td><code>ns</code></td> <td> <p>The namespace that the query will be filtered to. This is a named list or vector which has as its name a namespace, and its value is the namespace URI. This can be ignored for the un-namespaced functions. </p> </td></tr> <tr valign="top"><td><code>prefix</code></td> <td> <p>The prefix to apply to the resulting XPath expression. The default or <code>""</code> are most commonly used. </p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>Parameters to be passed onto <code>css_to_xpath</code>. </p> </td></tr> </table> <h3>Details</h3> <p>The <code>querySelectorNS</code> and <code>querySelectorAllNS</code> functions are convenience functions for working with namespaced documents. They filter out all content that does not belong within the given namespaces. Note that when searching for particular elements in a selector, they must have a namespace prefix, e.g. <code>"svg|g"</code>. </p> <p>The namespace argument, <code>ns</code>, is simply passed on to <code><a href="../../XML/html/getNodeSet.html">getNodeSet</a></code> or <code><a href="../../xml2/html/xml_find_all.html">xml_find_all</a></code> if it is necessary to use a namespace present within the document. This can be ignored for content lacking a namespace, which is usually the case when using <code>querySelector</code> or <code>querySelectorAll</code>. </p> <h3>Value</h3> <p>For <code>querySelector</code>, the result is a single node that represents the first matched node from a selector. If no matching nodes are found, <code>NULL</code> is returned. </p> <p>For <code>querySelectorAll</code>, the result is a list of XML nodes. This list may be empty in the case that no match is found. </p> <p>The <code>querySelectorNS</code> and <code>querySelectorAllNS</code> functions return the same type of content as their un-namespaced counterparts. </p> <h3>Author(s)</h3> <p>Simon Potter </p> <h3>References</h3> <p>CSS3 Selectors <a href="https://www.w3.org/TR/css3-selectors/">https://www.w3.org/TR/css3-selectors/</a>, XPath <a href="https://www.w3.org/TR/xpath/">https://www.w3.org/TR/xpath/</a>, querySelectorAll <a href="https://developer.mozilla.org/en-US/docs/DOM/Document.querySelectorAll">https://developer.mozilla.org/en-US/docs/DOM/Document.querySelectorAll</a> and <a href="http://www.w3.org/TR/selectors-api/#interface-definitions">http://www.w3.org/TR/selectors-api/#interface-definitions</a>. </p> <h3>Examples</h3> <pre> hasXML <- require(XML) hasxml2 <- require(xml2) if (!hasXML && !hasxml2) return() # can't demo without XML or xml2 packages present parseFn <- if (hasXML) xmlParse else read_xml # Demo for working with the XML package (if present, otherwise xml2) exdoc <- parseFn('<a><b class="aclass"/><c id="anid"/></a>') querySelector(exdoc, "#anid") # Returns the matching node querySelector(exdoc, ".aclass") # Returns the matching node querySelector(exdoc, "b, c") # First match from grouped selection querySelectorAll(exdoc, "b, c") # Grouped selection querySelectorAll(exdoc, "b") # A list of length one querySelector(exdoc, "d") # No match querySelectorAll(exdoc, "d") # No match # Read in a document where two namespaces are being set: # SVG and MathML svgdoc <- parseFn(system.file("demos/svg-mathml.svg", package = "selectr")) # Search for <script/> elements in the SVG namespace querySelectorNS(svgdoc, "svg|script", c(svg = "http://www.w3.org/2000/svg")) querySelectorAllNS(svgdoc, "svg|script", c(svg = "http://www.w3.org/2000/svg")) # MathML content is *within* SVG content, # search for <mtext> elements within the MathML namespace querySelectorNS(svgdoc, "math|mtext", c(math = "http://www.w3.org/1998/Math/MathML")) querySelectorAllNS(svgdoc, "math|mtext", c(math = "http://www.w3.org/1998/Math/MathML")) # Search for *both* SVG and MathML content querySelectorAllNS(svgdoc, "svg|script, math|mo", c(svg = "http://www.w3.org/2000/svg", math = "http://www.w3.org/1998/Math/MathML")) if (!hasXML) return() # already demo'd xml2 # Demo for working with the xml2 package exdoc <- read_xml('<a><b class="aclass"/><c id="anid"/></a>') querySelector(exdoc, "#anid") # Returns the matching node querySelector(exdoc, ".aclass") # Returns the matching node querySelector(exdoc, "b, c") # First match from grouped selection querySelectorAll(exdoc, "b, c") # Grouped selection querySelectorAll(exdoc, "b") # A list of length one querySelector(exdoc, "d") # No match querySelectorAll(exdoc, "d") # No match # Read in a document where two namespaces are being set: # SVG and MathML svgdoc <- read_xml(system.file("demos/svg-mathml.svg", package = "selectr")) # Search for <script/> elements in the SVG namespace querySelectorNS(svgdoc, "svg|script", c(svg = "http://www.w3.org/2000/svg")) querySelectorAllNS(svgdoc, "svg|script", c(svg = "http://www.w3.org/2000/svg")) # MathML content is *within* SVG content, # search for <mtext> elements within the MathML namespace querySelectorNS(svgdoc, "math|mtext", c(math = "http://www.w3.org/1998/Math/MathML")) querySelectorAllNS(svgdoc, "math|mtext", c(math = "http://www.w3.org/1998/Math/MathML")) # Search for *both* SVG and MathML content querySelectorAllNS(svgdoc, "svg|script, math|mo", c(svg = "http://www.w3.org/2000/svg", math = "http://www.w3.org/1998/Math/MathML")) </pre> <hr /><div style="text-align: center;">[Package <em>selectr</em> version 0.4-2 <a href="00Index.html">Index</a>]</div> </body></html>