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 Moddy Te'eni <mt...@informatica.com> on 2009/03/03 07:40:17 UTC

Questions about XPATH API

I'm trying to write a server that uses XPath API. I based my code around the
sample 'SimpleXPathAPI'. I have many questions.

My server receives messages with documents, and returns the results of a few
XPath expressions on these documents. the expressions can be updated while
running. There are some external functions, and some varirables that i added
to XPathExecutionContextDefault (following dave's advice
http://www.nabble.com/creating-variables-for-XPath-td21823462.html)

1. Can I use an XPath after its XPathConstructionContext is deleted? if not,
can i use one global XPathConstructionContext to construct all my XPath
objects?

2. What is the difference between XPathProcessor::initXPath and
XPathProcessor::initMatchPattern ?

3. Why does XPathEvaluator::evaluate calls reset() for its executionContext
and XObjectFactory, and why does it sets the executionContext's EnvSupport,
DOMSupport and XObjectFactory? What happens if I set it only once? 
(assuming there is one evaluator for each input document)

4. Can I have one global XPathProcessorImpl?

I think that's it for today.

Thanks, Moddy.
-- 
View this message in context: http://www.nabble.com/Questions-about-XPATH-API-tp22303292p22303292.html
Sent from the Xalan - C - Users mailing list archive at Nabble.com.


Re: Questions about XPATH API

Posted by David Bertoni <db...@apache.org>.
Moddy Te'eni wrote:
> 
> David Bertoni wrote:
>>> 1. Can I use an XPath after its XPathConstructionContext is deleted? if
>>> not,
>>> can i use one global XPathConstructionContext to construct all my XPath
>>> objects?
>> No.  You can use a global XPathConstructionContext, but you will need to 
>> serialize access to it if you are using multiple threads. 
>>
> So I reckon I can have a global XPathConstructionContext and a global
> XPathProcessorImpl, and protect initXPath() with a mutex; destroy the XPaths
> only in the end.
> 
> 
> David Bertoni wrote:
>>
>>  
>>> 2. What is the difference between XPathProcessor::initXPath and
>>> XPathProcessor::initMatchPattern ?
>> One creates an XPath instance from an XPath expression, and the other 
>> creates an XPath instance from a match pattern, which is a limited 
>> subset of XPath.
>>
> Thanks. I'll use initXPath(), then.
> 
> 
> David Bertoni wrote:
>>> 3. Why does XPathEvaluator::evaluate calls reset() for its
>>> executionContext
>>> and XObjectFactory, and why does it sets the executionContext's
>>> EnvSupport,
>>> DOMSupport and XObjectFactory? What happens if I set it only once? 
>>> (assuming there is one evaluator for each input document)
>> It resets these for various reasons:
>>
>> 1. It resets the XPathExecutionContext and the XPathEnvSupport instance 
>> because it doesn't make sense for state from one evaluation to leak into 
>> another one.
>>
> 
> Makes sense.
> 
> 
> David Bertoni wrote:
>>
>> 2. It resets the XObjectFactory to prevent a large number of XObject 
>> instances from accumulating.  You can certainly change this if you want 
>> to.  For example, you might want to reset only after a certain number of 
>> XPath expressions have been evaluated.  This would mean you could keep a 
>> number of evaluation results around between evaluations.  The current 
>> model forces the user to copy the result of the evaluation before 
>> evaluating another expression.
>>
>>
> Let me rephrase to make sure I understand:
> XObjectFactory is one-per-thread, and when I reset it, it deletes all the
> XObject instances it had created. If I want to keep the instance after the
> reset, i need to copy it (using copy constructor, i presume) and keep it in
> another place.
No, you cannot copy-construct an XObject.  They are only available 
through the XObjectPtr smart-pointer wrapper.  To copy the value, you 
need to determine the type of the XObject by calling XObject::getType(). 
  After you've determined the type, you can call one of the typed 
accessor functions, like boolean(), num(), string() or nodeset().

For example, consider the following snippet:

XObjectPtr result = ...

if (result.getType() == XObject::eTypeNodeSet)
{
     NodeRefList copy = result.nodeset();
}

which copies the nodes from the result nodeset into a local NodeRefList 
instance.

If you prefer a more "object-oriented" approach, you can use the member 
function ProcessXObjectTypeCallback(), which will call back your 
XObjectTypeCallback-derive class with the appropriate value for the type 
of the XObject.  You can search the code for more details.

> 
> I guess I can use two factories: one for temporary results (which I reset
> frequently) and one for the results I need to keep.
It's easier just to make copies of the results into real types, instead 
of creating new XObject to wrap them.

Dave

Re: Questions about XPATH API

Posted by Moddy Te'eni <mt...@informatica.com>.

David Bertoni wrote:
> 
>> 1. Can I use an XPath after its XPathConstructionContext is deleted? if
>> not,
>> can i use one global XPathConstructionContext to construct all my XPath
>> objects?
> 
> No.  You can use a global XPathConstructionContext, but you will need to 
> serialize access to it if you are using multiple threads. 
> 
So I reckon I can have a global XPathConstructionContext and a global
XPathProcessorImpl, and protect initXPath() with a mutex; destroy the XPaths
only in the end.


David Bertoni wrote:
> 
> 
>  
>> 2. What is the difference between XPathProcessor::initXPath and
>> XPathProcessor::initMatchPattern ?
> One creates an XPath instance from an XPath expression, and the other 
> creates an XPath instance from a match pattern, which is a limited 
> subset of XPath.
> 
Thanks. I'll use initXPath(), then.


David Bertoni wrote:
> 
>> 3. Why does XPathEvaluator::evaluate calls reset() for its
>> executionContext
>> and XObjectFactory, and why does it sets the executionContext's
>> EnvSupport,
>> DOMSupport and XObjectFactory? What happens if I set it only once? 
>> (assuming there is one evaluator for each input document)
> 
> It resets these for various reasons:
> 
> 1. It resets the XPathExecutionContext and the XPathEnvSupport instance 
> because it doesn't make sense for state from one evaluation to leak into 
> another one.
> 

Makes sense.


David Bertoni wrote:
> 
> 
> 2. It resets the XObjectFactory to prevent a large number of XObject 
> instances from accumulating.  You can certainly change this if you want 
> to.  For example, you might want to reset only after a certain number of 
> XPath expressions have been evaluated.  This would mean you could keep a 
> number of evaluation results around between evaluations.  The current 
> model forces the user to copy the result of the evaluation before 
> evaluating another expression.
> 
> 
Let me rephrase to make sure I understand:
XObjectFactory is one-per-thread, and when I reset it, it deletes all the
XObject instances it had created. If I want to keep the instance after the
reset, i need to copy it (using copy constructor, i presume) and keep it in
another place.

I guess I can use two factories: one for temporary results (which I reset
frequently) and one for the results I need to keep.



David Bertoni wrote:
> 
> 
> Dave
> 
> 

Thank you.
-- 
View this message in context: http://www.nabble.com/Questions-about-XPATH-API-tp22303292p22323531.html
Sent from the Xalan - C - Users mailing list archive at Nabble.com.


Re: Questions about XPATH API

Posted by David Bertoni <db...@apache.org>.
Moddy Te'eni wrote:
> I'm trying to write a server that uses XPath API. I based my code around the
> sample 'SimpleXPathAPI'. I have many questions.
> 
> My server receives messages with documents, and returns the results of a few
> XPath expressions on these documents. the expressions can be updated while
> running. There are some external functions, and some varirables that i added
> to XPathExecutionContextDefault (following dave's advice
> http://www.nabble.com/creating-variables-for-XPath-td21823462.html)
> 
> 1. Can I use an XPath after its XPathConstructionContext is deleted? if not,
> can i use one global XPathConstructionContext to construct all my XPath
> objects?
No.  You can use a global XPathConstructionContext, but you will need to 
serialize access to it if you are using multiple threads. If you do 
this, you cannot destroy any XPath instances you've constructed with 
until you can destroy them all.  It works this way so that in situations 
where there are lots of XPath instances, such as in an XSLT stylesheet, 
common resources are shared.

> 
> 2. What is the difference between XPathProcessor::initXPath and
> XPathProcessor::initMatchPattern ?
One creates an XPath instance from an XPath expression, and the other 
creates an XPath instance from a match pattern, which is a limited 
subset of XPath.  Match patterns are described here:

http://www.w3.org/TR/xslt#patterns

These should really be separate classes, but the history is complicated, 
and it would be somewhat difficult to separate out the two classes at 
this point.

It's unlikely you would be interested in match patterns, since they are 
designed to determine whether or not a node matches the pattern, rather 
than to find what nodes match a particular XPath expression.

> 
> 3. Why does XPathEvaluator::evaluate calls reset() for its executionContext
> and XObjectFactory, and why does it sets the executionContext's EnvSupport,
> DOMSupport and XObjectFactory? What happens if I set it only once? 
> (assuming there is one evaluator for each input document)
It resets these for various reasons:

1. It resets the XPathExecutionContext and the XPathEnvSupport instance 
because it doesn't make sense for state from one evaluation to leak into 
another one.

2. It resets the XObjectFactory to prevent a large number of XObject 
instances from accumulating.  You can certainly change this if you want 
to.  For example, you might want to reset only after a certain number of 
XPath expressions have been evaluated.  This would mean you could keep a 
number of evaluation results around between evaluations.  The current 
model forces the user to copy the result of the evaluation before 
evaluating another expression.

3. You can ignore DOMSupport::reset() as it is only there for 
backward-compatibility. It's a no-op in the current version.

> 
> 4. Can I have one global XPathProcessorImpl?
As long as you serialize access to it and any other objects it uses that 
are not thread-safe.

Most objects in Xalan-C are not thread safe.  The ones that are, such as 
instances of the the source tree, and XPaths are thread-safe, because 
they keep state in the other objects (such as XPathExecutionContext, 
XPathEnvSupport, etc.).  This allows sharing these "heavier" objects 
between threads.

Dave