Class: Saxon::XSLT::Compiler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/saxon/xslt/compiler.rb

Overview

The simplest way to construct an Compiler is to call Processor#xslt_compiler.

processor = Saxon::Processor.create
# Simplest, default options
compiler = processor.xslt_compiler

In order to set compile-time options, declare static compile-time parameters then pass a block to the method using the DSL syntax (see EvaluationContext::DSL

compiler = processor.xslt_compiler {
  static_parameters 'param' => 'value'
  default_collation 'https://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive/'
}

The static evaluation context for a Compiler cannot be changed, you must create a new one with the context you want. It’s very simple to create a new Compiler based on an existing one. Declaring a parameter with a an existing name overwrites the old value.

new_compiler = compiler.create {
  static_parameters 'param' => 'new value'
}
new_compiler.default_collation #=> "https://www.w3.org/2005/xpath-functions/collation/html-ascii-case-insensitive/"

If you wanted to remove a value, you need to start from scratch. You can, of course, extract any data you want from a compiler instance separately and use that to create a new one.

params = compiler.static_parameters
new_compiler = processor.xslt_compiler {
  static_parameters params
}
new_compiler.default_collation #=> nil

Once you have a compiler, call #compile and pass in a Source or an existing Saxon::XDM::Node. Parameters and other run-time configuration options can be set using a block in the same way as creating a compiler. You’ll be returned a Executable.

source = Saxon::Source.create('my.xsl')
xslt = compiler.compile(source) {
  initial_template_parameters 'param' => 'other value'
}

You can also pass in (or override) parameters at stylesheet execution time, but if you’ll be executing the same stylesheet against many documents with the same initial parameters then setting them at compile time is simpler.

Global and initial template parameters can be set at compiler creation time, compile time, or execution time. Static parameters can only be set at compiler creation or compile time.

xslt = compiler.compile(source) {
  static_parameters 'static-param' => 'static value'
  global_parameters 'param' => 'global value'
  initial_template_parameters 'param' => 'other value'
  initial_template_tunnel_parameters 'param' => 'tunnel value'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s9_processor, evaluation_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.

Parameters:

  • s9_processor (net.sf.saxon.s9api.Processor)

    the Saxon Processor to wrap

  • evaluation_context (Saxon::XSLT::EvaluationContext)

    the static context XPaths compiled using this compiler will have



91
92
93
# File 'lib/saxon/xslt/compiler.rb', line 91

def initialize(s9_processor, evaluation_context)
  @s9_processor, @evaluation_context = s9_processor, evaluation_context
end

Instance Attribute Details

#declared_collationsHash<String => java.text.Collator> (readonly)

Returns declared collations as URI => Collator hash.

Returns:

  • (Hash<String => java.text.Collator>)

    declared collations as URI => Collator hash



# File 'lib/saxon/xslt/compiler.rb', line 96

#default_collationString (readonly)

Returns the URI of the default declared collation.

Returns:

  • (String)

    the URI of the default declared collation



# File 'lib/saxon/xslt/compiler.rb', line 96

#static_parametersHash<Saxon::QName => Saxon::XDM::Value, Saxon::XDM::Node, (readonly)

Saxon::XDM::AtomicValue>] parameters required at compile time as QName => value hash

Returns:



# File 'lib/saxon/xslt/compiler.rb', line 96

Class Method Details

.create(processor) { ... } ⇒ Saxon::XSLT::Compiler

Create a new XSLT::Compiler using the supplied Processor. Passing a block gives access to a DSL for setting up the compiler’s static context.

Parameters:

Yields:

  • An XSLT compiler DSL block

Returns:



76
77
78
79
# File 'lib/saxon/xslt/compiler.rb', line 76

def self.create(processor, &block)
  evaluation_context = XSLT::EvaluationContext.define(block)
  new(processor.to_java, evaluation_context)
end

Instance Method Details

#compile(source) { ... } ⇒ Saxon::XSLT::Executable

Returns the executable stylesheet.

Parameters:

Yields:

Returns:



107
108
109
110
111
112
113
114
# File 'lib/saxon/xslt/compiler.rb', line 107

def compile(source, &block)
  new_evaluation_context = evaluation_context.define(block)
  s9_compiler = new_compiler(new_evaluation_context)
  Saxon::XSLT::Executable.new(
    s9_compiler.compile(source.to_java),
    new_evaluation_context
  )
end

#create { ... } ⇒ Saxon::XSLT::Compiler

Allows the creation of a new Saxon::XSLT::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.

Yields:

  • An XSLT compiler DSL block

Returns:



122
123
124
125
# File 'lib/saxon/xslt/compiler.rb', line 122

def create(&block)
  new_evaluation_context = evaluation_context.define(block)
  self.class.new(@s9_processor, new_evaluation_context)
end