rexslt 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/rexslt.rb +77 -15
- metadata +2 -2
data/lib/rexslt.rb
CHANGED
@@ -8,16 +8,19 @@ class Rexslt
|
|
8
8
|
doc_xml = Document.new(xml)
|
9
9
|
doc_xsl = Document.new(xsl)
|
10
10
|
|
11
|
-
@xsl_methods = [:apply_templates, :value_of]
|
11
|
+
@xsl_methods = [:apply_templates, :value_of, :element, :if, :choose, :when]
|
12
12
|
|
13
13
|
# fetch the templates
|
14
14
|
@templates = XPath.match(doc_xsl.root, 'xsl:template').inject({}) do |r,x|
|
15
15
|
r.merge(x.attribute('match').value => x)
|
16
16
|
end
|
17
17
|
|
18
|
+
xpath = String.new @templates.to_a[0][0]
|
19
|
+
o = XPath.first(doc_xml, xpath)
|
20
|
+
|
18
21
|
# using the 1st template
|
19
|
-
@a = XPath.match(doc_xml,
|
20
|
-
|
22
|
+
@a = XPath.match(doc_xml, xpath).map do |x|
|
23
|
+
read_node @templates.to_a[0][-1], x
|
21
24
|
end
|
22
25
|
|
23
26
|
end
|
@@ -30,29 +33,88 @@ class Rexslt
|
|
30
33
|
|
31
34
|
private
|
32
35
|
|
33
|
-
def
|
34
|
-
|
35
|
-
XPath.match(template_node, '*').map do |x|
|
36
|
+
def read_node(template_node, doc_node)
|
36
37
|
|
37
|
-
|
38
|
+
procs = {"REXML::Element" => :read_raw_element, "REXML::Text" => :read_raw_text}
|
39
|
+
template_node.map do |x|
|
40
|
+
#puts 'x : ' + x.inspect
|
41
|
+
method(procs[x.class.to_s]).call(doc_node, x)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
38
45
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
46
|
+
def read_raw_text(doc_node, x)
|
47
|
+
r = x.to_s.strip.length > 0 ? x.to_s : ''
|
48
|
+
#puts 'xx : ' + r.inspect
|
49
|
+
r
|
50
|
+
end
|
51
|
+
|
52
|
+
def read_raw_element(doc_node, x)
|
53
|
+
method_name = x.name.gsub(/-/,'_').to_sym
|
54
|
+
if @xsl_methods.include? method_name then
|
55
|
+
|
56
|
+
method(method_name).call(doc_node, x)
|
57
|
+
else
|
58
|
+
|
59
|
+
if x.has_elements? then
|
60
|
+
element = "<%s>%%s</%s>" % ([x.name.to_s] * 2)
|
61
|
+
k = element % read_node(x, doc_node).flatten.join
|
62
|
+
#puts 'k : ' + k.inspect
|
63
|
+
k
|
64
|
+
else
|
43
65
|
x.to_s
|
44
66
|
end
|
45
|
-
end
|
67
|
+
end
|
46
68
|
end
|
47
69
|
|
48
|
-
def apply_templates(doc_node,
|
70
|
+
def apply_templates(doc_node, x)
|
71
|
+
field = x.attribute('select').value.to_s
|
49
72
|
XPath.match(doc_node, field).map do |x|
|
50
|
-
|
73
|
+
read_node @templates[field], x
|
51
74
|
end
|
52
75
|
end
|
53
76
|
|
54
|
-
def value_of(doc_node,
|
77
|
+
def value_of(doc_node, x)
|
78
|
+
field = x.attribute('select').value.to_s
|
55
79
|
doc_node.text(field)
|
56
80
|
end
|
57
81
|
|
82
|
+
def element(doc_node, x)
|
83
|
+
name = x.attribute('name').value.to_s
|
84
|
+
element = "<%s>%%s</%s>" % ([name] * 2)
|
85
|
+
r = element % (read_node(x, doc_node).flatten.join)
|
86
|
+
#puts 'r : ' + r.inspect
|
87
|
+
r
|
88
|
+
end
|
89
|
+
|
90
|
+
def if(doc_node, x)
|
91
|
+
condition = x.attribute('test').value.to_s
|
92
|
+
node = XPath.first(doc_node, condition)
|
93
|
+
read_node(x, doc_node).flatten.join if node and node == true
|
94
|
+
end
|
95
|
+
|
96
|
+
def choose(doc_node, x)
|
97
|
+
|
98
|
+
#get the when clause
|
99
|
+
nodes = XPath.match(x, "xsl:when")
|
100
|
+
|
101
|
+
r = nodes.map do |xsl_node|
|
102
|
+
|
103
|
+
condition = xsl_node.attribute('test').value.to_s
|
104
|
+
node = XPath.first(doc_node, condition)
|
105
|
+
|
106
|
+
if node and node == true then
|
107
|
+
read_node(xsl_node, doc_node).flatten.join
|
108
|
+
else
|
109
|
+
nil
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
if r.compact.length <= 0 then
|
114
|
+
xsl_node = XPath.first(x, 'xsl:otherwise')
|
115
|
+
r = read_node(xsl_node, doc_node).flatten.join if xsl_node
|
116
|
+
end
|
117
|
+
|
118
|
+
r
|
119
|
+
end
|
58
120
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rexslt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.3
|
6
6
|
platform: ruby
|
7
7
|
authors: []
|
8
8
|
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-04-07 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|