This discussion is archived
4 Replies Latest reply: Sep 27, 2012 4:00 PM by DrClap RSS

Setting a stylesheet ref when outputting XML using SAXTransformerFactory

659725 Newbie
Currently Being Moderated
Hello, I am using SAXTransformerFactory to output an XML file but can't figure out how to output the stylesheet setting, for example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ectd:ectd SYSTEM "util/dtd/ich-ectd-3-2.dtd">
<?xml-stylesheet type="text/xsl" href="util/style/ectd-2-0.xsl"?>

I am using the following code to get the first two lines, I just can't see how to output the third line above.

SAXTransformerFactory mTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = mTransformerFactory.newTransformerHandler();
Transformer mTransformer = handler.getTransformer();

mTransformer.setOutputProperty(OutputKeys.VERSION, "1.0");
mTransformer.setOutputProperty(OutputKeys.ENCODING, IBF_UtilXML.XML_ENCODING_UTF8);
mTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "util/dtd/ich-ectd-3-2.dtd");

I believe I found an example using a DOM approach but I would much prefer using SAX. Any assistance on outputting this line would be most appreciated:
<?xml-stylesheet type="text/xsl" href="util/style/ectd-2-0.xsl"?>

Thanks
  • 1. Re: Setting a stylesheet ref when outputting XML using SAXTransformerFactory
    DrClap Expert
    Currently Being Moderated
    I was going to suggest that your transformation should just output that processing-instruction as the first thing it does.

    But then I noticed you were just doing an identity transformation, so you don't have a transformation. So, couldn't you take the identity transformation and modify it to output that PI as the first line of the output? Then use that to transform the data.
  • 2. Re: Setting a stylesheet ref when outputting XML using SAXTransformerFactory
    659725 Newbie
    Currently Being Moderated
    DrClap, thanks for the reply. I believe that is the approach I saw used when constructing a DOM to hold the output. You create the structure first then insert the process instruction where needed. Unfortunately, that means building and maintaining the entire structure in memory. Because of the potential size of the overall output I'm trying to go with the SAX approach where I'm actually outputting the data line by line. The SAX transform just helps with the formatting and encoding.

    However, if the DOM concept is the only way then I may have to switch my approach. But i though I would ask the question first to see what results I get or to find out if it is even supported at all.
  • 3. Re: Setting a stylesheet ref when outputting XML using SAXTransformerFactory
    DrClap Expert
    Currently Being Moderated
    Sorry, I don't understand that. All I suggested was for your SAX transformer to use an actual transformation instead of the default identity transformation. I don't think I suggested you should change anything to DOM at all.
  • 4. Re: Setting a stylesheet ref when outputting XML using SAXTransformerFactory
    659725 Newbie
    Currently Being Moderated
    Sorry, I missunderstood. Since I saw an example using an insertion of an instruction before an element that was based on a DOM I thought that was what you were referring to. Since using SAX outputs each line one at a time, I couldn't see how an insertion could work. At least that appears to be how it works to me.

    I guess i don't understand the reference to an "actual transformation" vs. the "default identify transformation". I've basically used the standard default SAX and DOM factories to read and ouput XML but I'm not versed on other options. Could you provide a link or more information on how I could use a different transformer with SAX to get the desired results?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points