recordx-xslt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/recordx-xslt.rb +90 -0
- metadata +56 -0
data/lib/recordx-xslt.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
#!/usr/bin/envruby
|
2
|
+
|
3
|
+
# file: recordx-xslt.rb
|
4
|
+
|
5
|
+
class RecordxXSLT
|
6
|
+
|
7
|
+
attr_accessor :schema, :xslt_schema
|
8
|
+
|
9
|
+
def initialize(options={})
|
10
|
+
o = {schema: '', xslt_schema: ''}.merge(options)
|
11
|
+
@schema, @xslt_schema = o.values
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_xslt()
|
15
|
+
|
16
|
+
header =<<HEADER
|
17
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
18
|
+
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
|
19
|
+
<xsl:output method="xml" indent="yes" />
|
20
|
+
|
21
|
+
HEADER
|
22
|
+
|
23
|
+
a_element = @schema.split('/').map{|x| x[/\w+/]}
|
24
|
+
|
25
|
+
a_html = @xslt_schema.split('/').map do |x|
|
26
|
+
|
27
|
+
name = x[/\w+/]
|
28
|
+
children = ($')[/^[\(\[]([^\)\]]+)[\)\]]$/,1]
|
29
|
+
list = children.split(',').map {|y| y.split(':') } if children
|
30
|
+
|
31
|
+
[name, list]
|
32
|
+
end
|
33
|
+
|
34
|
+
a = a_element.zip(a_html).map.with_index do |a,i|
|
35
|
+
out = []
|
36
|
+
tag = a.shift
|
37
|
+
field = i > 0 ? 'records/' + tag : tag
|
38
|
+
out << "<xsl:template match='#{field}'>" + "\n"
|
39
|
+
a.flatten!(1)
|
40
|
+
if a.last.is_a? Array then
|
41
|
+
|
42
|
+
out << scan_e(a, tag)
|
43
|
+
out << "</xsl:template>\n\n"
|
44
|
+
else
|
45
|
+
out << " <%s>\n" % a.first
|
46
|
+
out << " <xsl:apply-templates select='summary'/>\n"
|
47
|
+
out << " <xsl:apply-templates select='records'/>\n"
|
48
|
+
out << " <\\%s>\n" % a.first
|
49
|
+
out << "</xsl:template>\n\n"
|
50
|
+
out << "<xsl:template match='%s/summary'>\n" % [tag]
|
51
|
+
out << "</xsl:template>\n\n"
|
52
|
+
end
|
53
|
+
|
54
|
+
out
|
55
|
+
end
|
56
|
+
|
57
|
+
header + a.flatten.join + "</xsl:stylesheet>"
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def scan_e(a, prev_tag='', indent=' ')
|
63
|
+
|
64
|
+
out = []
|
65
|
+
|
66
|
+
unless a.first.is_a? Array then
|
67
|
+
tag = a.shift
|
68
|
+
out << indent + "<%s>\n" % tag
|
69
|
+
out << indent + " <xsl:apply-templates select='summary'/>\n"
|
70
|
+
out << indent + "<\\%s>\n" % tag
|
71
|
+
out << "</xsl:template>\n\n"
|
72
|
+
out << "<xsl:template match='%s/summary'>\n" % [prev_tag]
|
73
|
+
|
74
|
+
a.flatten!(1)
|
75
|
+
if a.last.is_a? Array then
|
76
|
+
out << scan_e(a, tag, indent + ' ')
|
77
|
+
else
|
78
|
+
out << indent + ' ' + a.first + "\n"
|
79
|
+
end
|
80
|
+
|
81
|
+
else
|
82
|
+
a.map do |x|
|
83
|
+
out << indent + "<%s><xsl:value-of select='%s'/></%s>\n" % [x[0],x[-1],x[0]]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
out
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: recordx-xslt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- James Robertson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-03-15 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/recordx-xslt.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: recordx-xslt
|
55
|
+
test_files: []
|
56
|
+
|