• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Style sheet using Transform

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the first time I've had to create an xml file using java, so I hope that this is something obvious that I'm missing. I'm creating an xml file using org.w3c.dom.Document and I'm writing the xml document to the file system using javax.xml.transform.TransformerFactory. I'd like to include an xml stylsheet in the xml document, but I'm not sure how to do that. I think I'm looking for something like this: <?xml-stylesheet type="text/xsl" href="./css/myStyle.xsl"?>
My code looks something like this (thanks in advance):

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
}
catch (ParserConfigurationException pce) {
System.err.println(pce);
}
Document doc = db.newDocument();
Element root = doc.createElement("submission");
root.setAttribute("date", getTodaysDate(true));
doc.appendChild(root);
(build more xml here...)
File file = new File("myFile.xml");
Result result = new StreamResult(new FileOutputStream(file.toString()));
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
xformer.transform(source, result);
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never done this, but I would try adding the stylesheet reference as a processing instruction like this.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Griffith:

Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
xformer.transform(source, result);


Hello Frank,
The problem lies with the above three line. To apply a xsl file to your xml file, you need not include the <?xml-stylesheet type="text/xsl" href="./css/myStyle.xsl"?> tag in your xml file.
Use the following code for that -
String xslPath = "css/myStyle.xsl";
TransformerFactory myTF = TransformerFactory.newInstance();

Templates tmplXsl = myTF.newTemplates(new StreamSource(new File(xslPath)));
Transformer xformer = tmplXsl.newTransformer();

xformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
xformer.transform(source, result);
- Ashish Agrawal.
 
Frank Griffith
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashish,
Your solution works great. Thanks!
Lasse, your resolution works as well, however the ProcessingInstruction object adds a name, value pair when I actually need 3 parameters in the tag (xml-stylesheet, type and href).
Thanks again for the help!
Frank
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Griffith:
Lasse, your resolution works as well, however the ProcessingInstruction object adds a name, value pair when I actually need 3 parameters in the tag (xml-stylesheet, type and href).


Yes, you need to provide the full "data section" for the PI as the 2nd argument for createProcessingInstruction(String, String):

PS. these don't have to be "attributes" -- the following is a valid processing instruction as well:
<?newsflash Finland won Russia in ice hockey 30 mins ago?>
 
Frank Griffith
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. I'll give it a try. Thanks again.
 
reply
    Bookmark Topic Watch Topic
  • New Topic