t's becoming common to retrieve data from a relational database formatted as a hierarchical XML document. By using an XML schema, it's possible to generate data from XML documents and then create a relational database from the schema in which to store the datain other words, the schema relationship can apply in both directions.
It's also common to use XSLT to format existing XML data, but using XSLT to generate data is not as intuitive. Subsequently, it should be possible to use XSLT and a schema to generate stored procedures that both create a relational database and store the data in the generated tables.
Although it's most commonly used for transforming XML to HTML, XSLT is a general-purpose transformation language useful for generating XML documents from other XML documents or combination of documents, and even for generating other types documents typessuch as database stored procedures. This solution shows you how to generate randomized data using XSLT. A future solution will show you how to use a schema and XSLT to generate relational tables and store the XSLT-generated data in those tables using XML, ADO 2.5 and SQL Server 2000.
Sometimes, you need to be able to generate random data and use the generated document schema to create a set of database tables in which to store the data.
This solution shows you how to generate randomized data using XSLT. A follow-up solution will show you how to use a schema and XSLT to generate relational tables in which to store the generated data.
XSLTNot Just For HTML
Some time ago, I wrote an article for this column that dealt with accessing XML information from databases, and after some discussion with my editors I decided that I wanted to update the piece, looking at the interaction between XML, XSLT and databases with the release of ADO 2.5 (and the upcoming ADO.NET), the MSXML3 Parser and SQL Server 2000. Over the course of the next few months, I'll have a series of different articles focused on these topics, with this being the first.
I used to play a number of role-playing games, and a brother who works for Wizards of the Coast guarantees that I still periodically get to try my hand at adventure scenarios, even if it's far less often than in the past. The facet of game playing that I found most fascinating was creating the game worldgenerating the characters and towns and encounters, and it was this that actually got me into programming in the first place. I initially started to build a database of characters in a SQL Server database, but as I was putting together this article it occurred to me that this was a perfect opportunity to show how you can use XSLT to create such a database in the first place.
You may be wondering about this assertion. Isn't XSLT for transforming XML to HTML? Yes and no. You can certainly do such a transformation, but XSLT can actually handle the transformation from any XML source to any other XML outputeven if that source doesn't necessarily have any direct bearing on the order or presentation of the output. In the case of the character generator, the source XML document contains a set of parameters that describes the values for given optionsa character's gender, species (it is a fantasy RPG), vocation, name, and so forth.
The XSLT generateCharacters.xsl (see Listing 3) uses this document along with a random number generator to create an arbitrary number of characters where the values are constrained to those in the worldData.xml file (see Listing 1). While a few specific categories must be known in order to describe their rules, the properties that define the characters ('strength', 'memory', 'presence', and so forth) are actually contained in the worldData document, along with the ways that the primary characteristics ('species', 'gender', 'vocation', etc.) affect these secondary properties.