You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Jo...@oti.com on 2002/03/18 20:07:53 UTC

RE: TransformerException using DOMSource

Thanks, this works when I create the DOM from a file.  However, when I
create the DOM in memory the same problem occurs (
javax.xml.transform.TransformerException: stylesheet requires attribute:
version).  The workaround is to create the DOM in memory, then write it to
a file and load it back in memory.  This shouldn't be necessary though.
There must be something else simple that I'm missing.  I've included an
example that demonstrates the problem and the workaround.  In the example,
if the static boolean class variable readDocumentsFromFile is true, the DOM
is written to a file and then parsed back into a DOM.  If
readDocumentsFromFile is false it just creates the DOM in memory (omits the
write to file then parse step).  Here it is:

package org.eclipse.xslt.experimentB;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;


public class Main {
      public static boolean readDocumentsFromFile = true;
      public static File tempFile = new File("c:\\temp\\temp001.txt");
      /**
       * Performs a simple xslt transformation.
       */
      public static void main(String[] args){
            try {
                  Document xsl = buildXSLTDocument();
                  DOMSource xslSource = new DOMSource(xsl);
                  printDocument(xsl, System.out);
                  Document input = buildInputDocument();
                  DOMSource inputSource = new DOMSource(input);
                  printDocument(input, System.out);
                  DOMResult output = new DOMResult();
                  TransformerFactory tFactory =
TransformerFactory.newInstance();
                  Transformer transformer =
tFactory.newTransformer(xslSource);
                  transformer.transform(inputSource, output);
                  printDocument((Document)output.getNode(), System.out);
            } catch(Exception e){
                  e.printStackTrace();
            }
      }
      /**
       * Creates the following document:
       *
       * <?xml version="1.0" encoding="UTF-8"?>
       * <xsl:stylesheet version="1.0" xmlns:xsl
="http://www.w3.org/1999/XSL/Transform">
       *     <xsl:output method="xml"/>
       *     <xsl:template match="/">
       *         <DstBusinessObjectA>
       *             <DstField1>
       *                 <xsl:value-of select
="SrcBusinessObjectA/SrcField1/text()"/>
       *             </DstField1>
       *             <DstField2>
       *                 <xsl:value-of select
="SrcBusinessObjectA/SrcField2/text()"/>
       *             </DstField2>
       *         </DstBusinessObjectA>
       *     </xsl:template>
       * </xsl:stylesheet>
       */
      public static Document buildXSLTDocument() throws Exception {
            DocumentBuilderFactory df = DocumentBuilderFactory.newInstance
();
            df.setNamespaceAware(true);
            DocumentBuilder db = df.newDocumentBuilder();
            Document document = db.newDocument();

            Element stylesheet = document.createElement("xsl:stylesheet");
            stylesheet.setAttribute("version", "1.0");
            stylesheet.setAttribute("xmlns:xsl",
"http://www.w3.org/1999/XSL/Transform");
            document.appendChild(stylesheet);

            Element template = document.createElement("xsl:template");
            template.setAttribute("match", "/");
            stylesheet.appendChild(template);

            Element root = document.createElement("DstBusinessObjectA");
            template.appendChild(root);

            Element field1 = document.createElement("DstField1");
            root.appendChild(field1);
            Element value_of1 = document.createElement("xsl:value-of");
            value_of1.setAttribute("select",
"SrcBusinessObjectA/SrcField1/text()");
            field1.appendChild(value_of1);
            root.appendChild(field1);

            Element field2 = document.createElement("DstField2");
            root.appendChild(field2);
            Element value_of2 = document.createElement("xsl:value-of");
            value_of2.setAttribute("select",
"SrcBusinessObjectA/SrcField2/text()");
            field2.appendChild(value_of2);
            root.appendChild(field2);

            if(readDocumentsFromFile){
                  return convertToFileAndBack(document);
            }

            return document;
      }
      /**
       * Creates the following document:
       *
       * <?xml version="1.0" encoding="UTF-8"?>
       * <SrcBusinessObjectA>
       *     <SrcField1>string1</SrcField1>
       *     <SrcField2>string2</SrcField2>
       *  </SrcBusinessObjectA>
       */
      public static Document buildInputDocument() throws Exception {
            DocumentBuilderFactory df = DocumentBuilderFactory.newInstance
();
            df.setNamespaceAware(true);
            DocumentBuilder db = df.newDocumentBuilder();
            Document document = db.newDocument();

            Element root = document.createElement("SrcBusinessObjectA");
            document.appendChild(root);

            Element child1 = document.createElement("SrcField1");
            Text text1 = document.createTextNode("string1");
            child1.appendChild(text1);
            root.appendChild(child1);

            Element child2 = document.createElement("SrcField2");
            Text text2 = document.createTextNode("string2");
            child2.appendChild(text2);
            root.appendChild(child2);

            if(readDocumentsFromFile){
                  return convertToFileAndBack(document);
            }

            return document;
      }
      public static Document convertToFileAndBack(Document document) throws
Exception {
            OutputStream os = new FileOutputStream(tempFile);
            printDocument(document, os);
            os.close();

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance
();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document newDocument = db.parse(tempFile);

            return newDocument;
      }
      /**
       * Prints the given document to given output stream.
       */
      public static void printDocument(Document document, OutputStream os)
throws IOException {
            XMLSerializer serializer = new XMLSerializer();
            OutputFormat outputFormat = new OutputFormat();
            outputFormat.setIndenting(true);
            serializer.setOutputFormat(outputFormat);
            serializer.setOutputCharStream(new OutputStreamWriter(os));
            serializer.serialize(document);
      }
}


Jonathan



                                                                                                                                  
                      "Gary L Peskin"                                                                                             
                      <garyp@firstech.         To:      <xa...@xml.apache.org>                                                
                      com>                     cc:                                                                                
                                               Subject: RE: TransformerException using DOMSource                                  
                      02/28/02 04:59                                                                                              
                      PM                                                                                                          
                      Please respond                                                                                              
                      to xalan-dev                                                                                                
                                                                                                                                  
                                                                                                                                  



Try adding

  dfactory.setNamespaceAware(true);

Before your newDocumentBuilder() call.

HTH,
Gary

> -----Original Message-----
> From: Jonathan_Pico@oti.com [mailto:Jonathan_Pico@oti.com]
> Sent: Thursday, February 28, 2002 12:49 PM
> To: xalan-dev@xml.apache.org
> Subject: TransformerException using DOMSource
>
>
> I get an exception:
>
>       javax.xml.transform.TransformerException: stylesheet requires
> attribute: version
>
> when I execute xalan with a DOMSource like so:
>
> public static void main(String[] args){
>     DocumentBuilderFactory dfactory =
> DocumentBuilderFactory.newInstance();
>     DocumentBuilder dBuilder = dfactory.newDocumentBuilder();
>     Source xsl = new DOMSource(dBuilder.parse(new
> File(workingDir, "input.xsl")));
>     //Source xsl = new StreamSource(new File(workingDir,
> "input.xsl"));
>     Source input = new DOMSource(dBuilder.parse(new
> File(workingDir, "input.xml")));
>     StreamResult output = new StreamResult(new
> File(workingDir, "output.xml"));
>     TransformerFactory tFactory = TransformerFactory.newInstance();
>     Transformer transformer = tFactory.newTransformer(xsl);
>     transformer.transform(input, output);
> }
>
> but when I execute xalan with a StreamSource it works fine.
> Here is my input.xsl file:
>
> <?xml version="1.0"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:lxslt="http://xml.apache.org/xslt"
> xmlns:customMap="CustomSplit"
> extension-element-prefixes="customMap" version="1.0">
>     <lxslt:component prefix="customMap"
> elements="customSplit1 customSplit2 customSplit3">
>         <lxslt:script lang="javaclass" src
> ="xalan://org.eclipse.xml.xslt.experiment7.CustomSplit"/>
>     </lxslt:component>
>
>     <xsl:template match="/">
>         <DstRoot>
>             <DstElement1><customMap:customSplit1/></DstElement1>
>             <DstElement2><customMap:customSplit2/></DstElement2>
>             <DstElement3><customMap:customSplit3/></DstElement3>
>         </DstRoot>
>     </xsl:template>
> </xsl:stylesheet>
>
> Any ideas why this does not work?
>
> I'm using xalan-j_2_3_1on windows NT.
>
>