Class: Nokogiri::XSLT::Stylesheet
- Inherits:
-
Object
- Object
- Nokogiri::XSLT::Stylesheet
- Defined in:
- lib/nokogiri/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 = Nokogiri::XSLT(File.read('some_transformer.xslt'))
puts xslt.transform(doc)
See Nokogiri::XSLT::Stylesheet#transform for more transformation information.
Class Method Summary collapse
-
.parse_stylesheet_doc(document) ⇒ Object
Parse a stylesheet from
document
.
Instance Method Summary collapse
-
#apply_to(document, params = []) ⇒ Object
Apply an XSLT stylesheet to an XML::Document.
-
#serialize(document) ⇒ Object
Serialize
document
to an xml string. -
#transform(document, params = []) ⇒ Object
Apply an XSLT stylesheet to an XML::Document.
Class Method Details
.parse_stylesheet_doc(document) ⇒ Object
Parse a stylesheet from document
.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'ext/nokogiri/xslt_stylesheet.c', line 64
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);
}
|
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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'ext/nokogiri/xslt_stylesheet.c', line 96
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 = []) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'ext/nokogiri/xslt_stylesheet.c', line 131
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, ¶mobj);
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 = StringValueCStr(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) ;
}
|