public
Description: ruby libxml library targetting speed and ease of use. provides an hpricot-like interface to xml
Homepage: http://trac.hasno.info/fastxml
Clone URL: git://github.com/segfault/fastxml.git
xslt support is now functional, yay
segfault (author)
Wed Jul 09 21:01:28 -0700 2008
commit  5fe746767df2456a451be0b562487038a71732a6
tree    2f172921e7f2a42761589e577fb103fb4efa695d
parent  e72f4aca12e7f251be3d7a86ff41a2a41c2f058c
...
38
39
40
 
41
42
43
...
71
72
73
 
 
 
74
75
76
...
79
80
81
 
82
83
84
...
38
39
40
41
42
43
44
...
72
73
74
75
76
77
78
79
80
...
83
84
85
86
87
88
89
0
@@ -38,6 +38,7 @@ void Init_fastxml()
0
 
0
     xmlInitParser();
0
     xmlXPathInit();
0
+ xsltInit();
0
     rb_mFastXml = rb_define_module( "FastXml" );
0
     rb_define_const( rb_mFastXml, "LIBXML_VERSION", rb_str_new2( LIBXML_DOTTED_VERSION ) );
0
 
0
@@ -71,6 +72,9 @@ void fastxml_data_free( fxml_data_t *data )
0
     if (data->xpath_obj != NULL)
0
       xmlXPathFreeObject( data->xpath_obj );      
0
   
0
+ if (data->xslt != NULL)
0
+ xsltFreeStylesheet( data->xslt );
0
+
0
         if (data->doc != NULL && data->node == NULL && data->list == NULL && data->xpath_obj == NULL)
0
             xmlFreeDoc( data->doc );
0
 
0
@@ -79,6 +83,7 @@ void fastxml_data_free( fxml_data_t *data )
0
     data->xpath_obj = NULL;
0
     data->list = NULL;
0
         data->doc = NULL;
0
+ data->xslt = NULL;
0
     data->node = NULL;
0
         free( data );
0
     }
...
33
34
35
36
37
38
39
40
...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
109
110
111
 
112
113
114
...
118
119
120
121
 
122
 
 
 
 
 
 
123
124
 
125
126
127
...
129
130
131
 
132
133
134
...
33
34
35
 
 
36
37
38
...
70
71
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
74
75
...
81
82
83
84
85
86
87
...
91
92
93
 
94
95
96
97
98
99
100
101
102
 
103
104
105
106
...
108
109
110
111
112
113
114
0
@@ -33,8 +33,6 @@ void Init_fastxml_doc()
0
     rb_define_method( rb_cFastXmlDoc, "to_s", fastxml_doc_to_s, 0 );
0
     rb_define_method( rb_cFastXmlDoc, "root", fastxml_doc_root, 0 );
0
   rb_define_method( rb_cFastXmlDoc, "transform", fastxml_doc_transform, 1 );
0
- rb_define_method( rb_cFastXmlDoc, "stylesheet=", fastxml_doc_stylesheet_set, 1 );
0
- rb_define_method( rb_cFastXmlDoc, "stylesheet", fastxml_doc_stylesheet, 0 );
0
   rb_define_method( rb_cFastXmlDoc, "children", fastxml_doc_children, 0 );  
0
   rb_define_method( rb_cFastXmlDoc, "inspect", fastxml_doc_inspect, 0 );
0
 }
0
@@ -72,32 +70,6 @@ VALUE fastxml_doc_children(VALUE self)
0
   return fastxml_nodelist_to_obj( data->doc->children, -1 );
0
 }
0
 
0
-/* Returns the FastXml::Doc that's defined as the xsl for this document
0
- */
0
-VALUE fastxml_doc_stylesheet(VALUE self)
0
-{
0
- return rb_iv_get( self, "@lxml_style" );
0
-}
0
-
0
-/*
0
- * call-seq:
0
- * doc.stylesheet = FastXml::Doc.new( open( 'my.xsl' ) )
0
- */
0
-VALUE fastxml_doc_stylesheet_set(VALUE self, VALUE style)
0
-{
0
- VALUE dv, xslt_doc;
0
- fxml_data_t *data;
0
-
0
- xslt_doc = rb_class_new_instance(1, &style, rb_cFastXmlDoc );
0
-
0
- dv = rb_iv_get( xslt_doc, "@lxml_doc" );
0
- Data_Get_Struct( dv, fxml_data_t, data );
0
- data->xslt = xsltParseStylesheetDoc( data->doc );
0
- rb_iv_set( self, "@lxml_style", xslt_doc );
0
-
0
- return Qnil;
0
-}
0
-
0
 /* Applys an XSLT to the target FastXml::Doc.
0
  * Returns the resulting FastXml::Doc
0
  *
0
@@ -109,6 +81,7 @@ VALUE fastxml_doc_transform(VALUE self, VALUE xform)
0
   VALUE ret, dv, xform_dv, ret_str, ret_dv;
0
   fxml_data_t *my_data, *xf_data, *ret_data;
0
   xmlDocPtr ret_doc;
0
+ xsltStylesheetPtr style;
0
 
0
   if (xform == Qnil)
0
     return Qnil;
0
@@ -118,10 +91,16 @@ VALUE fastxml_doc_transform(VALUE self, VALUE xform)
0
   xform_dv = rb_iv_get( xform, "@lxml_doc" );
0
   Data_Get_Struct( xform_dv, fxml_data_t, xf_data );
0
   
0
- if (xf_data->xslt == NULL)
0
+ if (xf_data->doc == NULL)
0
     return Qnil;
0
+
0
+ if (xf_data->xslt == NULL) {
0
+ style = xsltParseStylesheetDoc( xf_data->doc );
0
+ if (style == NULL)
0
+ return Qnil; // TODO: this should throw a FastXml exception
0
+ }
0
 
0
- ret_doc = (xmlDocPtr)xsltApplyStylesheet( xf_data->xslt, my_data->doc, NULL );
0
+ ret_doc = (xmlDocPtr)xsltApplyStylesheet( style, my_data->doc, NULL );
0
   ret_str = rb_str_new2( "<shouldNeverBeSeen/>" );
0
   ret = rb_class_new_instance( 1, &ret_str, rb_cFastXmlDoc ); // provide an xml snipped temporarily
0
   ret_dv = rb_iv_get( ret, "@lxml_doc" );
0
@@ -129,6 +108,7 @@ VALUE fastxml_doc_transform(VALUE self, VALUE xform)
0
   xmlFree( ret_data->doc );
0
   ret_data->doc = ret_doc;
0
   
0
+
0
   return ret;
0
 }
0
 
...
80
81
82
83
84
85
86
87
88
89
90
 
 
 
91
92
 
93
94
95
96
 
 
 
97
98
 
 
 
 
 
 
 
 
 
 
 
 
99
100
...
80
81
82
 
 
 
 
 
 
 
 
83
84
85
86
 
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
0
@@ -80,21 +80,31 @@ describe FastXml::Doc, " functionality" do
0
     rlm.should_not == @doc
0
   end
0
   
0
- it 'should be able to have an xsl assigned' do
0
- doc = FastXml( open("./test_data/transform_base.xml") )
0
- doc.should_not be_nil
0
- tran = FastXml( open("./test_data/transform.xsl") )
0
- tran.should_not be_nil
0
- doc.stylesheet = tran
0
- doc.stylesheet.should_not be_nil
0
- end
0
+ it 'should provide an xml stylesheet transform method' do
0
+ @doc.should respond_to( :transform )
0
+ end
0
   
0
- it 'should be able to have an xslt assigned' do
0
+ it 'should be able to be transformed using another xml document (xslt)' do
0
     doc = FastXml( open("./test_data/transform_base.xml") )
0
     doc.should_not be_nil
0
     tran = FastXml( open("./test_data/transform.xsl") )
0
     tran.should_not be_nil
0
+ tran.to_s.should_not be_nil
0
+ tran.to_s.length.should > 5
0
+
0
     result = doc.transform( tran )
0
     result.should_not be_nil
0
+ names = (result/"/name")
0
+ names.should_not be_nil
0
+ names.length.should == 2
0
+ result.to_s.should_not be_nil
0
+ result.to_s.length.should > 5
0
+ end
0
+
0
+ it 'should return when a nil transform is applied' do
0
+ doc = FastXml( open("./test_data/transform_base.xml") )
0
+ doc.should_not be_nil
0
+ res = doc.transform( nil )
0
+ res.should be_nil
0
   end
0
 end

Comments

    No one has commented yet.