Class: Saxon::XSLT::Stylesheet

Inherits:
Object
  • Object
show all
Defined in:
lib/saxon/xslt.rb

Overview

a Stylesheet transforms input (XML) into output

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Stylesheet

Returns a new instance of Stylesheet.

Parameters:



38
39
40
41
42
# File 'lib/saxon/xslt.rb', line 38

def initialize(source)
  processor = source.processor
  compiler = processor.to_java.new_xslt_compiler()
  @xslt = compiler.compile(source.to_java.as_source)
end

Class Method Details

.parse(processor, string_or_io, opts = {}) ⇒ Saxon::XSLT::Stylesheet

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 the compiled XSLT stylesheet.

Parameters:

  • processor (Saxon::Processor)

    the Saxon processor object

  • string_or_io (File, IO, String)

    the input XSLT

  • opts (Hash) (defaults to: {})

    Stylesheet and input options

Returns:



23
24
25
26
# File 'lib/saxon/xslt.rb', line 23

def self.parse(processor, string_or_io, opts = {})
  source = processor.XML(string_or_io, opts)
  new(source)
end

.parse_stylesheet_doc(document) ⇒ Saxon::XSLT::Stylesheet

Compile a stylesheet from an existing Saxon::XML instance of an XSLT source

Parameters:

Returns:



33
34
35
# File 'lib/saxon/xslt.rb', line 33

def self.parse_stylesheet_doc(document)
  new(document)
end

Instance Method Details

#apply_to(document, params = {}) ⇒ String

Transform an input document and return the result as a string.

See #transform for details of params handling

Parameters:

  • document (Saxon::XML::Document)

    the XML Document object to transform

  • params (Hash, Array) (defaults to: {})

    xsl params to set in the xsl document

Returns:

  • (String)

    the transformed XML Document serialised to a string



72
73
74
# File 'lib/saxon/xslt.rb', line 72

def apply_to(document, params = {})
  serialize(transform(document, params))
end

#serialize(document) ⇒ String

Serialise a document to a string

Not the most useful serialiser in the world. Provided for Nokogiri API compatibility

Parameters:

Returns:

  • (String)

    the XML Document serialised to a string



84
85
86
# File 'lib/saxon/xslt.rb', line 84

def serialize(document)
  document.to_s
end

#transform(document, params = {}) ⇒ Saxon::XML::Document

Transform an input document

To pass global parameters you can pass a hash with parameter names as keys and values as XPath expressions as values: to pass a string value, you need to pass it quoted: ‘“’string’”‘. An unquoted string is an XPath reference into the document being transformed.

Parameters:

  • document (Saxon::XML::Document)

    the XML Document object to transform

  • params (Hash, Array) (defaults to: {})

    xsl params to set in the xsl document

Returns:



55
56
57
58
59
60
61
62
63
# File 'lib/saxon/xslt.rb', line 55

def transform(document, params = {})
  output = S9API::XdmDestination.new
  transformer = @xslt.load
  transformer.setInitialContextNode(document.to_java)
  transformer.setDestination(output)
  set_params(transformer, document, params)
  transformer.transform
  Saxon::XML::Document.new(output.getXdmNode)
end