Sorry for repeating this question. I thought I’d try to clarify my problem and see if someone has run into similar issue. I’ve had this question for a while so I am eager to find out what others have to say.

 

Basically, I’d like to create a node set inside an extension function. In pseudo-code, here’s what’s going on:

 

In the stylesheet, I have something like

<xsl:copy-of select="external:myfunc($input)"/>

 

where $input is a node set defined as:

<xsl:variable name="input" select="/Elements/Element"/>

 

and documents are like

<?xml version="1.0" encoding="UTF-8"?>

<Elements>

<Element>1</Element>

<Element>2</Element>

</Elements>

 

In the extension function, the code (vastly simplified) is like

 

XObjectPtr FunctionPromoCache::execute(

XPathExecutionContext& executionContext,  XalanNode* context, const XObjectArgVectorType& args, const LocatorType* locator) const

{

 

const ResultTreeFragBase& theInput = args[0]->rtree();

 

std::vector<XalanNode*> theElements

 

//

// fill the vector with individual XalanNodes

//

 

XalanNode* pChild = pNode->getFirstChild();

 

if (!pChild)

{

…        

}

do {

            if (pChild->getNodeType() != XalanNode::ELEMENT_NODE)

            {

                        continue;

            }

            theElements.push_back(pChild);

 

} while (pChild = pChild->getNextSibling());

 

            //

            // Create the result – for the sake of this example, without any change

            //

 

XObjectFactory::BorrowReturnMutableNodeRefList theResult(executionContext);

 

std::vector<XalanNode*>::const_iterator  itNode = theElements.begin(),endNode = theElements.end();

 

for (;itNode != endNode; ++itNode)

{

            theResult->addNode(*itNode);

}

 

return executionContext.getXObjectFactory().createNodeSet(theResult);

}

 

So, this function works as long as the result contains pointers to existing nodes created during the transform. Here, the XSL statement copy-of will output

<Element>1</Element>

<Element>2</Element>

 

My problem is that I would like to create new nodes and embedded the existing elements in them. For instance, I’d like the  <xsl:copy-of select="external:myfunc($input)"/> to return something like

 

 <MyElements>

<Element>1</Element>

<WeirdElements>

<Element>3</Element>

</WeirdElements>

</MyElements>

 

where I’d create the MyElements and WeirdElements nodes inside the extension function.

 

So my question is, what is the best way to generate new XalanNodes in such a way that they get properly created and deleted inside the transformation.

 

Thanks in advance,

 

Emmanuel

 

-----Original Message-----
From: Abram-Profeta, Emmanuel
Sent: Wednesday, April 23, 2003 12:39 PM
To: [email protected]
Subject: Best way to create a XalanNode inside an extension function

 

Hi,

 

I looked at the extensionFunction sample in Xalan 1.4 and 1.5 and I am trying to come up with a function that builds new elements dynamically.

 

Background: The examples shown deal with string/number like results (e.g., sqrt, cube, etc). For instance (sorry for the tabs…)

 

virtual XObjectPtr

            execute(

                        XPathExecutionContext&                                    executionContext,

                        XalanNode*                                                                    context,

                        const XObjectArgVectorType&                args,

            const LocatorType*                                            locator) const

            {

 

….

 

                        return executionContext.getXObjectFactory().createNumber(sqrt(args[0]->num()));

            }

 

So far, I’ve been able to do the same thing and use the executionContext.getXObjectFactory().createNodeSet() function to return XalanNode* pointers belonging to XalanDocument objects output by a transform or created from a xerces DOM using the XercesDOMParserLiaison.

 

However, what I’d like to do is manually create a XalanNode* that would be an XML element with embedded elements within; The difference here is that I am not reusing pointers to locations of existing documents. I’m aware that it may sound like a trivial question but after experimenting with the XalanDOM and the XalanSourceTree classes, I noticed that there’s still a lot of functionality that remains to be implemented. (Hence my questions.)

 

One trivial way I thought of was to just create a string containing the XML I need and parse it inside the extension function. But I’m sure there’s a smarter and better way to just say “create this XalanNode with this name, append this node set, etc.”.

 

Thanks in advance for any help,

 

Emmanuel

 

Reply via email to