File: C:/Ruby27-x64/share/doc/ruby/html/REXML/XPath.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class REXML::XPath - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "../";
var index_rel_prefix = "../";
</script>
<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="../index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="../table_of_contents.html#pages">Pages</a>
<a href="../table_of_contents.html#classes">Classes</a>
<a href="../table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="../Object.html">Object</a>
</div>
<div id="includes-section" class="nav-section">
<h3>Included Modules</h3>
<ul class="link-list">
<li><a class="include" href="Functions.html">REXML::Functions</a>
</ul>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-each">::each</a>
<li ><a href="#method-c-first">::first</a>
<li ><a href="#method-c-match">::match</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-REXML::XPath">
<h1 id="class-REXML::XPath" class="class">
class REXML::XPath
</h1>
<section class="description">
<p>Wrapper class. Use this class to access the <a href="XPath.html"><code>XPath</code></a> functions.</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section class="constants-list">
<header>
<h3>Constants</h3>
</header>
<dl>
<dt id="EMPTY_HASH">EMPTY_HASH
<dd><p>A base <a href="../Hash.html"><code>Hash</code></a> object, supposing to be used when initializing a default empty namespaces set, but is currently unused. TODO: either set the namespaces=EMPTY_HASH, or deprecate this.</p>
</dl>
</section>
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-each" class="method-detail ">
<div class="method-heading">
<span class="method-name">each</span><span
class="method-args">(element, path=nil, namespaces=nil, variables={}, options={}, &block)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Iterates over nodes that match the given path, calling the supplied block with the match.</p>
<dl class="rdoc-list note-list"><dt>element
<dd>
<p>The context element</p>
</dd><dt>path
<dd>
<p>The xpath to search for. If not supplied or nil, defaults to '*'</p>
</dd><dt>namespaces
<dd>
<p>If supplied, a <a href="../Hash.html"><code>Hash</code></a> which defines a namespace mapping</p>
</dd><dt>variables
<dd>
<p>If supplied, a <a href="../Hash.html"><code>Hash</code></a> which maps $variables in the query to values. This can be used to avoid <a href="XPath.html"><code>XPath</code></a> injection attacks or to automatically handle escaping string values.</p>
</dd></dl>
<pre>XPath.each( node ) { |el| ... }
XPath.each( node, '/*[@attr='v']' ) { |el| ... }
XPath.each( node, 'ancestor::x' ) { |el| ... }
XPath.each( node, '/book/publisher/text()=$publisher', {}, {"publisher"=>"O'Reilly"}) \
{|el| ... }</pre>
<div class="method-source-code" id="each-source">
<pre><span class="ruby-comment"># File lib/rexml/xpath.rb, line 60</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">XPath</span><span class="ruby-operator">::</span><span class="ruby-identifier ruby-title">each</span>(<span class="ruby-identifier">element</span>, <span class="ruby-identifier">path</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">namespaces</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">variables</span>={}, <span class="ruby-identifier">options</span>={}, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
<span class="ruby-identifier">raise</span> <span class="ruby-string">"The namespaces argument, if supplied, must be a hash object."</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">namespaces</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">namespaces</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Hash</span>)
<span class="ruby-identifier">raise</span> <span class="ruby-string">"The variables argument, if supplied, must be a hash object."</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">variables</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Hash</span>)
<span class="ruby-identifier">parser</span> = <span class="ruby-constant">XPathParser</span>.<span class="ruby-identifier">new</span>(<span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">namespaces</span> = <span class="ruby-identifier">namespaces</span>
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">variables</span> = <span class="ruby-identifier">variables</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">"*"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">path</span>
<span class="ruby-identifier">element</span> = [<span class="ruby-identifier">element</span>] <span class="ruby-keyword">unless</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Array</span>
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">element</span>).<span class="ruby-identifier">each</span>( <span class="ruby-operator">&</span><span class="ruby-identifier">block</span> )
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-c-first" class="method-detail ">
<div class="method-heading">
<span class="method-name">first</span><span
class="method-args">(element, path=nil, namespaces=nil, variables={}, options={})</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Finds and returns the first node that matches the supplied xpath.</p>
<dl class="rdoc-list note-list"><dt>element
<dd>
<p>The context element</p>
</dd><dt>path
<dd>
<p>The xpath to search for. If not supplied or nil, returns the first node matching '*'.</p>
</dd><dt>namespaces
<dd>
<p>If supplied, a <a href="../Hash.html"><code>Hash</code></a> which defines a namespace mapping.</p>
</dd><dt>variables
<dd>
<p>If supplied, a <a href="../Hash.html"><code>Hash</code></a> which maps $variables in the query to values. This can be used to avoid <a href="XPath.html"><code>XPath</code></a> injection attacks or to automatically handle escaping string values.</p>
</dd></dl>
<pre>XPath.first( node )
XPath.first( doc, "//b"} )
XPath.first( node, "a/x:b", { "x"=>"http://doofus" } )
XPath.first( node, '/book/publisher/text()=$publisher', {}, {"publisher"=>"O'Reilly"})</pre>
<div class="method-source-code" id="first-source">
<pre><span class="ruby-comment"># File lib/rexml/xpath.rb, line 31</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">XPath</span><span class="ruby-operator">::</span><span class="ruby-identifier ruby-title">first</span>(<span class="ruby-identifier">element</span>, <span class="ruby-identifier">path</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">namespaces</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">variables</span>={}, <span class="ruby-identifier">options</span>={})
<span class="ruby-identifier">raise</span> <span class="ruby-string">"The namespaces argument, if supplied, must be a hash object."</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">namespaces</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">namespaces</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Hash</span>)
<span class="ruby-identifier">raise</span> <span class="ruby-string">"The variables argument, if supplied, must be a hash object."</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">variables</span>.<span class="ruby-identifier">kind_of?</span>(<span class="ruby-constant">Hash</span>)
<span class="ruby-identifier">parser</span> = <span class="ruby-constant">XPathParser</span>.<span class="ruby-identifier">new</span>(<span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">namespaces</span> = <span class="ruby-identifier">namespaces</span>
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">variables</span> = <span class="ruby-identifier">variables</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">"*"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">path</span>
<span class="ruby-identifier">element</span> = [<span class="ruby-identifier">element</span>] <span class="ruby-keyword">unless</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Array</span>
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">element</span>).<span class="ruby-identifier">flatten</span>[<span class="ruby-value">0</span>]
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div id="method-c-match" class="method-detail ">
<div class="method-heading">
<span class="method-name">match</span><span
class="method-args">(element, path=nil, namespaces=nil, variables={}, options={})</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an array of nodes matching a given <a href="XPath.html"><code>XPath</code></a>.</p>
<div class="method-source-code" id="match-source">
<pre><span class="ruby-comment"># File lib/rexml/xpath.rb, line 72</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">XPath</span><span class="ruby-operator">::</span><span class="ruby-identifier ruby-title">match</span>(<span class="ruby-identifier">element</span>, <span class="ruby-identifier">path</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">namespaces</span>=<span class="ruby-keyword">nil</span>, <span class="ruby-identifier">variables</span>={}, <span class="ruby-identifier">options</span>={})
<span class="ruby-identifier">parser</span> = <span class="ruby-constant">XPathParser</span>.<span class="ruby-identifier">new</span>(<span class="ruby-operator">**</span><span class="ruby-identifier">options</span>)
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">namespaces</span> = <span class="ruby-identifier">namespaces</span>
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">variables</span> = <span class="ruby-identifier">variables</span>
<span class="ruby-identifier">path</span> = <span class="ruby-string">"*"</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">path</span>
<span class="ruby-identifier">element</span> = [<span class="ruby-identifier">element</span>] <span class="ruby-keyword">unless</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">Array</span>
<span class="ruby-identifier">parser</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">path</span>,<span class="ruby-identifier">element</span>)
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>