Subjects
Home
Xalan extension functions
Fomatting question serializing DOM with pretty print
xalan with pull parser
Cannot find the declaration
Apache Xalan drop support to run on JRE 1 1 x
Why does Doctype change processing of a document
Node set to XML string via Java extensions in Xalan J: possible?
Templates/Transformers + thread safety???
Problem evaluating xpath with muliple prefix with different namespace
remove an arbitrary attribute from xsl output
Xalan3 XSLT 2 0 XPath 2 0 support?
Problem using compiled translets with Xalan !!
Xalan and jstl 1 1 problem with transform tag
NullPointer in DOM2DTM getLocalName
URIResolvers base parameter with xsltc and cascaded imports
Performance problem for Xalan J on intel dual core
Standard libraries in JAXP?
Serializing a DOM tree to XML file, customize entities replacement
Library Conflict Involving BCEL Library
A question on how users are using <xsl:message >
Kevin Cormier as a new Apache Xalan J committer
Struggling to iterate over tokenized string
Xalan count() trouble
Problem with recursive xpath
Error when switching to java 1 5
document( ' ')
Problem with Xalan2 7 0 transformation
cr/lf options
entity encoded XML
can xalan transform 2 xml using one xslt?
Xalan J JIRA defect review Monday October 16, 2006 from 2:00 to 3:30 pm ED
xsl transform with cdata section elements
xslt parameters not expanded
Weird behavior of XPath evaluate()
How to avoid <xsl:message > instruction prints stylesheet file informations ?
Cannot find SimpleTransform subdirectory after installing Xalan J
recover from document not found exceptions
jdk1 5 and Xalan jar differences?
Performance Issue
Error/Bug adding floating point numbers
XPathAPI: eval exp using nodes with default namespace
modifying xalan to output invalid XML
NullPointerException
mege two separate xml nodes into one
Is this a XALAN document identification bug?
is StylesheetRoot really java io Serializable ?
transform() fails for DOMSource but succeeds for StreamSource
Thoughts on Transformer parameter passing
HELP, Xalan and jstl 1 1 problem with transformer
Problem with XPath namespace axis?
string utils:replace deleting search string if replacement string is an HTML
help with enumeration values pls
xalan 2 5 1 vs 2 7 performance question
How to insert/update in XML document
HTML Serialization and Handling of Ampersands in HREF Attributes
XHTML link tag stripping
SystemId Unknown; Line #24; Column #49; java lang NullPointerException
xpath text() help
Apostrophe problem with xalan 2 7 0
How to set variables in XML document?
Links
Home
Oracle database error code ...
 
Search:  
Power your search with and, or, +, -, or "some phrase" operators.
Namespace declarations are lost on transformation to DOMResult

Namespace declarations are lost on transformation to DOMResult

2007-04-27       - By Kevin Cormier
Reply:     1     2  

Hi Christian,

This looks like a bug to me.  Note that your test program works for all
sources except StAXSource if you use XSLTC instead of the interpretive
processor (org.apache.xalan.xsltc.trax.TransformerFactoryImpl rather than
org.apache.xalan.processor.TransformerFactoryImpl).

Could you please open a bug in JIRA?
http://issues.apache.org/jira/browse/XALANJ

Thanks,

Kevin Cormier
Software Developer, XSLT Development
IBM Toronto Lab, D1-435
E-mail:  kcormier@(protected)



            Christian Koppen
            <christian.koppen
            @(protected)                                          To
            e>                        xalan-j-users@(protected)
                                                                       cc
            04/27/2007 10:08
            AM                                                    Subject
                                      Namespace declarations are lost on
                                      transformation to DOMResult










I have a problem with Xalan 2.7. Have a look at the following snippet:

<a:a1 xmlns:a="http://a"
      xmlns:b="http://b"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <a:a2 xsi:type="b:B"/>
</a:a1>

As you can see, the value of the attribute 'xsi:type' contains a namespace
prefix, which is declared in the root element.


When I read this document as a StreamSource or SAXSource and transform it
with Xalan into a DOMResult, the namespaces are invalid because the
namespace declaration 'xmlns:b="http://b"' is lost:

<a:a1 xmlns:a="http://a">
  <a:a2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:type="b:B"/>
</a:a1>


When I try to read the document as a DOMSource and try to transform it into

a DOMResult, an exception occurs ('NAMESPACE_ERR: An attempt is made to
create or change an object in a way which is incorrect with regard to
namespaces.').

I attached the example code and output.

Can somebody tell me if I'm doing something wrong,
or if this is a bug in Xalan?

Thanks in advance,
Christian
class javax.xml.transform.stream.StreamSource
Parsed only          : <a:a1 xmlns:a="http://a" xmlns:b="http://b"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><a:a2
xsi:type="b:B"/></a:a1>
Null-Transform to DOM: <a:a1 xmlns:a="http://a"><a:a2 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:type="b:B"/></a:a1>

class javax.xml.transform.sax.SAXSource
Parsed only          : <a:a1 xmlns:a="http://a" xmlns:b="http://b"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><a:a2
xsi:type="b:B"/></a:a1>
Null-Transform to DOM: <a:a1 xmlns:a="http://a"><a:a2 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xsi:type="b:B"/></a:a1>

class javax.xml.transform.dom.DOMSource
Parsed only          : <a:a1 xmlns:a="http://a" xmlns:b="http://b"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><a:a2
xsi:type="b:B"/></a:a1>
org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or
change an object in a way which is incorrect with regard to namespaces.
            at
com.sun.org.apache.xerces.internal.dom.ElementNSImpl.setName(ElementNSImpl.java
:149)

            at
com.sun.org.apache.xerces.internal.dom.ElementNSImpl.<init>(ElementNSImpl.java
:80)

            at
com.sun.org.apache.xerces.internal.dom.CoreDocumentImpl.createElementNS
(CoreDocumentImpl.java:2084)

            at
org.apache.xml.utils.DOMBuilder.startElement(DOMBuilder.java:322)
            at
org.apache.xalan.transformer.TransformerIdentityImpl.startElement
(TransformerIdentityImpl.java:1072)

            at
org.apache.xml.serializer.TreeWalker.startNode(TreeWalker.java:357)
            at
org.apache.xml.serializer.TreeWalker.traverse(TreeWalker.java:143)
            at
org.apache.xalan.transformer.TransformerIdentityImpl.transform
(TransformerIdentityImpl.java:389)

            at TransformerTest.transform(TransformerTest.java:71)
            at TransformerTest.main(TransformerTest.java:35)

class javax.xml.transform.stax.StAXSource
javax.xml.transform.TransformerException: Umsetzen einer Quelle des Typs
javax.xml.transform.stax.StAXSource ist nicht m�glich.
            at
org.apache.xalan.transformer.TransformerIdentityImpl.transform
(TransformerIdentityImpl.java:418)

            at TransformerTest.transform(TransformerTest.java:66)
            at TransformerTest.main(TransformerTest.java:35)
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.ParameterizedType;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.InputSource;

public class TransformerTest {

 private static final String INPUT
 = "<a:a1 xmlns:a='http://a'"
      + " xmlns:b='http://b' "
      + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"
   + "<a:a2 xsi:type='b:B' /></a:a1>";



 public static void main(String[] args)  {
   Class[] sourceTypes = new Class[] { StreamSource.class,
SAXSource.class, DOMSource.class, StAXSource.class };
   for (Class<? extends Source> c : sourceTypes) {
     try {
       transform(SourceProvider.getProviderFor(c, INPUT));
       System.out.println();
     } catch (Exception e) {
       e.printStackTrace();
     }
   }

 }

 /**
  * @(protected) p
  * @(protected) TransformerConfigurationException
  * @(protected) TransformerException
  * @(protected) Exception
  */
 private static void transform(SourceProvider<?> p) throws
TransformerConfigurationException, TransformerException, Exception {
   StringWriter target;
   Transformer t;
   t = new
org.apache.xalan.processor.TransformerFactoryImpl().newTransformer();
//    t = new
com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl()
.newTransformer();

//    t = new net.sf.saxon.TransformerFactoryImpl().newTransformer();
   t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//    System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
//        "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

   System.out.println(((ParameterizedType)
p.getClass().getGenericSuperclass())
                      .getActualTypeArguments()[0]);


   /* Parse input document and print the result */
   target = new StringWriter();
   t.transform(p.newSource(), new StreamResult(target));
   System.out.println("Parsed only          : " + target.toString());

   /* Parse input document, transform it to DOM and print the result */
   DOMResult result = new DOMResult();
   t.transform(p.newSource(), result);
   DOMSource ds = new DOMSource(result.getNode());
   target = new StringWriter();
   t.transform(ds, new StreamResult(target));
   System.out.println("Null-Transform to DOM: " + target.toString());


 }

 private static abstract class SourceProvider<T extends Source> {
   protected final String xml;
   protected SourceProvider(String xml) {
     this.xml = xml;
   }
   public abstract T newSource() throws Exception;
   public static <T extends Source> SourceProvider<T>
           getProviderFor(Class<T> typ, String xmlContent) {
     SourceProvider<?> result = null;
     if (typ != null) {
       if (typ.equals(DOMSource.class))    result = new
DOMSourceProvider(xmlContent);
       if (typ.equals(SAXSource.class))    result = new
SAXSourceProvider(xmlContent);
       if (typ.equals(StreamSource.class)) result = new
StreamSourceProvider(xmlContent);
       if (typ.equals(StAXSource.class))   result = new
StAXSourceProvider(xmlContent);
     }
     return (SourceProvider<T>) result;
   }
 }

 private static final class DOMSourceProvider extends
SourceProvider<DOMSource> {
   protected DOMSourceProvider(String xml) { super(xml); }
   @(protected)
   public  DOMSource newSource() throws Exception {
     return new DOMSource(
         DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
             new InputSource(new StringReader(xml))));
   }
 }

 private static final class SAXSourceProvider extends
SourceProvider<SAXSource> {
   protected SAXSourceProvider(String xml) { super(xml); }
   @(protected)
   public  SAXSource newSource() throws Exception {
     return new SAXSource(new InputSource(new StringReader(xml)));
   }
 }

 private static final class StreamSourceProvider extends
SourceProvider<StreamSource> {
   protected StreamSourceProvider(String xml) { super(xml); }
   @(protected)
   public StreamSource newSource() throws Exception {
     return new StreamSource(new StringReader(xml));
   }
 }

 private static final class StAXSourceProvider extends
SourceProvider<StAXSource> {
   protected StAXSourceProvider(String xml) { super(xml); }
   @(protected)
   public StAXSource newSource() throws Exception {
     return new
StAXSource(XMLInputFactory.newInstance().createXMLStreamReader(new
StringReader(xml)));
   }
 }

}