Re: [xsl] Code Generation

Subject: Re: [xsl] Code Generation
From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
Date: Wed, 01 May 2002 00:32:40 +0200
doug@xxxxxxxxxxx wrote:
Can anyone point me towards XML/XSLT resources for generating Java source
code?  I'm looking for a simple working template that builds a basic .java
file so that I can customize it to build more complex methods than the
setter/getter methods that are generated by other frameworks I've been
investigating.

Cocoon generates Java source from XSP files using XSLT. The most important point is to use the text output method and perhaps an output encoding the Java compiler doesn't choke on. Further issues are whether you want to have a human redable output, beautifully indented (you have to do it yourself, indent="yes" doesn't work here), and how you want to relate problems reported by the compiler or during run time (stack traces) to your source XML or the boilerplate code in your style sheet. Saxon has a lineNumber() extension function, which can help here (put the source line number in a comment).

Here is also a simplistic example (caution: badly designed
XML):

XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<class name="stuff">
  <attribute name="foo" type="int"/>
</class>

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

  <xsl:template match="class">
    <xsl:text>public class </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text> {&#x0A;</xsl:text>
    <xsl:apply-templates select="attribute"/>
    <xsl:text>}&#x0A;</xsl:text>
  </xsl:template>

  <xsl:template match="attribute">
    <xsl:text>  private </xsl:text>
    <xsl:value-of select="@type"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text> ;&#x0A;</xsl:text>
    <xsl:text>  public </xsl:text>
    <xsl:value-of select="@type"/>
    <xsl:text> get</xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text> () { return </xsl:text>
    <xsl:value-of select="@name"/>
    <xsl:text> ; }&#x0A;</xsl:text>
  </xsl:template>
</xsl:stylesheet>

J.Pietschmann


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread