Search code examples
xmlxsltjdom-2

How to apply xml-stylesheet to JDOM2 Document


Suppose you have built your XML document using JDOM2 library. What is the API for adding xml-stylesheet to get something like:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
  <node>
...
</root>

Solution

  • I was fishing around for a while before I found the proper method call. So here it is in case someone is looking as well:

    Document doc = ...
    
    Map<String, String> m = new HashMap<String, String>(); 
    m.put("href", "test.xsl"); 
    m.put("type", "text/xsl"); 
    doc.addContent(0, new ProcessingInstruction("xml-stylesheet", m)