There is nothing wrong with the script. It is working fine.
Initially I was also facing the same problem.
But the issue is, if you are having spaces in the name of your project then you will face this kind of problem.
Try to generate the epub output after removing the spaces from the project name.
I hope it would resolve your issue.
I don't know if you're still having the problem with the TOC not displaying. I just ran across your post today - November 9, 2010. If you're still having the problem - not getting the TOC in Adobe Digital Editions - the answer may be that you haven't set the XML handler correctly. Make sure the handler is set to XHTML and try to generate the output again. This time, you should get the TOC in Adobe Digital Editions.
Also make sure that the base filename of your XML output matches your project name. For example, if your project is named Acme_Widget_User_Guide.xpj, set the name of your XML output to Acme_Widget_User_Guide.xml. (Do this in the Select Output Folder and File Name field in your XML Output generation wizard window.)