Module: Nokogiri::XSLT
- Defined in:
- lib/nokogiri/xslt.rb,
lib/nokogiri/xslt/stylesheet.rb,
ext/nokogiri/nokogiri.c
Overview
See Nokogiri::XSLT::Stylesheet for creating and manipulating Stylesheet object.
Defined Under Namespace
Classes: Stylesheet
Class Method Summary collapse
-
.parse(string, modules = {}) ⇒ Object
:call-seq: parse(xsl) → Nokogiri::XSLT::Stylesheet parse(xsl, modules) → Nokogiri::XSLT::Stylesheet.
-
.quote_params(params) ⇒ Object
:call-seq: quote_params(params) → Array.
-
.register(uri, obj) ⇒ Object
docstring is in lib/nokogiri/xslt.rb.
Instance Method Summary collapse
-
#register(uri, custom_handler_class) ⇒ Object
call-seq: register(uri, custom_handler_class).
Class Method Details
.parse(string, modules = {}) ⇒ Object
:call-seq:
parse(xsl)
Parse the stylesheet in xsl, registering optional modules as custom class handlers.
- Parameters
-
xsl(String) XSL content to be parsed into a stylesheet -
modules(Hash<String ⇒ Class>) A hash of URI-to-handler relations for linking a namespace to a custom function handler.
⚠ The XSLT handler classes are registered globally.
Also see Nokogiri::XSLT.register
Example
xml = Nokogiri.XML("<nodes>\n <node>Foo</node>\n <node>Bar</node>\n</nodes>\n")
handler = Class.new do
def reverse(node)
node.text.reverse
end
end
xsl = "<xsl:stylesheet version=\"1.0\"\n xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n xmlns:myfuncs=\"http://nokogiri.org/xslt/myfuncs\"\n extension-element-prefixes=\"myfuncs\">\n <xsl:template match=\"/\">\n <reversed>\n <xsl:for-each select=\"nodes/node\">\n <reverse><xsl:copy-of select=\"myfuncs:reverse(.)\"/></reverse>\n </xsl:for-each>\n </reversed>\n </xsl:template>\n</xsl:stylesheet>\n"
xsl = Nokogiri.XSLT(xsl, "http://nokogiri.org/xslt/myfuncs" => handler)
xsl.transform(xml).to_xml
# => "<?xml version=\"1.0\"?>\n" +
# "<reversed>\n" +
# " <reverse>ooF</reverse>\n" +
# " <reverse>raB</reverse>\n" +
# "</reversed>\n"
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/nokogiri/xslt.rb', line 70 def parse(string, modules = {}) modules.each do |url, klass| XSLT.register(url, klass) end doc = XML::Document.parse(string, nil, nil, XML::ParseOptions::DEFAULT_XSLT) if Nokogiri.jruby? Stylesheet.parse_stylesheet_doc(doc, string) else Stylesheet.parse_stylesheet_doc(doc) end end |
.quote_params(params) ⇒ Object
:call-seq:
quote_params(params)
Quote parameters in params for stylesheet safety. See Nokogiri::XSLT::Stylesheet.transform for example usage.
- Parameters
-
params(Hash, Array) XSLT parameters (key->value, or tuples of [key, value])
- Returns
-
Array of string parameters, with quotes correctly escaped for use with XSLT::Stylesheet.transform
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/nokogiri/xslt.rb', line 94 def quote_params(params) params.flatten.each_slice(2).with_object([]) do |kv, quoted_params| key, value = kv.map(&:to_s) value = if value.include?("'") "concat('#{value.gsub("'", %q{', "'", '})}')" else "'#{value}'" end quoted_params << key quoted_params << value end end |
.register(uri, obj) ⇒ Object
docstring is in lib/nokogiri/xslt.rb
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'ext/nokogiri/xslt_stylesheet.c', line 391 static VALUE rb_xslt_s_register(VALUE self, VALUE uri, VALUE obj) { VALUE modules = rb_iv_get(self, "@modules"); if (NIL_P(modules)) { rb_raise(rb_eRuntimeError, "internal error: @modules not set"); } rb_hash_aset(modules, uri, obj); xsltRegisterExtModule( (unsigned char *)StringValueCStr(uri), initFunc, shutdownFunc ); return self; } |
Instance Method Details
#register(uri, custom_handler_class) ⇒ Object
call-seq:
register(uri, custom_handler_class)
Register a class that implements custom XSLT transformation functions.
121 122 123 124 |
# File 'lib/nokogiri/xslt.rb', line 121 def register(uri, custom_handler_class) # NOTE: this is implemented in the C extension, see ext/nokogiri/xslt_stylesheet.c raise NotImplementedError, "Nokogiri::XSLT.register is not implemented on JRuby" end |