I know that I can create xml files programmatically by using DOM api in java like the following:
DocumentBuilderFactory do
You can create an XSLT the same way you create an XML file, since XSLTs are XML files.
However, if you have to deal with XML/XSLT transformations a lot, Apache Cocoon 3 has a very lightweight XML/XSLT pipeline system to use as a library instead of dealing with all XML dom stuff and XSLT transformations manually.