class XMLToXSL

Attributes

to_xsl[R]

Public Class Methods

new(xml) click to toggle source
# File lib/xml_to_xsl.rb, line 12
  def initialize(xml)

    doc = Rexle.new(xml)

  @to_xsl = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'"\
  " version='1.0'>

  <xsl:template match='#{doc.root.name}'>

    <xsl:element name='#{doc.root.name}'>
  #{write_attributes(doc.root.attributes).join("\n")}
      #{build_xml(doc.root)}
    </xsl:element>

  </xsl:template>

</xsl:stylesheet>"

  end

Private Instance Methods

build_template(element) click to toggle source
# File lib/xml_to_xsl.rb, line 64
def build_template(element)

  "<xsl:apply-templates select='#{element.name}' />
  </xsl:element>

</xsl:template>

<xsl:template match='#{element.name}'>"

end
build_value(element,parent) click to toggle source
# File lib/xml_to_xsl.rb, line 75
  def build_value(element,parent)

  "\n
    <xsl:element name='#{parent.name}'>
#{write_attributes(parent.attributes).join("\n")}
      <xsl:apply-templates select='#{element.name}' />
    </xsl:element>

  </xsl:template>

  <xsl:template match='#{element.name}'>

    <xsl:element name='#{element.name}'>
#{write_attributes(element.attributes).join("\n")}
      <xsl:value-of select='.' />"

  end
build_xml(node) click to toggle source
# File lib/xml_to_xsl.rb, line 35
def build_xml(node)

  r = []

  previous_name = ''


  node.elements.each do |child| 


    if child.children.length > 1 

      r << build_template(child)

      r << build_xml(child) 

    else

      r << build_value(child,node) if child.name != previous_name

      previous_name = child.name

    end

  end
  r.join

end
write_attributes(attr) click to toggle source
# File lib/xml_to_xsl.rb, line 93
def write_attributes(attr)
  
  attr.map do |key, val|
    "      <xsl:attribute name='#{key}'>
      <xsl:value-of select='@#{key}'/>
    </xsl:attribute>"
  end
  
end