Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Generate Ruby code using XML and XSL (See related posts)

This XSL code transforms an XML file into a basic Ruby file. See the previous post ('Transforming an XML file into an XSL file' http://snipr.com/1usrh [dzone.com]) for an example of the XML used.

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

  <xsl:template match="recordx">
  <xsl:variable name="project" select="summary/project" />
  <xsl:variable name="parent_element" select="summary/parent_element" />
  <xsl:text>#!/usr/bin/ruby
#file: </xsl:text><xsl:value-of select="$project"/><xsl:text>.rb

require 'recordx'

class </xsl:text><xsl:value-of select="$project"/><xsl:text> &lt; RecordX

  def initialize()
    @project = "</xsl:text><xsl:value-of select="$project"/><xsl:text>"
    @parent_element = '</xsl:text><xsl:value-of select="$parent_element"/><xsl:text>fields'
    @proj = Projxml.new
    @doc_vrecord = initialize_xml_class(SERVER_URL + '/' + @project + '/class_template.xml')
    @doc_file = get_doc()
  end
  
end

if __FILE__ == $0
    r = </xsl:text><xsl:value-of select="$project"/><xsl:text>.new()
end
  </xsl:text>
  </xsl:template>
  
</xsl:stylesheet>

You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts