Class: DynarexXSLT

Inherits:
Object
  • Object
show all
Defined in:
lib/dynarex-xslt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DynarexXSLT

Returns a new instance of DynarexXSLT.



12
13
14
15
# File 'lib/dynarex-xslt.rb', line 12

def initialize(options={})
  o = {schema: '', xslt_schema: ''}.merge(options)
  @schema, @xslt_schema = o.values
end

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



10
11
12
# File 'lib/dynarex-xslt.rb', line 10

def schema
  @schema
end

#xslt_schemaObject

Returns the value of attribute xslt_schema.



10
11
12
# File 'lib/dynarex-xslt.rb', line 10

def xslt_schema
  @xslt_schema
end

Instance Method Details

#to_xsltObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dynarex-xslt.rb', line 17

def to_xslt()

  a_element = @schema.split('/').map{|x| x[/\w+/]}

  a_html = @xslt_schema.split('/').map do |x|
      
    name = x[/\w+/]
    children = ($')[/^[\(\[]([^\)\]]+)[\)\]]$/,1]
    list = children.split(',').map {|y| y.split(':') } if children

    [name, list]
  end

  a = a_element.zip(a_html)

  xml = RexleBuilder.new
  raw_a = xml.xsl_stylesheet(xmlns_xsl: "http://www.w3.org/1999/XSL/Transform", version: "1.0") do
    xml.xsl_output(method: "xml", indent: "yes")

    xml.xsl_template(match: a[0][0]) do
      xml.send a[0][1][0].to_sym do
        xml.xsl_apply_templates(select: 'summary')
        xml.xsl_apply_templates(select: 'records')
      end 
    end

    xml.xsl_template(match: a[0][0] + '/summary') do
      summary_fields = a[0][1][1] || []
      summary_fields.each do |element, field|
        xml.send (element.strip).to_sym do
          xml.xsl_value_of(select: field)
        end
      end
    end

    xml.xsl_template(match: 'records/' + a[1][0]) do
      xml.send a[1][1][0].to_sym do
        a[1][1][1].each do |element, field|
          xml.send element.to_sym do
            xml.xsl_value_of(select: field)
          end
        end      
      end
    end

  end
  
  Rexle.new(raw_a).xml(pretty: true).gsub('xsl_apply_templates',\
      'xsl:apply-templates').gsub('xsl_value_of','xsl:value-of').\
      gsub('xsl_template','xsl:template').gsub('xsl_','xsl:').\
      gsub('xmlns_xsl','xmlns:xsl')

end