Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Generating Data with XSLT-3 : Page 3


advertisement
Building Loops in XSLT
The generateCharacters (see Listing 3) template calls itself recursively after outputting each character's XML, incrementing a counter by 1 for each iteration. This continues until the number of characters reaches a certain number, at which point it stops—the equivalent of an indexed for loop in a procedural language. By default, the value is 20, the number in the worldData.xml property , but it is set up as a global parameter and can hence be changed through code.

The property items also perform a potentially sophisticated bit of processing themselves. For example, the strength item in the WorldData.xml file has the following structure:

This tells the generateCharacters.xsl file (Listing 3) to generate a number as follows:

However, there are a few points to consider with this:

$randomizers can contain the name of a primary element to use in calculating the number of dice. For example, if randomizers="Level" then the value of the Level property is used to determine how many dice are used.

"primary". One of the most powerful attribute values for the property mode is "primary". It will look at all of the elements that were defined as having mode="primary" and will check to see if an attribute with the same name as the current element exists. If it does, then this and all similar matching element attribute values are summed together. If used as an argument for @min or @max, this will be used to determine the size of the dice. If used for @randomizers, this will determine the number of dice to roll, while if used for @offset the primary sum will be added to the sum of the dice rolls.

Constants. You can use a combination of $min, $max, $offset and $primary to directly add properties together. For instance, setting $randomizers, $min and $max to 0 and $offset to 10 will make the result ten, while setting $min=$max will multiply the values of $min by the $randomizer without any random effects.

Applying the Techniques
I suspect that for most people, the need for a routine to generate game characters is probably not a high priority. However, by separating the transformation mechanism from the intrinsic structure of the output, this same mechanism can be used to generate test data of any sort. For instance, you could create an XML data page that would generate faux employee information that might have the following structure.

The BusCardData.xml, template illustrates how the same basic setup with different elements can generate similarly rich XML structures using generateCharacters.xsl.

<world display="html"> <numCharacters>10</numCharacters> <Titles type="percent" label="Title" mode="primary"> <Title min="1" max="50" Access="0" Salary="40000" StockOptions="1000"> Associate</Title> <Title min="51" max="62" Access="1" Salary="44000" StockOptions="120"> Administrative Assistant</Title> <Title min="63" max="87" Access="1" Salary="52000" StockOptions="1500"> Programmer/Analyst</Title> <Title min="88" max="96" Access="2" Salary="62000" StockOptions="2000"> Manager</Title> <Title min="97" max="100" Access="3" Salary="92000" StockOptions="5000">Executive</Title> </Titles> <Departments mode="primary" type="random" label="Department"> <Department>Accounting</Department> <Department>Research &amp; Development</Department> <Department>Production</Department> <Department>Marketing &amp; Sales</Department> <Department>Distribution</Department> </Departments> <Years type="percent" label="Years" mode="primary"> <Year min="1" max="24">1</Year> <Year min="25" max="60">2</Year> <Year min="61" max="80">3</Year> <Year min="81" max="90">4</Year> <Year min="91" max="94">5</Year> <Year min="95" max="96">6</Year> <Year min="97" max="97">7</Year> <Year min="98" max="98">8</Year> <Year min="99" max="99">9</Year> <Year min="100" max="100">10</Year> </Years> <Genders type="percent" label="Gender" mode="primary"> <gender min="1" max="50">Male</gender> <gender min="51" max="100">Female</gender> </Genders> <Salary mode="property" randomizers="Years" min="1" max="500" offset="primary"/> <StockOptions mode="property" randomizers="Years" min="1" max="primary" offset="0"/> <Access mode="property" randomizers="0" min="0" max="0" offset="primary"/> <names> <gender type="Male"> <first> <name>Thorin</name> <name>Kor</name> <name>Alfrin</name> <name>Meior</name> <name>Kirin</name> <name>Tomas</name> <!-- as per the WorldData.xml file --> </world>

Most important, perhaps, are the lessons that can be gleaned from following common structures and designing in as general a case as possible. The output is clean and simple (and largely one dimensional), which also makes it ideal for depositing in a database.

Next month, I'll show how this XML structure can be entered into a SQL Server database through an XSLT-generated stored procedure, and after that I hope to look at how you can retrieve and interact with the same information using SQL and ADO.



Kurt Cagle is the author or co-author of twelve books and several dozen articles on web technologies, XML and web services. He is the president of Cagle Communications (Olympia, WA), which specializes in the production of training materials for XML and Web Services education. He can be reached at kurt@kurtcagle.net.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date