Class: Saxon::XPath::Compiler
- Inherits:
-
Object
- Object
- Saxon::XPath::Compiler
- Extended by:
- Forwardable
- Defined in:
- lib/saxon/xpath/compiler.rb
Overview
An Compiler turns XPath expressions into executable queries you can run against XDM Nodes (like XML documents, or parts of XML documents).
To compile an XPath requires an XPath::Compiler
instance. You can create one by calling Compiler.create and passing a Processor, with an optional block for context like bound namespaces and declared variables. Alternately, and much more easily, you can call Processor#xpath_compiler on the Processor instance you’re already working with.
processor = Saxon::Processor.create
compiler = processor.xpath_compiler {
namespace a: 'http://example.org/a'
variable 'a:var', 'xs:string'
}
# Or...
compiler = Saxon::XPath::Compiler.create(processor) {
namespace a: 'http://example.org/a'
variable 'a:var', 'xs:string'
}
xpath = compiler.compile('//a:element[@attr = $a:var]')
matches = xpath.evaluate(document_node, {
'a:var' => 'the value'
}) #=> Saxon::XDM::Value
In order to use prefixed QNames in your XPaths, like /ns:name/
, then you need to declare prefix/namespace URI bindings when you create a compiler.
It’s also possible to make use of variables in your XPaths by declaring them at the compiler creation stage, and then passing in values for them as XPath run time.
Instance Attribute Summary collapse
-
#declared_namespaces ⇒ Hash<String => String>
readonly
Declared namespaces as prefix => URI hash.
-
#declared_variables ⇒ Hash<Saxon::QName => Saxon::XPath::VariableDeclaration>
readonly
Declared variables as QName => Declaration hash.
-
#default_collation ⇒ String
readonly
The URI of the default declared collation.
Class Method Summary collapse
-
.create(processor) { ... } ⇒ Saxon::XPath::Compiler
Create a new
XPath::Compiler
using the supplied Processor.
Instance Method Summary collapse
-
#compile(expression) ⇒ Saxon::XPath::Executable
The executable query.
-
#create { ... } ⇒ Saxon::XPath::Compiler
Allows the creation of a new Compiler starting from a copy of this Compiler’s static context.
-
#initialize(s9_processor, static_context) ⇒ Compiler
constructor
private
A new instance of Compiler.
Constructor Details
#initialize(s9_processor, static_context) ⇒ Compiler
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Compiler.
60 61 62 |
# File 'lib/saxon/xpath/compiler.rb', line 60 def initialize(s9_processor, static_context) @s9_processor, @static_context = s9_processor, static_context end |
Instance Attribute Details
#declared_namespaces ⇒ Hash<String => String> (readonly)
Returns declared namespaces as prefix => URI hash.
|
# File 'lib/saxon/xpath/compiler.rb', line 65
|
#declared_variables ⇒ Hash<Saxon::QName => Saxon::XPath::VariableDeclaration> (readonly)
Returns declared variables as QName => Declaration hash.
|
# File 'lib/saxon/xpath/compiler.rb', line 65
|
#default_collation ⇒ String (readonly)
Returns the URI of the default declared collation.
|
# File 'lib/saxon/xpath/compiler.rb', line 65
|
Class Method Details
.create(processor) { ... } ⇒ Saxon::XPath::Compiler
Create a new XPath::Compiler
using the supplied Processor. Passing a block gives access to a DSL for setting up the compiler’s static context.
45 46 47 48 |
# File 'lib/saxon/xpath/compiler.rb', line 45 def self.create(processor, &block) static_context = XPath::StaticContext.define(block) new(processor.to_java, static_context) end |
Instance Method Details
#compile(expression) ⇒ Saxon::XPath::Executable
Returns the executable query.
74 75 76 |
# File 'lib/saxon/xpath/compiler.rb', line 74 def compile(expression) Saxon::XPath::Executable.new(new_compiler.compile(expression), static_context) end |
#create { ... } ⇒ Saxon::XPath::Compiler
Allows the creation of a new Saxon::XPath::Compiler starting from a copy of this Compiler’s static context. As with create, passing a block gives access to a DSL for setting up the compiler’s static context.
84 85 86 87 |
# File 'lib/saxon/xpath/compiler.rb', line 84 def create(&block) new_static_context = static_context.define(block) self.class.new(@s9_processor, new_static_context) end |