Hi David Thanks for your response and time. Pl. see my comments inline.
David : >Cloning is almost never necessary, nor ever efficient. The pointers returned in the XObject are pointers into your >instance document, so as long as that is in scope, all you have to do >is copy the pointers to your own data structure. Mayank: I tried that(copying pointers) but did't work. That's why turned to cloning as a last resort. Outside query(), pointers returned in the XObject pointer becoming invalid. I also tried making a copy of Xobjectptr itself(by assingning it to a global XObjectPtr hoping that this will increase the reference count of original XOjectPtr and hence increase the lifecycle of it), but got an assertion in ~XalanReferenceCountedObject(). David: >Are you using the default implementation of Xalan's source tree, or are you wrapping the Xerces DOM for these XPath >searches? The general rule is if you're going to modify the DOM >instance, you should use the Xerces DOM and wrap it for >Xalan. Mayank: I don't want to modify DOM instance hence am using Xalan all the way. David: >Please provide a _small_ snippet of code which shows how your are >building the tree and how you do the XPath query. Mayank: Pl. have a look below. QueryResult query(const char* xml, const char* context, const char* expr,ostream& errorStream) { QueryResult L_resultSet; // Initialize the XPath subsystem... XPathInit theInit; XalanSourceTreeDOMSupport theDOMSupport; XalanSourceTreeParserLiaison theLiaison(theDOMSupport); theDOMSupport.setParserLiaison(&theLiaison); XalanElement* rootElem = 0; try { XALAN_USING_XERCES(MemBufInputSource) // parse XML and get root element MemBufInputSource inStream((XMLByte*)xml, strlen(xml), "foo", false); XalanDocument* const doc = theLiaison.parseXMLStream(inStream); assert(doc != 0); rootElem = doc->getDocumentElement(); assert(rootElem != 0); } catch(...){} XPathEnvSupportDefault theEnvSupport; XObjectFactoryDefault theXObjectFactory; XPathExecutionContextDefault theExecutionContext(theEnvSupport, theDOMSupport, theXObjectFactory); XPathConstructionContextDefault theXPathConstructionContext; XPathFactoryDefault theXPathFactory; XPathProcessorImpl theXPathProcessor; try { XPath* const contextXPath = theXPathFactory.create(); theXPathProcessor.initXPath(*contextXPath, theXPathConstructionContext, XalanDOMString(context), ElementPrefixResolverProxy(rootElem, theEnvSupport, theDOMSupport)); XObjectPtr xObj = contextXPath->execute(rootElem, ElementPrefixResolverProxy(rootElem, theEnvSupport, theDOMSupport), theExecutionContext); const NodeRefListBase& contextNodeList = xObj->nodeset(); const unsigned int theLength = contextNodeList.getLength(); XPath* const xpath = theXPathFactory.create(); theXPathProcessor.initXPath(*xpath, theXPathConstructionContext, TranscodeFromLocalCodePage(expr), ElementPrefixResolverProxy(rootElem, theEnvSupport, theDOMSupport)); xObj = xpath->execute(contextNodeList.item(0), ElementPrefixResolverProxy(rootElem, theEnvSupport, theDOMSupport), theExecutionContext); // This will store the address of XObjectPtr in QueryResult class. // Can't pass reference here as Copying is not allowed. (~XalanReferenceCountedObject() will give // assertion failure). L_resultSet.setConfigNode(&xObj); // This will extract the Nodeset from Xobjectptr and then clone each individual XalanNode // and store the CLONED XalanNodes to a vector L_resultSet.cloneXalanNodes(); // Everything in L_resultSet works fine till here. Once this function return QueryResult CLONED nodes // inside L_resultSet will become invalid. return L_resultSet; } -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Friday, June 20, 2003 9:14 AM To: 'xalan-c-users@xml.apache.org' Subject: Re: XalanNode.cloneNode() issue !! > I'm new to XSL and related technologies. Was trying my hands on Xalan > 1.5 library for C++ and faced the following issue: > > Actually I tried to perform a Xpath query on a XML document. Xpath > Query is > working fine. The problem is to encapsulate the query > result(XObjectPtr) into my own class to use it somewhere else later > on. I've got a cloning method in my QueryResult wrapper class(see > attached code snippet below) to > clone all the XalanNodes which are available via XObjectPtr just after the > successful query(witin the scope of MyXPathQuery::query function). I'm > opting for cloning becoz as the XObjectPtr will go out of scope it's > reference count would be reduced to 0 and it will become invalid. I > tried making a copy of XObjectPtr(by assingment operator) returned by > the query but Xalan library is throwing an assertion immediately when > query() function > is exiting and this is happening when XObjectPtr is getting deleted. > (In ~XalanReferenceCountedObject()). According to my understanding it > sud't happen. Cloning is almost never necessary, nor ever efficient. The pointers returned in the XObject are pointers into your instance document, so as long as that is in scope, all you have to do is copy the pointers to your own data structure. Are you using the default implementation of Xalan's source tree, or are you wrapping the Xerces DOM for these XPath searches? The general rule is if you're going to modify the DOM instance, you should use the Xerces DOM and wrap it for Xalan. If you're not going to modify the DOM, and you're just parsing a stream of XML to create it, use Xalan's default source tree. If you are wrapping the Xerces DOM, you need to recover the original Xerces DOM nodes from the Xalan wrapper nodes. Please provide a _small_ snippet of code which shows how your are building the tree and how you do the XPath query. There are member functions provided to map Xalan nodes to Xerces nodes so you can do this. > Deep Cloning works fine as long as I'm using the cloned object till original > Xpointer object is valid. After original Xpointer object > dies(immediately after MyXPathQuery::query exited) all the string > based data in CLONED XalanNodes becomes invalid (all attribute/element > values and names). This is > happening inspite of DEEP CLONING. What is this Xpointer object you're talking about? We don't have a class by that name in Xalan-C. As I said before, cloning is rarely necessary, and it's irrelevant whether or not you're using deep cloning. > if( (*m_xObjectPtr)->getType() == XObject::eTypeNodeSet ) XObjectPtr has its own operator->() defined, so you can just do this: if( m_xObjectPtr->getType() == XObject::eTypeNodeSet ) Dave