Class: Leptris::XML::XSLT::Stylesheet

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

Overview

A compiled XSLT 1.0 stylesheet (libleptris 1.9.1): the stylesheet parses ONCE into an immutable instruction forest — patterns to XPath ASTs, selects through the compiled-XPath path — then applies to any number of documents with no re-parsing.

style = Leptris::XML::XSLT.parse(<<~XSL)
  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/"><out><xsl:value-of select="count(//item)"/></out></xsl:template>
  </xsl:stylesheet>
XSL
style.apply_to(doc)      # => result Document (queryable tree)
style.serialize(doc)     # => String (fragments and top-level
                         #    text nodes preserved)

Defined Under Namespace

Classes: Handle

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ Stylesheet

Returns a new instance of Stylesheet.



62
63
64
# File 'lib/leptris/xml/xslt.rb', line 62

def initialize(handle)
  @handle = handle
end

Class Method Details

.parse(stylesheet_xml) ⇒ Object

Compile a stylesheet. Raises XPathError on a stylesheet syntax error (detail on the thread-global last error).



40
41
42
43
44
45
46
47
48
# File 'lib/leptris/xml/xslt.rb', line 40

def self.parse(stylesheet_xml)
  raw = Leptris::XML::FFI.leptris_xslt_parse(
    stylesheet_xml.to_s, stylesheet_xml.to_s.bytesize)
  if raw.null?
    raise Leptris::XML::XPathError,
      "stylesheet parse failed: #{Leptris::XML::FFI.leptris_last_error}"
  end
  new(Handle.new(raw))
end

.parse_file(path) ⇒ Object

Compile from a file. Resolves §2.7 embedded stylesheets (xml-stylesheet PI with an href="#id" fragment) when the file's root is not itself a stylesheet.



53
54
55
56
57
58
59
60
# File 'lib/leptris/xml/xslt.rb', line 53

def self.parse_file(path)
  raw = Leptris::XML::FFI.leptris_xslt_parse_file(path.to_s)
  if raw.null?
    raise Leptris::XML::XPathError,
      "stylesheet parse failed: #{Leptris::XML::FFI.leptris_last_error}"
  end
  new(Handle.new(raw))
end

Instance Method Details

#apply_to(document) ⇒ Object

Apply to document (not modified) and return the result tree as an owning Document — query it with xpath/css like any other.



68
69
70
71
72
73
74
75
# File 'lib/leptris/xml/xslt.rb', line 68

def apply_to(document)
  raw = Leptris::XML::FFI.leptris_xslt_apply(@handle, document.c_ptr)
  if raw.null?
    raise Leptris::XML::XPathError,
      "transform failed: #{document.last_error || Leptris::XML::FFI.leptris_last_error}"
  end
  Leptris::XML::Document.wrap(raw)
end

#serialize(document) ⇒ Object

Apply and serialize in one call — keeps top-level text nodes and result fragments that the tree API would flatten.



79
80
81
82
83
# File 'lib/leptris/xml/xslt.rb', line 79

def serialize(document)
  str_ptr = Leptris::XML::FFI.leptris_xslt_apply_string(
    @handle, document.c_ptr)
  Leptris::XML::FFI.read_owned_string(str_ptr)
end