I HATE recursion. Last night, out of pure boredom I started to write a class to generate an XSD from an XML document, which will naturally involve some type of tree-traversal. The base logic was:
level = 0
Start at the root
Are there any children?
Yes - Go to first child, increment level counter, and repeat
No - Add tag to array of tags at level
When done, start with first element, extract attributes, check data type, etc and generate xs:blah tag. Write out to out.xsd.
Recursion and I have never really gotten along, and didn't fair any better here. I either got precisely one tag before the code stopped, or it get running, randomly jumping back the same place and starting over.
I may come back to look at it tomorrow if I get stuck on something at work.
As an aside, this is an absolute must-read for those interested in Rails deployment strategies (I've been told at work that I'm interested. Very interested indeed.).

Leave a comment