Class: Saxon::XSLT::Stylesheet

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

Overview

a Stylesheet transforms input (XML) into output

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Stylesheet

Returns a new instance of Stylesheet.

Parameters:



42
43
44
45
46
# File 'lib/saxon/xslt.rb', line 42

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

Instance Attribute Details

#processorSaxon::Processor (readonly)

Return the processor used to create the source of this transformer

Returns:

  • (Saxon::Processor)

    return the processor used to create the source of this transformer



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/saxon/xslt.rb', line 19

class Stylesheet
  # @api private
  # @param processor [Saxon::Processor] the Saxon processor object
  # @param string_or_io [File, IO, String] the input XSLT
  # @param opts [Hash] Stylesheet and input options
  # @return [Saxon::XSLT::Stylesheet] the compiled XSLT stylesheet
  def self.parse(processor, string_or_io, opts = {})
    source = processor.XML(string_or_io, opts)
    new(source)
  end

  # Compile a stylesheet from an existing Saxon::XML instance of an XSLT
  # source
  #
  # @param document [Saxon::XML::Document] the input XSLT as an XML document
  # @return [Saxon::XSLT::Stylesheet] the compiled XSLT stylesheet
  def self.parse_stylesheet_doc(document)
    new(document)
  end

  attr_reader :processor

  # @param source [Saxon::XML::Document] the input XSLT as an XML document
  def initialize(source)
    @processor = source.processor
    compiler = processor.to_java.new_xslt_compiler()
    @xslt = compiler.compile(source.to_java.as_source)
  end

  # 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.
  #
  # @param document [Saxon::XML::Document] the XML Document object to
  #   transform
  # @param params [Hash,Array] xsl params to set in the xsl document
  # @return [Saxon::XML::Document] the transformed XML Document
  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, processor)
  end

  # Transform an input document and return the result as a string.
  #
  # See #transform for details of params handling
  # @param [Saxon::XML::Document] document the XML Document object to
  #   transform
  # @param params [Hash,Array] xsl params to set in the xsl document
  # @return [String] the transformed XML Document serialised to a string
  def apply_to(document, params = {})
    serialize(transform(document, params))
  end

  # Serialise a document to a string
  #
  # Not the most useful serialiser in the world. Provided for Nokogiri API
  # compatibility
  #
  # @param document [Saxon::XML::Document] the XML Document object to
  #   serialise
  # @return [String] the XML Document serialised to a string
  def serialize(document)
    document.to_s
  end

  private

  def resolve_q_name(q_name_or_string)
    case q_name_or_string
    when S9API::QName
      q_name_or_string
    else
      S9API::QName.new(q_name_or_string.to_s)
    end
  end

  def params_as_kv_pairs(params)
    case params
    when Hash
      params
    when Array
      params.each_slice(2).map { |k,v|
        raise ArgumentError.new("Odd number of values passed as params: #{params}") if v.nil?
        [k, v]
      }
    end
  end

  def set_params(transformer, document, params)
    params_as_kv_pairs(params).each do |k,v|
      transformer.setParameter(resolve_q_name(k), document.xpath(v))
    end
  end
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:



25
26
27
28
# File 'lib/saxon/xslt.rb', line 25

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:



35
36
37
# File 'lib/saxon/xslt.rb', line 35

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



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

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



88
89
90
# File 'lib/saxon/xslt.rb', line 88

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:



59
60
61
62
63
64
65
66
67
# File 'lib/saxon/xslt.rb', line 59

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, processor)
end