You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Steven Kaufman <St...@individual.com> on 2000/08/31 16:53:28 UTC

Problem modifying Document object + using XSL processor

Hi,

My goal is to read in an XML document from a text file and parse it into a
Document object, dynamically add elements to the document, then apply a
stylesheet to the document using an XSL processor. I'm having trouble
because the new elements I dynamically add to the Document object do not
appear to be "seen" by the XSL processor. That is, the template I define in
the stylesheet to process the new element never gets executed. I'm using the
Apache Xerces XML parser and Xalan XSL processor.

Specifically, the initial XML document I read in from the file contains the
element <dnb:Topics>. After I create the Document object in memory, I try to
replace <dnb:Topics> with <dnb:Content> in the following way:
// Create <dnb:Content> element
Element contentElt = null;
        try {
            contentElt = 
                    (Element) doc.createElement("dnb:Content"); 
 // doc is the Document object
        }
.
.
//  Get <dnb:Topics> node from doc
        NodeList eltList;
        // This is a Node, but contentElt is an Element. Could this be a
problem ??
        Node dnbTopicsElt = null; 
        try {
            eltList = doc.getElementsByTagName("dnb:Topics");
            if (eltList.getLength() > 0) {
                dnbTopicsElt = eltList.item(0);            
                // Replace <dnb:Topics> with <dnb:Content>
                Node parent = dnbTopicsElt.getParentNode();
                parent.replaceChild(contentElt, dnbTopicsElt);
                return (doc);
            }         
            else {
                return (null);
            }
        }

I invoke the XSL processor in this method, passing in the Document object:

private void processXsl(Document doc) {
        try {
            // Get root node
            Node root = doc.getDocumentElement();            
            // Create an XSLT processor.
            XSLTProcessor processor = XSLTProcessorFactory.getProcessor();

            // Create XSLT input source: root node of Document
            XSLTInputSource docRoot= new XSLTInputSource(root);
            // Process document - output to stdout
            XSLTInputSource stylesheetID = new XSLTInputSource();
            processor.process(docRoot, gStylesheetID, new
XSLTResultTarget(System.out));

 // gStylesheetID is global var declared as:

 // private XSLTInputSource gStylesheetID;
 // gStylesheetID = new XSLTInputSource("tst.xsl");

        } catch (Exception e) {
            System.out.println("Something bad happened processing XSL!!");
            e.printStackTrace();
        }
    }

My test stylesheet contains the following template instruction:

<xsl:template match="dnb:Content">Matched dnb:Content</xsl:template>

When I run the application, it does not print out: "Matched dnb:Content".

I ruled out the possibility of my passing a bad Document object (one not
containing the dynamically added <dnb:Content> ) to the XSL processor. I
have a test class which can iterate thru all nodes in a Document object and
print out each element name, etc. That does output "<dnb:Content>" so I know
the elt. is there. 

Also, I ruled out any problem with the styelsheet; if I run the application
with an initial XML file already containing the <dnb:Content> node and I
don't try to dynamically modify the document obj, it DOES print out "Matched
dnb:Content".
If anyone wants a challenge and can figure out what I'm doing wrong, I'd
greatly appreciate it. Thanks!

-Steve Kaufman

 


Re: Problem modifying Document object + using XSL processor

Posted by Gary L Peskin <ga...@firstech.com>.
> Steven Kaufman wrote:
> 
> Hi,
> 
> My goal is to read in an XML document from a text file and parse it
> into a Document object, dynamically add elements to the document, then
> apply a stylesheet to the document using an XSL processor. I'm having
> trouble because the new elements I dynamically add to the Document
> object do not appear to be "seen" by the XSL processor.
>[snip]
> Element contentElt = null;
>         try {
>             contentElt =
>                     (Element) doc.createElement("dnb:Content");
>  // doc is the Document object
>         }

Steven --

I'm not sure what you're trying to do since I don't have the full source
document or stylesheet but do you want doc.createElementNS? 
createElement will create an element with a null namespace.

Gary