calin radoni's humble web presence |
home docs toolbox about
|
Using Visual Studio and xsltproc to create xml projects for transforming .xml files to .html files
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).
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).
Here are the requirements needed to implement the solution the way I have done it:
xsltproc application should be installed and configuredVisual Studio must be installedI will start with an empty C# project and add basic folders and documents:
File -> New -> Project from
the Visual Studio's main menu then select
Visual C# Projects -> Empty Project;
.xml document (index.xml);
build, to host the resulting .html filesbuild/cssbuild/imagescssimagesxsl, to host the .xsl filesxsl directory create your .xsl files
(header.xsl, footer.xsl and normal.xsl as an example);
css create your .css file(s), let say for
start the file default.css;
<yourProjectName>.cs.Solution Explorer right-click your project name and select
Add -> Add New Item;
Code on the left and the
Class template on the right;
Open button.
Visual Studio when you choose one of the
Build options and to launch the default, resultant,
.html file when you run it.build.bat;
finish.bat.
<yourProjectName>.cs,
build.bat and finish.bat files.
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:
Solution Explorer, under your project, right-click
References and select
Add Reference;
Add Reference window, from the
.NET tab, select the
System.Windows.Forms.dll component;
Select button;OK button.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.
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\
The file build.bat must be added as a pre-build event:
Solution Explorer right-click your project name and select
Properties;
Properties window, under Common Properties
select Build Events;
Pre-build Event Command Line add:
call $(ProjectDir)build.bat
The file finish.bat must be added as a post-build event:
Solution Explorer right-click your project name and select
Properties;
Properties window, under Common Properties
select Build Events;
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).
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:
build.bat file;model.xml file.This document is copyrighted (c) 2005 by Calin Radoni. Permission is granted to copy and/or distribute this document.
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.