### Home Classes Methods

### In Files

- lib/nokogiri/xslt/stylesheet.rb
- ext/nokogiri/xslt_stylesheet.c

### Parent

Object

### Methods

- ::parse_stylesheet_doc
- #apply_to
- #serialize
- #transform

### Files

- CHANGELOG.ja.rdoc
- CHANGELOG.rdoc
- C_CODING_STYLE.rdoc
- Manifest.txt
- README.ja.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Nokogiri
- Nokogiri::CSS
- Nokogiri::CSS::Node
- Nokogiri::CSS::Parser
- Nokogiri::CSS::SyntaxError
- Nokogiri::CSS::Tokenizer
- Nokogiri::CSS::Tokenizer::ScanError
- Nokogiri::Decorators
- Nokogiri::Decorators::Slop
- Nokogiri::EncodingHandler
- Nokogiri::HTML
- Nokogiri::HTML::Builder
- Nokogiri::HTML::Document
- Nokogiri::HTML::Document::EncodingReader
- Nokogiri::HTML::Document::EncodingReader::JumpSAXHandler
- Nokogiri::HTML::DocumentFragment
- Nokogiri::HTML::ElementDescription
- Nokogiri::HTML::EntityDescription
- Nokogiri::HTML::EntityLookup
- Nokogiri::HTML::SAX
- Nokogiri::HTML::SAX::Parser
- Nokogiri::HTML::SAX::ParserContext
- Nokogiri::HTML::SAX::PushParser
- Nokogiri::SyntaxError
- Nokogiri::XML
- Nokogiri::XML::Attr
- Nokogiri::XML::AttributeDecl
- Nokogiri::XML::Builder
- Nokogiri::XML::CDATA
- Nokogiri::XML::CharacterData
- Nokogiri::XML::Comment
- Nokogiri::XML::DTD
- Nokogiri::XML::Document
- Nokogiri::XML::DocumentFragment
- Nokogiri::XML::Element
- Nokogiri::XML::ElementContent
- Nokogiri::XML::ElementDecl
- Nokogiri::XML::EntityDecl
- Nokogiri::XML::EntityReference
- Nokogiri::XML::Namespace
- Nokogiri::XML::Node
- Nokogiri::XML::Node::SaveOptions
- Nokogiri::XML::NodeSet
- Nokogiri::XML::Notation
- Nokogiri::XML::PP
- Nokogiri::XML::PP::CharacterData
- Nokogiri::XML::PP::Node
- Nokogiri::XML::ParseOptions
- Nokogiri::XML::ProcessingInstruction
- Nokogiri::XML::Reader
- Nokogiri::XML::RelaxNG
- Nokogiri::XML::SAX
- Nokogiri::XML::SAX::Document
- Nokogiri::XML::SAX::Parser
- Nokogiri::XML::SAX::Parser::Attribute
- Nokogiri::XML::SAX::ParserContext
- Nokogiri::XML::SAX::PushParser
- Nokogiri::XML::Schema
- Nokogiri::XML::SyntaxError
- Nokogiri::XML::Text
- Nokogiri::XML::XPath
- Nokogiri::XML::XPath::SyntaxError
- Nokogiri::XML::XPathContext
- Nokogiri::XSLT
- Nokogiri::XSLT::Stylesheet
- XSD
- XSD::XMLParser
- XSD::XMLParser::Nokogiri
- Object

No matching classes.

# Nokogiri::XSLT::Stylesheet

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  = Nokogiri::XSLT(File.read('some_transformer.xslt'))

    puts xslt.transform(doc)

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

### Public Class Methods

parse_stylesheet_doc(document) click to toggle source

Parse a stylesheet from `document`.

    static VALUE parse_stylesheet_doc(VALUE klass, VALUE xmldocobj)
    {
        xmlDocPtr xml, xml_cpy;
        VALUE errstr, exception;
        xsltStylesheetPtr ss ;
        Data_Get_Struct(xmldocobj, xmlDoc, xml);
        exsltRegisterAll();

        errstr = rb_str_new(0, 0);
        xsltSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler);

        xml_cpy = xmlCopyDoc(xml, 1); /* 1 => recursive */
        ss = xsltParseStylesheetDoc(xml_cpy);

        xsltSetGenericErrorFunc(NULL, NULL);

        if (!ss) {
            xmlFreeDoc(xml_cpy);
            exception = rb_exc_new3(rb_eRuntimeError, errstr);
            rb_exc_raise(exception);
        }

        return Nokogiri_wrap_xslt_stylesheet(ss);
    }

### Public Instance Methods

apply_to(document, params = []) click to toggle source

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

    # File lib/nokogiri/xslt/stylesheet.rb, line 20
    def apply_to document, params = []
      serialize(transform(document, params))
    end

serialize(document) click to toggle source

Serialize `document` to an xml string.

    static VALUE serialize(VALUE self, VALUE xmlobj)
    {
        xmlDocPtr xml ;
        nokogiriXsltStylesheetTuple *wrapper;
        xmlChar* doc_ptr ;
        int doc_len ;
        VALUE rval ;

        Data_Get_Struct(xmlobj, xmlDoc, xml);
        Data_Get_Struct(self, nokogiriXsltStylesheetTuple, wrapper);
        xsltSaveResultToString(&doc_ptr, &doc_len, xml, wrapper->ss);
        rval = NOKOGIRI_STR_NEW(doc_ptr, doc_len);
        xmlFree(doc_ptr);
        return rval ;
    }

transform(document, params = []) click to toggle source

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'])

    static VALUE transform(int argc, VALUE* argv, VALUE self)
    {
        VALUE xmldoc, paramobj, errstr, exception ;
        xmlDocPtr xml ;
        xmlDocPtr result ;
        nokogiriXsltStylesheetTuple *wrapper;
        const char** params ;
        long param_len, j ;
        int parse_error_occurred ;

        rb_scan_args(argc, argv, "11", &xmldoc, &paramobj);
        if (NIL_P(paramobj)) { paramobj = rb_ary_new2(0L) ; }
        if (!rb_obj_is_kind_of(xmldoc, cNokogiriXmlDocument))
          rb_raise(rb_eArgError, "argument must be a Nokogiri::XML::Document");

        /* handle hashes as arguments. */
        if(T_HASH == TYPE(paramobj)) {
          paramobj = rb_funcall(paramobj, rb_intern("to_a"), 0);
          paramobj = rb_funcall(paramobj, rb_intern("flatten"), 0);
        }

        Check_Type(paramobj, T_ARRAY);

        Data_Get_Struct(xmldoc, xmlDoc, xml);
        Data_Get_Struct(self, nokogiriXsltStylesheetTuple, wrapper);

        param_len = RARRAY_LEN(paramobj);
        params = calloc((size_t)param_len+1, sizeof(char*));
        for (j = 0 ; j < param_len ; j++) {
          VALUE entry = rb_ary_entry(paramobj, j);
          const char * ptr = StringValuePtr(entry);
          params[j] = ptr;
        }
        params[param_len] = 0 ;

        errstr = rb_str_new(0, 0);
        xsltSetGenericErrorFunc((void *)errstr, xslt_generic_error_handler);
        xmlSetGenericErrorFunc(NULL, (xmlGenericErrorFunc)&swallow_superfluous_xml_errors);

        result = xsltApplyStylesheet(wrapper->ss, xml, params);
        free(params);

        xsltSetGenericErrorFunc(NULL, NULL);
        xmlSetGenericErrorFunc(NULL, NULL);

        parse_error_occurred = (Qfalse == rb_funcall(errstr, rb_intern("empty?"), 0));

        if (parse_error_occurred) {
          exception = rb_exc_new3(rb_eRuntimeError, errstr);
          rb_exc_raise(exception);
        }

        return Nokogiri_wrap_xml_document((VALUE)0, result) ;
    }

[Validate]

Generated with the Darkfish Rdoc Generator 2.