I know that I can create xml files programmatically by using DOM api in java like the following:
DocumentBuilderFactory do
DOM is a pretty cumbersome way of creating XML.
There's a far better way - use XSLT.
The more complex the XML, the bigger the win from using XSLT rather than DOM to create it.
There's no reason why you can't use XSLT to create XSLT (there's even a special declaration xsl:namespace-alias to make it a little bit easier - searching for xsl:namespace-alias will show up examples of its use.