Class: Nokogiri::XSLT::Stylesheet

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xslt/stylesheet.rb,
lib/nokogiri/ffi/xslt/stylesheet.rb,
ext/nokogiri/xslt_stylesheet.c

Overview

A Stylesheet represents an XSLT Stylesheet object. Stylesheet creation is done through Nokogiri.XSLT. Here is an example of transforming an XML::Document with a Stylesheet:

doc   = Nokogiri::XML(File.read('some_file.xml'))
xslt  = Nokogir::XSLT(File.read('some_transformer.xslt'))

puts xslt.transform(doc)

See Nokogiri::XSLT::Stylesheet#transform for more transformation information.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cstructObject

:nodoc:



5
6
7
# File 'lib/nokogiri/ffi/xslt/stylesheet.rb', line 5

def cstruct
  @cstruct
end

Class Method Details

.parse_stylesheet_doc(document) ⇒ Object

Parse a stylesheet from document.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nokogiri/ffi/xslt/stylesheet.rb', line 7

def self.parse_stylesheet_doc(document) # :nodoc:
  LibXML.exsltRegisterAll

  generic_exception_handler = lambda do |ctx, msg|
    raise RuntimeError.new(msg) # TODO: varargs
  end
  LibXML.xsltSetGenericErrorFunc(nil, generic_exception_handler)

  ss = LibXML.xsltParseStylesheetDoc(LibXML.xmlCopyDoc(document.cstruct, 1)) # 1 => recursive

  LibXML.xsltSetGenericErrorFunc(nil, nil)

  obj = allocate
  obj.cstruct = LibXML::XsltStylesheet.new(ss)
  obj
end

Instance Method Details

#apply_to(document, params = []) ⇒ Object

Apply an XSLT stylesheet to an XML::Document. params is an array of strings used as XSLT parameters. returns serialized document



20
21
22
# File 'lib/nokogiri/xslt/stylesheet.rb', line 20

def apply_to document, params = []
  serialize(transform(document, params))
end

#serialize(document) ⇒ Object

Serialize document to an xml string.



24
25
26
27
28
29
30
# File 'lib/nokogiri/ffi/xslt/stylesheet.rb', line 24

def serialize(document) # :nodoc:
  buf_ptr = FFI::Buffer.new :pointer
  buf_len = FFI::Buffer.new :int
  LibXML.xsltSaveResultToString(buf_ptr, buf_len, document.cstruct, cstruct)
  buf = Nokogiri::LibXML::XmlAlloc.new(buf_ptr.get_pointer(0))
  buf.pointer.read_string(buf_len.get_int(0))
end

#transform(document, params = []) ⇒ Object

Apply an XSLT stylesheet to an XML::Document. params is an array of strings used as XSLT parameters. returns Nokogiri::XML::Document

Example:

doc   = Nokogiri::XML(File.read(ARGV[0]))
xslt  = Nokogiri::XSLT(File.read(ARGV[1]))
puts xslt.transform(doc, ['key', 'value'])


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nokogiri/ffi/xslt/stylesheet.rb', line 32

def transform(document, params=[]) # :nodoc:
  params = params.to_a.flatten if params.is_a?(Hash)
  raise(TypeError) unless params.is_a?(Array)

  param_arr = FFI::MemoryPointer.new(:pointer, params.length + 1, false)

  # Keep the MemoryPointer instances alive until after the call
  ptrs = params.map { |param | FFI::MemoryPointer.from_string(param.to_s) }
  param_arr.put_array_of_pointer(0, ptrs)
  
  # Terminate the list with a NULL pointer
  param_arr.put_pointer(LibXML.pointer_offset(params.length), nil)

  ptr = LibXML.xsltApplyStylesheet(cstruct, document.cstruct, param_arr)
  raise(RuntimeError, "could not perform xslt transform on document") if ptr.null?

  XML::Document.wrap(ptr)
end