You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by David N Bertoni/CAM/Lotus <Da...@lotus.com> on 2001/10/11 16:37:16 UTC

Re: XMLTransformer question

Hi Mark,

XalanTransformer has its own classes for representing parsed documents.
You should use those for keeping compiled stylesheets and parsed source
documents around.  In particular, you should take a look at the ThreadSafe
sample, which uses a XalanTransformer instance as a "factory" for creating
compiled stylesheets and parsed source documents.  This is handy, because
the XalanTransformer instance keeps track of everything and deletes all of
the stuff when it goes out of scope.  The only disadvantage is that this
isn't thread-safe, so if your application is multithreaded, you'll need to
synchronize access to this instance when you need to parse something, or
compile a stylesheet.  (Read-only access to the parsed documents and
compiled stylesheets _is_ thread-safe.)

You can also create compiled stylesheets and parsed source documents
directly, but you'll need to manage things.  It's pretty straightforward --
just see the code in XalanTransformer::compileStylesheet() and
XalanTransformer::parseSource().  For information on creating an
XSLTInputSource to parse a memory buffer, see the StreamTransform sample.

Hope that helps...

Dave



                                                                                                         
                      "Mark Weaver"                                                                      
                      <mark@npsl.co.uk         To:      <xa...@xml.apache.org>                   
                      >                        cc:      (bcc: David N Bertoni/CAM/Lotus)                 
                                               Subject: XMLTransformer question                          
                      10/10/2001 10:07                                                                   
                      AM                                                                                 
                                                                                                         
                                                                                                         



I'm porting some code over from using the MSXML parser.  What the code
currently does is this:

(1) Get an XML document
(2) Find a "stylesheet" node within the document
(3) Get the stylesheet, if not cached & parse it.  If it is cached, use
the precompiled stylesheet.
(4) Apply the transform (spits out some HTML).

What I would like to know is how is it possible to do something similar
with Xalan?  The XSLTInputSource seems to be rather confusing in that you
can construct one with pretty much any old object, but the only thing that
I can get to work so far is an istream*, and I want to avoid doing this as
I would then have to parse the XML document twice.  If I construct it with
a
XalanDocument like this:

    // We'll use these to parse the XML file.
    XalanSourceTreeDOMSupport       theDOMSupport;
    XalanSourceTreeParserLiaison    theLiaison(theDOMSupport);

    // Hook the two together...
    theDOMSupport.setParserLiaison(&theLiaison);

    XalanElement*   rootElem = 0;
    XalanDocument*  xmlDoc = 0;

    try
    {
            // parse XML and get root element
            MemBufInputSource inStream((XMLByte*)pXML,
                                                 http.m_response.count()
-http.m_bodyOffset, "foo", false);

            xmlDoc = theLiaison.parseXMLStream(inStream);
            assert(doc != 0);

            rootElem = xmlDoc->getDocumentElement();
            assert(rootElem != 0);
    }
    catch(const XMLException&)
    {
             OutputDebugStringA("arse\n");
    }

             XSLTInputSource source(xmlDoc);
             /* apply transform... */

Then I get:

An exception occured! Type:RuntimeException, Message:The primary document
entity could not be opened. Id={null}

as the error message from XalanTransformer.  What am I doing wrong?

Thanks,

Mark