Visual Studio XGen - Example

If you are an XML and XSLT guru then this should be home turf for you. If you are new to XML and XSLT then you might want to read up a little on XML and XSLT. But for both the expert and the novice, here's a simple example.

The xtransform document

First you need an xtransform document. The xtransform document should have extension .xtr.xml. It describes how to use the .xslt document to convert the .xml document into the output document. You can also give an arbitrary number of parameters.
<?xml version="1.0" encoding="utf-8" ?>
<xtransformations xmlns = "http://vsxgen.sourceforge.net/XTransformV1.xsd">
  <xtransform xsource="input.xml" xtemplate="template.xslt" output="output.cs">
    <param name="type" value="DAL"/>
  </xtransform>
  <xtransform xsource="input.xml" xtemplate="[xgenlib]\template.xslt" output="output.sql">
    <param name="type" value="SP"/>
  </xtransform>
</xtransformations>
Please note the use of http://vsxgen.sourceforge.net/XTransformV1.xsd as a name space. Also, note the use of [xgenlib]. That is a project variable. The use of [xgenlib] assumes that there is a project called xgenlib (or XGenLib, it is case insensitive) and the angle brackets and everything between them will be replaced with the working folder for that project.

The .xml document

<?xml version="1.0" encoding="utf-8" ?>
<xsource>
  using System.Xml;
  using System;
  using System.Runtime.Serialization;
  
  namespace ConsoleApplication1 {
    <exception name="ExampleException1" base="ApplicationException" />
    <exception name="ExampleException2" base="ApplicationException" />
  }
</xsource>

Please note that you can write normal code (C# in this example) in the xsource document. In this case the "namespace ConsoleApplication1 {" will be moved verbatim to the result file. You need to remember though that the less than sign has special meaning. So if you want to write "x < 2" then you need to write &lt; instead of the less than sign.

The .xslt file

We also need the template referred to in the xtemplate attribute. Please note how the "type" parameter is declared and used.

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

<template match="xsource">
  //<value-of select="$type"/>
  <apply-templates />
</template>

<template match="exception">
  [Serializable]
  public class <value-of select="@name" /> : <value-of select="@base" />
  {
    // Default constructor
    public <value-of select="@name" />() : base()
    {
    }
    // Constructor with exception message
    public <value-of select="@name" />(string message) : base(message)
    {
    } // Constructor with message and inner exception
    public <value-of select="@name" />(string message, Exception inner)
      : base(message,inner)
    {
    }
    // Protected constructor to de-serialize data
    protected <value-of select="@name" />(SerializationInfo info, 
    StreamingContext context) : base(info, context)
    {
    }
    }</template>
  
</stylesheet>

So there you have it.