[jdom-interest] setting a stylesheet for an xml document

Nott, Matthias mnott at vignette.com
Tue Jun 11 16:26:56 PDT 2002


Richard,

Correct. In addition, as you are specifically talking about
using jdom, I suspect you have a JDOM document which you're
about to transform. You can apply your style sheet directly
to this document object if you know the location of the xsl
- if,  for example,  you have a JSP which calls, at a given
point, a custom tag which loads an xml tree from a database
(simulated here by hard-coding the result) and wants to ap-
ply an xsl transformation to this result before getting the
result back,  you can pass the xsl  location  as a relative
path  into the tag class and then do what I've outlined be-
low.

HTH,

Matthias

----------------------------------------------------------------

PageContext pc = pageContext;
JspWriter  out = pc.getOut();
String myXML =
  "<object oid=\"4711\" name=\"Java\" url=\"\">" +
    "<object oid=\"4712\" name=\"Thinking Java\" url=\"tjava.html\" />" +
    "<object oid=\"4713\" name=\"Dreaming Java\" url=\"djava.html\" />" +
    "<object oid=\"4714\" name=\"Drinking Java\" url=\"bjava.html\" />" +
  "</object>";

try {
  String       XSL = pc.getServletContext().getRealPath(xsl);
  Document     XML = new SAXBuilder().build(new StringReader(myXML));
  Transformer  trn = TransformerFactory.newInstance()
                     .newTransformer(new StreamSource(XSL));
  StreamResult  tXML = new StreamResult(out);
  trn.transform(new JDOMSource(XML), tXML);
  out.flush();
} catch (org.jdom.JDOMException jde) 
     {throw new JspTagException("JDOM Error: " + jde.getMessage());}
  catch (TransformerException e)
     {throw new JspTagException("Transform Error: " + e.getMessage());}
  catch (java.io.IOException ioe)
     {throw new JspTagException("IO Error: " + ioe.getMessage());}


On Tue, 11 Jun 2002, Richard Backhouse wrote:

> i've seen a similar question in the archives but couldn't find an
answer. i think what i need to does is simple, and i'll probably kick
myself when i find out, but how do i set an xsl stylesheet for a
document using jdom? i.e. 
> after the initial <?xml version="1.0" encoding="UTF-8"?> i need to add
something like <?xml-stylesheet type="text/xsl"
href="aStyleSheet.xsl"?>.
> 
> Thanks,
> 
> Richard
> 



More information about the jdom-interest mailing list