Monday, September 22, 2008

Oracle and XML : Random Nodes

So I solved my "scoring XML" problem from a previous post using XSL stored in Oracle.  A simple XMLTransform function later and all of my profiles now get a numeric value. Awesome.

I thought I'd push my luck.  I want to put in a hint system so, for instance, if the user has not filled out the work experience section, he might get a message saying "You can get 10 more points by telling us about jobs you've had."    I could hardwire the heck out of it, but that's no fun.  What I'd like to do is write out a bunch of "hint" elements to the result of the transform, one or more for each section that's not filled out, and then let the interface pick a random one to use.  So if my result said something like:

<ProfileScore>

<Score>10</Score>

<Hints>

<Hint>Tell us about some jobs</Hint>

<Hint>Take up a hobby</Hint>

<Hint>Get some friends to sign up</Hint>

</Hints>

</ProfileScore>

then I'm looking for a way, using Oracle's XML/XSL functions, to return one of those Hint elements.

It's apparently much harder than it looks. :(  The closest I found was something that said I could use "//Hints/Hint[random()]", but Oracle barfs on that syntax.  Doesn't like 'random'.

Everything I googled on the subject comes back with stuff about how to generate random sequences.  I need to select a random value.  One option is to emit each Hint element with some sort of random identifier, and then when it comes time to pick one, just pick a constant.  But that's a pain in the neck as well, since I need to be guaranteed I'll get one and only one result, I would have to make sure (without knowing how many Hints I'm to have) that I have adequately spanned the 1-n space just right with my random numbers.

For right now we're leaving the hints off.  But it bothers me that something that seems simple -- "Get me one of my random children", does not have an obvious solution.

No comments: