You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by John Gouveia <jg...@camara.co.za> on 2001/05/17 18:40:58 UTC

Processing an com.sun.xml.tree.XmlDocument with Xalan

Hi 

I am new to XML. I am having difficulty accessing the Xalan mailing list and I dont know where else to look. I am sure this topic has been brought up before.

Could anyone please help me with the following code. I have parsed a String into  com.sun.xml.tree.XmlDocument;.

I am trying to process this document with Xalan version 1.2.2

When I process the document the following error occurs:

" XSL Error: SAX Exception   Warning: can't output text before document element! Ignoring..."

What am I doing wrong? Would I need to serilalize the output to display the result Node as html?

This is the code I have been able to write:

import org.apache.xalan.xslt.XSLTEngineImpl;
import org.apache.xalan.xslt.XSLTProcessor;
import org.apache.xalan.xslt.XSLTInputSource;
import org.apache.xalan.xslt.XSLTResultTarget;
import org.apache.xalan.xslt.XSLTProcessorFactory;
import org.apache.xalan.xpath.xml.JaxpLiaison;
import org.apache.xalan.xpath.xdom.*;
import org.apache.xalan.xpath.xml.XMLParserLiaisonDefault;
import org.apache.xalan.xpath.xml.JaxpLiaison;
import org.apache.xml.serialize.*;
import com.sun.xml.*;
import com.sun.xml.tree.XmlDocument;


public class DisplayItemsBean extends Object {

  
  public XmlDocument createDocument(String xml){


 XmlDocument xmlDoc = new XmlDocument();

   try
    {
     ByteArrayInputStream in = new ByteArrayInputStream(xml.getBytes());
     xmlDoc = XmlDocument.createXmlDocument (in, false);

     }
    catch (SAXParseException e)
     {
    //Handle Exception
      }
     catch (SAXException e)
     {
    //Handle Exception
     }
     catch (java.io.IOException e)
     {
     //Handle Exception
      }


  return xmlDoc;

  }

   public void process(XmlDocument inDoc) throws Exception {
   XmlDocument doc = new XmlDocument();
   Node outputNode  =  (Node)doc;
   XSLTResultTarget rt = new XSLTResultTarget (outputNode);

    try{
       XSLTInputSource xsltIn = new XSLTInputSource();
       xsltIn.setSystemId("xsl address");

       XSLTInputSource xmlIn = new XSLTInputSource(inDoc.getDocumentElement());

       XSLTProcessor processor = XSLTProcessorFactory.getProcessor(new JaxpLiaison());


       processor.process(xmlIn,
       xsltIn,
       rt);

       }

Node output = rt.getNode();

  catch(Exception e){
       System.out.println(e.getMessage());
       }
 }





Both  the XML and XSL have been successfully  processed before

my XML : 
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<orderProviderList><startDate>15-Apr-2001</startDate><endDate>25-Apr-2001</endDate><order><order_no>10002</order_no>
<buyer_name>Heyster</buyer_name><order_date>16/04/2001 08:05</order_date><order_total>R0.00</order_total>
</order><order><order_no>10003</order_no><buyer_name>David</buyer_name><order_date>16/04/2001 19:58</order_date>
<order_total>R597.61</order_total></order><order>

my XSL:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<output>
<xsl:template match="/">
 <tr>
  <td colspan="4" width="100%" align="center" class="cheading">
  Order Table
  </td>
 </tr>
 <tr bgcolor="#e9e9df">
  <b>
  <td class="text">
   Order No
  </td>
  <td class="text">
   Customer Name
  </td>
  <td class="text">
   Order Date
  </td>
  <td class="text">
   Total
  </td>
  </b>
 </tr>
 <xsl:for-each select="orderProviderList/order">
 <tr>
  <td align="left" valign="top">
   <u/>
    <a>
     <xsl:attribute name="href">
      orderProducts.jsp?orderNo=<xsl:value-of select="order_no"/>&amp;startDate=<xsl:value-of select="//startDate"/>&amp;endDate=<xsl:value-of select="//endDate"/>&amp;cursor=<xsl:value-of select="//cursor"/>
     </xsl:attribute>
      <xsl:value-of select="order_no"/>
    </a>
  </td>
  <td align="left" valign="top" class="text">
   <xsl:value-of select="buyer_name"/>
  </td>
  <td align="left" valign="top" class="text">
   <xsl:value-of select="order_date"/>
  </td>
  <td align="right" valign="top" class="text">
   <xsl:value-of select="order_total"/>
  </td>
 </tr>
 </xsl:for-each>
 </xsl:template>
 </output>
 </xsl:stylesheet>

Thanks


Re: Processing an com.sun.xml.tree.XmlDocument with Xalan

Posted by Gary L Peskin <ga...@firstech.com>.
John --

Hi!  I'm not sure why you're getting this particular error message. 
However, you should remove the <output> </output> tags from where they
are and place them inside the xsl:template, which is the correct
location.  An xsl:template element is not allowed inside a top-level
literal tag.

Also, I'd convert as fast as possible to XalanJ2 as XalanJ1 is really no
longer in play or well supported.

Also, to see more of what's going on, try transforming to a file or
System.out rather than a DOM node to aid in debugging.  This will show
you what the text is that's being output prior to the document element.

HTH,
Gary

> John Gouveia wrote:
> 
> 
> Hi
> 
> I am new to XML. I am having difficulty accessing the Xalan mailing
> list and I dont know where else to look. I am sure this topic has been
> brought up before.
> 
> Could anyone please help me with the following code. I have parsed a
> String into  com.sun.xml.tree.XmlDocument;.
> 
> I am trying to process this document with Xalan version 1.2.2
> 
> When I process the document the following error occurs:
> 
> " XSL Error: SAX Exception   Warning: can't output text before
> document element! Ignoring..."
> 
> What am I doing wrong? Would I need to serilalize the output to
> display the result Node as html?
> 
> This is the code I have been able to write:
> 
> import org.apache.xalan.xslt.XSLTEngineImpl;
> import org.apache.xalan.xslt.XSLTProcessor;
> import org.apache.xalan.xslt.XSLTInputSource;
> import org.apache.xalan.xslt.XSLTResultTarget;
> import org.apache.xalan.xslt.XSLTProcessorFactory;
> import org.apache.xalan.xpath.xml.JaxpLiaison;
> import org.apache.xalan.xpath.xdom.*;
> import org.apache.xalan.xpath.xml.XMLParserLiaisonDefault;
> import org.apache.xalan.xpath.xml.JaxpLiaison;
> import org.apache.xml.serialize.*;
> import com.sun.xml.*;
> import com.sun.xml.tree.XmlDocument;
> 
> 
> public class DisplayItemsBean extends Object {
> 
> 
>   public XmlDocument createDocument(String xml){
> 
> 
>  XmlDocument xmlDoc = new XmlDocument();
> 
>    try
>     {
>      ByteArrayInputStream in = new
> ByteArrayInputStream(xml.getBytes());
>      xmlDoc = XmlDocument.createXmlDocument (in, false);
> 
>      }
>     catch (SAXParseException e)
>      {
>     //Handle Exception
>       }
>      catch (SAXException e)
>      {
>     //Handle Exception
>      }
>      catch (java.io.IOException e)
>      {
>      //Handle Exception
>       }
> 
> 
>   return xmlDoc;
> 
>   }
> 
>    public void process(XmlDocument inDoc) throws Exception {
>    XmlDocument doc = new XmlDocument();
>    Node outputNode  =  (Node)doc;
>    XSLTResultTarget rt = new XSLTResultTarget (outputNode);
> 
>     try{
>        XSLTInputSource xsltIn = new XSLTInputSource();
>        xsltIn.setSystemId("xsl address");
>        XSLTInputSource xmlIn = new
> XSLTInputSource(inDoc.getDocumentElement());
> 
>        XSLTProcessor processor = XSLTProcessorFactory.getProcessor(new
> JaxpLiaison());
> 
> 
>        processor.process(xmlIn,
>        xsltIn,
>        rt);
> 
>        }
> 
> Node output = rt.getNode();
> 
>   catch(Exception e){
>        System.out.println(e.getMessage());
>        }
>  }
> 
> 
> 
> 
> Both  the XML and XSL have been successfully processed before
> 
> my XML :
> <?xml version=\"1.0\" encoding=\"UTF-8\"?>
> <orderProviderList><startDate>15-Apr-2001</startDate><endDate>25-Apr-2001</endDate><order><order_no>10002</order_no>
> <buyer_name>Heyster</buyer_name><order_date>16/04/2001
> 08:05</order_date><order_total>R0.00</order_total>
> </order><order><order_no>10003</order_no><buyer_name>David</buyer_name><order_date>16/04/2001
> 19:58</order_date>
> <order_total>R597.61</order_total></order><order>
> 
> my XSL:
> <?xml version="1.0"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
> <output>
> <xsl:template match="/">
>  <tr>
>   <td colspan="4" width="100%" align="center" class="cheading">
>   Order Table
>   </td>
>  </tr>
>  <tr bgcolor="#e9e9df">
>   <b>
>   <td class="text">
>    Order No
>   </td>
>   <td class="text">
>    Customer Name
>   </td>
>   <td class="text">
>    Order Date
>   </td>
>   <td class="text">
>    Total
>   </td>
>   </b>
>  </tr>
>  <xsl:for-each select="orderProviderList/order">
>  <tr>
>   <td align="left" valign="top">
>    <u/>
>     <a>
>      <xsl:attribute name="href">
>       orderProducts.jsp?orderNo=<xsl:value-of
> select="order_no"/>&amp;startDate=<xsl:value-of
> select="//startDate"/>&amp;endDate=<xsl:value-of
> select="//endDate"/>&amp;cursor=<xsl:value-of select="//cursor"/>
>      </xsl:attribute>
>       <xsl:value-of select="order_no"/>
>     </a>
>   </td>
>   <td align="left" valign="top" class="text">
>    <xsl:value-of select="buyer_name"/>
>   </td>
>   <td align="left" valign="top" class="text">
>    <xsl:value-of select="order_date"/>
>   </td>
>   <td align="right" valign="top" class="text">
>    <xsl:value-of select="order_total"/>
>   </td>
>  </tr>
>  </xsl:for-each>
>  </xsl:template>
>  </output>
>  </xsl:stylesheet>
> 
> Thanks
>