rexslt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rexslt.rb +54 -0
  2. metadata +56 -0
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: rexslt.rb
4
+
5
+ require 'rexml/document'
6
+
7
+ class Rexslt
8
+ include REXML
9
+
10
+ def initialize(xsl, xml)
11
+
12
+ doc_xml = Document.new(xml)
13
+ doc_xsl = Document.new(xsl)
14
+
15
+ # fetch the templates
16
+ @templates = XPath.match(doc_xsl.root, 'xsl:template').inject({}) do |r,x|
17
+ r.merge(x.attribute('match').value => x)
18
+ end
19
+
20
+ # using the 1st template
21
+ @a = XPath.match(doc_xml, @templates.to_a[0][0]).map do |x|
22
+ read_template @templates.to_a[0][-1], x
23
+ end
24
+
25
+ end
26
+
27
+ def to_s()
28
+ @a.flatten.join
29
+ end
30
+
31
+ alias text to_s
32
+
33
+ private
34
+
35
+ def read_template(template_node, doc_node)
36
+
37
+ XPath.match(template_node, '*').map do |x|
38
+ method_name = x.name.gsub(/-/,'_').to_sym
39
+ field = x.attribute('select').value.to_s
40
+ method(method_name).call(doc_node, field)
41
+ end
42
+ end
43
+
44
+ def apply_templates(doc_node, field)
45
+ XPath.match(doc_node, field).map do |x|
46
+ read_template @templates[field], x
47
+ end
48
+ end
49
+
50
+ def value_of(doc_node, field)
51
+ doc_node.text(field)
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rexslt
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors: []
8
+
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-08 00:00:00 +00:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/rexslt.rb
27
+ has_rdoc: true
28
+ homepage:
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.5.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: rexslt
55
+ test_files: []
56
+