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-2 : Page 2


advertisement
Controlling XSLT Output
This is something of a mixed rule set, where the structure of the output is partially determined by the initial data and partially by the transformation. It is possible to change this into a format where the structure is completely determined by the source document and the transformation acts only to implement the rules, but such a transformation is also considerably more difficult to write well in the general case. Thus, the world document specifies the range of possibilities for various primary attributes. In the case of gender and species, for instance, each particular instance of an attribute has a range of probabilities (min and max) between 1 and 100. Thus, if you rolled a hundred-sided dice (and yes, there are such things), a score between 1 and 75 would indicate that the character was human, between 76 and 80 the character was a half-elf, and so forth.

This range of probabilities provides a way of creating a non-equal distribution that still accurately represents a population, a trait that is especially evident in the Level element, which can take a value of 1 to 10 but does so in a very non-linear way. On the other hand, a few qualities such as the Order or Temperament are given random values based upon the number of items in each category. The world document also recognizes two distinctly different modes. The primary mode indicates those properties that can be calculated directly. The property mode, on the other hand, depends upon quantities given in the primary elements. For example, the Strength property is normalized for humans, but an average centaur by dint of its size would have an additional three points of strength added to the baseline score. A number of attributes in the primary elements are recognizable as having the same name as the properties—if a character has a given primary characteristic (species as a centaur, for instance) which has a named property attribute (such as Strength), then this value will be added to the average of the sum of three six sided dice.

The world document would thus likely produce an output similar to Listing 2. Programming the Exceptions
A significant amount of programming comes in recognizing common cases and programming for them, and then programming for the exceptions. The file generateCharacters.xsl (see Listing 3) uses this model. It looks first at the generalized case of primary characteristics such as Level or Gender, and then determines secondary properties such as Strength or Stamina, which are dependent upon the primary characteristics. It also includes specialized handlers for Name and LifePoints, because neither one falls into the mode for generating content that all of the rest of the properties do:



Using Custom Namespaces
The generateCharacters.xsl stylesheet (Listing 3) makes extensive use of a custom defined namespace called game:rollDice(), which takes as parameters the number of dice to be rolled and the number of pips (spots) on the dice itself. XSLT contains no randomizing elements, so the stylesheet defines this extension explicitly. If you wanted to take this to a platform other than Microsoft's MSXML3 parser, you could write the extension in Java. The stylesheet works by calculating the two principle sets of information as well as the two exceptions (for creating names from a list, which is dependent upon gender information, and for determining the number of life points that the character has). Once it has this information, the generateCharacter named template outputs the name, simplifies the primary characteristics (which initially contain some attribute information from WorldData.xml for convenience) and sends these to the output stream, and then does the same for the properties.



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