Class: RecordxXSLT
- Inherits:
-
Object
- Object
- RecordxXSLT
- Defined in:
- lib/recordx-xslt.rb
Overview
file: recordx-xslt.rb
Instance Attribute Summary collapse
-
#schema ⇒ Object
Returns the value of attribute schema.
-
#xslt_schema ⇒ Object
Returns the value of attribute xslt_schema.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RecordxXSLT
constructor
A new instance of RecordxXSLT.
- #to_xslt ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RecordxXSLT
Returns a new instance of RecordxXSLT.
9 10 11 12 |
# File 'lib/recordx-xslt.rb', line 9 def initialize(={}) o = {schema: '', xslt_schema: ''}.merge() @schema, @xslt_schema = o.values end |
Instance Attribute Details
#schema ⇒ Object
Returns the value of attribute schema.
7 8 9 |
# File 'lib/recordx-xslt.rb', line 7 def schema @schema end |
#xslt_schema ⇒ Object
Returns the value of attribute xslt_schema.
7 8 9 |
# File 'lib/recordx-xslt.rb', line 7 def xslt_schema @xslt_schema end |
Instance Method Details
#to_xslt ⇒ Object
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 59 60 61 62 63 |
# File 'lib/recordx-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+/]} @xslt_schema = build_xslt_schema(@schema) if @xslt_schema.empty? a_html = @xslt_schema.split('/').map do |x| result = x.match(/(\w+)(?:[\(\[]([^\]\)]+)[\]\)])?(.*)/) name, children, remaining = result.captures if result list = children.split(/ *, */).map {|y| y.split(':',2)} 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 |