Class: PolyrexXSLT

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

Overview

file: polyrex-xslt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PolyrexXSLT

Returns a new instance of PolyrexXSLT.



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

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.



7
8
9
# File 'lib/polyrex-xslt.rb', line 7

def schema
  @schema
end

#xslt_schemaObject

Returns the value of attribute xslt_schema.



7
8
9
# File 'lib/polyrex-xslt.rb', line 7

def xslt_schema
  @xslt_schema
end

Instance Method Details

#to_xsltObject



14
15
16
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
# File 'lib/polyrex-xslt.rb', line 14

def to_xslt()

header =<<HEADER
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />

HEADER

  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).map.with_index do |a,i|
    out = []
    tag = a.shift
    field = i > 0 ? 'records/' + tag : tag
    out << "<xsl:template match='#{field}'>" + "\n"
    a.flatten!(1)
    if a.last.is_a? Array then

      out << scan_e(a, tag) 
      out << "</xsl:template>\n\n"
    else
      out << "  <%s>\n" % a.first
      out << "    <xsl:apply-templates select='summary'/>\n"
      out << "    <xsl:apply-templates select='records'/>\n"
      out << "  <\\%s>\n" % a.first
      out << "</xsl:template>\n\n"
      out << "<xsl:template match='%s/summary'>\n" % [tag]
      out << "</xsl:template>\n\n"
    end
    
    out
  end

  header + a.flatten.join + "</xsl:stylesheet>"
end