You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Ai...@nokia.com on 2000/09/29 16:37:13 UTC

Namespace problem after Processing DOM input with Xalan.

Hi
I wonder if you could help me,  I have a problem with loosing namespace
information from the DOM structure after XSL processing. I've checked the
FAQ's and usage patterns but can't find an clues.   I am using Xerces-j
1.1.3 and Xalan-j 1.2.D02.

In outline the situation is this, my code expects input from various content
providing modules in the form of an  'org.w3c.dom.Document' object. This
content is then formatted by passing it through one or more modules that
perform different sorts of formatting and return a 'org.w3c.dom.Document'
object.
In this case the DOM is styled with Xalan then any nodes from the i18n
namespace are replaced with internationalised text or data.

After processing with Xalan the result 'org.w3c.dom.Document' objects nodes
are lacking the node name prefix. (Though the namespace (xmlns) attributes
have been added to the correct nodes).  If the Document is serialised and
reparsed then the name prefixes are where I would have expected them.

The question is how can I avoid this reparseing step?

A more detailed sequence of events is as follows...

1) I Receive my input as an XML Document Object
<page_data xmlns:i18n='uri://com.myproject/i18n'>
    <greeting><i18n:text>hello</i18n:text></greeting>
    <date><i18n:date>20000927</i18n:date></date>
</page_data>

2) I process the document with the an XSL stylesheet
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="<http://www.w3.org/1999/XSL/Transform>"
version="1.0">
  <xsl:template match="page_data">
      <div>
        <xsl:copy-of select="greeting/*"/><BR/>
        (c)<xsl:copy-of select="date/*"/>
      </div>
  </xsl:template>
</xsl:stylesheet>

3) If I were to serialise and output the document at this point I get the
following result...
<div>
  <i18n:text xmlns:i18n="uri://com.myproject/i18n">hello</i18n:text><BR/>
  (c)<i18n:date xmlns:i18n="uri://com.myproject/i18n">20000927</i18n:date>
</div>

4) However instead of outputting this I process it by using a
NodeIteratorImpl object, looking only for nodes with the prefix 'i18n' in
order to do some post processing. According the iterator there are no such
nodes.

5) If instead take the serialised form (from 3 above) save it to a file and
then read and pass it to an XML Document object then use the same code to
iterate through it I get the <i18n:text> and <i18n:date> nodes returned no
problem.


Regards
   Ainsley Burdett


==== Relvant Java Sources ====

---- The first processor ----
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xalan.xslt.*;
public class XSLTModule
{
  private XSLTProcessor mFormatter;

  public XSLTModule()
  {
   mFormatter = XSLTProcessorFactory.getProcessor(new
org.apache.xalan.xpath.xdom.XercesLiaison());
  }

  public org.w3c.dom.Document format(org.w3c.dom.Document xmlDocument)
  {
    try
    {
      // Prepare the processor for use
      mFormatter.reset();
      // Get the formatting resource, the binary stylesheet
      StylesheetRoot stylesheet = mFormatter.processStylesheet(new
      XSLTInputSource(formattingResourceName));
      // Prepare source
      XSLTInputSource source = new XSLTInputSource(xmlDocument);
      // Prepare destination
      Document out = new DocumentImpl();
      XSLTResultTarget result = new XSLTResultTarget(out);
      // Tell the formatter the stylesheet to use
      mFormatter.setStylesheet(stylesheet);
      // process the document
      mFormatter.process(source, null, result);
      return out;
    }
    catch (Exception E)
    {
      System.out.println(E.getMessage());
    }
    return null;
  }
}

---- The second processor ----
import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.w3c.dom.traversal.*;

class I18NModule
{
  public org.w3c.dom.Document format( org.w3c.dom.Document xmlDocument)
  {
    try
    {
      NodeIteratorImpl N = new NodeIteratorImpl(
        (DocumentImpl)xmlDocument,
        xmlDocument.getDocumentElement(),
        NodeFilter.SHOW_ALL,
        new I18NNodeFilter(),
        true);

      Node node = N.nextNode();
      while (node!=null) {
        String data = node.getFirstChild().getNodeValue();

        // just replace the node with # text # for now
        TextImpl newNode = new
        TextImpl((DocumentImpl)node.getOwnerDocument(),"#"+data+"#");
        node.getParentNode().replaceChild(newNode,node);
        node = N.nextNode();
      }
      return xmlDocument;
    }
    catch (Exception E)
    {
      E.printStackTrace();
      System.out.println(E.getMessage());
    }
    return null;
  }
}
--The filter ---------
import org.w3c.dom.traversal.*;
import org.w3c.dom.Node;
class I18NNodeFilter implements NodeFilter
{
  public short acceptNode(Node n) 
  {
    String namespace = n.getPrefix();
    if (namespace!=null)
    {
      if (namespace.equals("i18n"))
      {
        return FILTER_ACCEPT;
      }
    }
    return FILTER_REJECT;
  }
}