calin radoni's humble web presence

homedocstoolboxabout

Using Visual Studio and xsltproc to create xml projects for transforming .xml files to .html files

Table of Contents

Introduction

If you use .xml files to generate .html ones with the xsltproc application under (unfortunatelly) Windows the following procedure could help you a lot (at least, it helped me).

Benefits

Choosing Debug -> Start from Visual Studio's main menu will convert your .xml files to .html and will launch the main one in the default browser (the newly created index.html).

Requirements

Here are the requirements needed to implement the solution the way I have done it:

  1. The xsltproc application should be installed and configured
  2. Visual Studio must be installed
  3. ... and a little pacience

Project/Solution creation

I will start with an empty C# project and add basic folders and documents:

  1. Create an empty C# project by selecting File -> New -> Project from the Visual Studio's main menu then select Visual C# Projects -> Empty Project;
  2. Create the first .xml document (index.xml);
  3. Add some folders:
  4. In the xsl directory create your .xsl files (header.xsl, footer.xsl and normal.xsl as an example);
  5. In the css create your .css file(s), let say for start the file default.css;
  6. Add a new C# file with a name like <yourProjectName>.cs.
    The purpose of this file is to be build by Visual Studio when you choose one of the Build options and to launch the default, resultant, .html file when you run it.
    The content that you should add for this file is described in the next section;
  7. Create an empty file named build.bat;
  8. Create an empty file named finish.bat.
Now, the required files and folders are created.
Read the next section to add functionality for the <yourProjectName>.cs, build.bat and finish.bat files.

The <yourProjectName>.cs file

Add two using directives at the head of your file:

using System.Diagnostics;
using System.Windows.Forms;
					

Inside you class add the following (quite ugly and unpolished) function:

[STAThread]
static void Main() 
{
	string str;
	string delim = "\\";
	char[] delimiters = delim.ToCharArray();
	string[] splits = null;
	int i, cnt;
	str = Application.StartupPath;
	splits = str.Split(delimiters);
	cnt = splits.Length;

	str = splits[cnt-1];
	if(str.CompareTo("Debug")==0 || str.CompareTo("Release")==0)
	{
		cnt -=2 ;
		str = "";
		for(i=0; i<cnt; i++)
		{
			str += splits[i];
			str += "\\";
		}
		str += "build\\index.html";
	}
	else
	{
		str = "index.html";
	}

	System.Diagnostics.Process objProc =
		System.Diagnostics.Process.Start(str);
}
					

Then, for a correct build, you should add a reference to the System.Windows.Forms component:

  1. In Solution Explorer, under your project, right-click References and select Add Reference;
  2. In the Add Reference window, from the .NET tab, select the System.Windows.Forms.dll component;
  3. click the Select button;
  4. click the OK button.

The build.bat file

cd..
cd..

del /F /S /Q .\build\*

copy .\css\* .\build\css\

copy .\images\*.jpg .\build\images\
copy .\images\*.png .\build\images\

xsltproc xsl\normal.xsl index.xml > .\build\index.html
					

For each one of your .xml files you should add a line to the build.bat file like the one for index.xml file.

The finish.bat file

This file is really simple, it copies the generated executable file to the build directory.
This file is needed only if you want to distribute the generated executable file along with your .html files.

copy .\yourProjectName.exe ..\..\build\
					

Glue

The file build.bat must be added as a pre-build event:

  1. In Solution Explorer right-click your project name and select Properties;
  2. In the Properties window, under Common Properties select Build Events;
  3. In the Pre-build Event Command Line add:
    call $(ProjectDir)build.bat
    							

The file finish.bat must be added as a post-build event:

  1. In Solution Explorer right-click your project name and select Properties;
  2. In the Properties window, under Common Properties select Build Events;
  3. In the Post-build Event Command Line add:
    call $(ProjectDir)finish.bat
    							

For Run the Post-Build Event? you should have On successful build (otherwise set it).

Now, if you select Debug -> Start from Visual Studio's main menu your resulting index.html file should be displayed in the default browser (of course, if there are no errors).

Project Editing

If you have done everything that I have enumerated above you are ready to edit you files as you wish viewing the result by just a single F5 press (or Debug -> Start command).

For easy and consistent work you could now build a model.xml file as a basis for all your new .xml files.

Adding a new .xml file involves the following steps:

  1. Add the file as usual;
  2. Edit the build.bat file;
  3. Start by filling it from the model.xml file.

History

Copyright and License

This document is copyrighted (c) 2005 by Calin Radoni. Permission is granted to copy and/or distribute this document.

Disclaimer

No liability for the contents of this document can be accepted. Use the concepts, examples and information at your own risk. There may be errors and inaccuracies that could be damaging to your system. Proceed with caution, the author do not take any responsibility.

All copyrights are held by their respective owners, unless specifically noted otherwise. Use of a term in this document should not be regarded as affecting the validity of any trademark or service mark. Naming of particular products or brands should not be seen as endorsements.


Copyright © 2005 - 2009 Calin Radoni Hosted on http://www.oocities.org/calinradoni Last page modification is 16 November 2005