2

I have a REST service implemented in GlassFish using Jersey. I have the service working using JSON output, now I would like to provide some human readable output as well. The most important thing missing in the JSON output is I want some fields in the statistics to be transformed to a clickable links, which would display a related statistics (using anoother REST call).

As I want to keep formatting and content separate, I do not want to produce HTML directly. I though using xml + xslt might be a sensible approach. I can use org.w3c.dom to produce a XML document, but I see no way how to attach a xslt to it.

Here is approximately what I do now:

@GET
@Path("history_id")
@Produces("application/xml")
public Document history(@QueryParam("id") String idText) throws ParserConfigurationException
{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element root = doc.createElement("root");
        doc.appendChild(root);
        Element el = doc.createElement(name);
        el.setTextContent(value);
        root.appendChild(el);
        return doc;

I would like following directive to appear in the XML produced:

<?xml-stylesheet type="text/xsl" href="history.xsl"?">

How can I specify a xslt to be attached to the XML? Or is my approach perhaps fundamentally wrong and this is usually done in some other way?

Suma
  • 33,181
  • 16
  • 123
  • 191
  • Just out of interest, why do you want to show the XML in a human readable way? Is it for displaying in part of a webpage (for example as part of an API visualizer, ie [Swagger](http://swagger.wordnik.com/) type approach) or is it for some other reason? – My Head Hurts Dec 03 '12 at 10:10
  • @My Head Hurts I want to produce some statistics, which may be consumed by an application (this I currently do using JSON), but they might be sometimes inspected by a human as well, and for this I would like to add some formatting (most important is to implement clickable links on some fields to display related statistics) as a convenience. – Suma Dec 03 '12 at 10:13
  • @MyHeadHurts It should be possible to provide a XML webpage with a XSLT stylesheet and web browser should be able to do the transformation and formatting on its side, line in http://www.w3schools.com/xml/simplexsl.xml. I understand I could do the transformation on the server, but somehow that feels like an inferior solution. – Suma Dec 03 '12 at 10:27
  • 1
    Have you considered using JAXB to produce both XML and JSON from the same domain objects? Have a look at http://blog.bdoughan.com/2012/11/using-jaxb-with-xslt-to-produce-html.html and http://stackoverflow.com/a/11012294/116509 – artbristol Dec 03 '12 at 10:33
  • @artbristol It seems like a reasonable way for many scenarios, but it does not seem to fit my purpose very well. 1) My output does not correspond directly to the database objects, I output some aggregated stats, not the objects themselves. 2) This seems to produce HTML, not XML. – Suma Dec 03 '12 at 10:42
  • 1
    @MyHeadHurts I am trying to implement the solution in the link provided, but I do not see any XSLT attached to the DOM produced by the transformation. I will study it a bit more to understand it. If that works, it might be an answer. – Suma Dec 03 '12 at 10:44
  • @MyHeadHurts I do not see any xslt being attach in the example. I am not sure I undestand it correctly, but the only output property set is indentation using transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); - am I missing something? – Suma Dec 03 '12 at 10:51

1 Answers1

3

That "directive" is called a "processing instruction" in the XML world. You can create one with the DOM API:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = factory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    doc.appendChild(doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"history.xsl\""));

Then, if you send the XML document to a browser/user agent that supports client-side XSLT it will apply the stylesheet to the XML.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110