Using stylesheets with DBToy


In this tutorial I'll show you how to modify dbtoy's output, using custom stylesheets.

How it works

As of release 0.6, dbtoy lets you insert a fragment of your choice in its xml files, between the header and the root tag. This feature can be used to add a reference to an xsl stylesheet to all the files, another use could be the insertion of a disclaimer or a comment.
To add a stylesheet to dbtoy files, just add the '-x' option to your command line arguments, e.g.:

[user@testlinux]$ dbtoy -u user -p my_pwd -d mysql -x'<?xml-stylesheet type="text/xsl" href="file:///path/to/file.xsl"?>' /mnt/dbtoy

Example A: An HTML Report with your data

Transforming xml files in html is a common task, there are many examples on the net of doing such transformation, so I'll show you a very simple xsl, to play with as you like. Please consider that I'm not a web designer and not even an xsl expert, so don't complain if the resulting pages will not be so good-looking, ok?
Here is the script:


Looking at the code, pleas note that: Paste the script in a file (or download it from the link at the end of the page) and run dbtoy with the right string for '-x'. To see the result of the transormation, open some file within firefox. This is what I got using a test table: (click for a larger version)

And this is part of the generated html code:

That's it. With a bit of work you could make it prettier, and add some nice feature like: sorting, pagination, etc.

Example B: Generate SQL scripts

In this second example, I'll show you how to generate SQL code from dbtoy xml files. Data files will be transformed in a sequence of insert statements, and types will become ddl scripts.

Here is the script:



And this is the output with my test table:

-- DDL script. Generated from dbtoy output.
create table user (
 int(11) id PRIMARY KEY,
 varchar(64) login ,
 varchar(64) password ,
 enum('yes','no') enabled ,
);

-- Table data insert script. Generated from dbtoy output
insert into user values(
  '1',
  'domenico',
  'domepw1',
  'yes',
 );
insert into user values(
  '2',
  'andrea',
  'ris1pl',
  'yes',
 );
insert into user values(
  '3',
  'luca',
  'def_12p',
  'yes',
 );
insert into user values(
  '4',
  'maek',
  'rino76',
  'yes',
 );
insert into user values(
  '5',
  'pino_dd',
  'para0m9',
  'yes',
 );
insert into user values(
  '6',
  'enzo',
  'enzo2',
  'no',
 );
...

Conclusion

I hope this tutorial has been helpful, feel free to write me a comment at pinguino{at}thesaguaros[d0t]com.
You can download the files used in the examples here