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 = []
  transform(document, params).to_s
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::MemoryPointer.new :pointer
  buf_len = FFI::MemoryPointer.new :int
  LibXML.xsltSaveResultToString(buf_ptr, buf_len, document.cstruct, cstruct)
  buf = Nokogiri::LibXML::XmlAlloc.new(buf_ptr.read_pointer)
  buf.pointer.read_string(buf_len.read_int)
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
# File 'lib/nokogiri/ffi/xslt/stylesheet.rb', line 32

def transform(document, params=[]) # :nodoc:
  param_arr = FFI::MemoryPointer.new(:pointer, params.length + 1)
  params.each_with_index do |param, j|
    param_arr[j].put_pointer(0, FFI::MemoryPointer.from_string(param.to_s))
  end
  param_arr[params.length].put_pointer(0,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