RE: [xml] Using variables with XPath expressions



Well, I have narrowed down the problem, but not sure my fix is entirely
correct.

When the xpath expression returned more than 1 node prior to the attribute
filtering using the variable, the expression would  crash in cleanup.  But,
if the expression would return 1 or less node(s), it would work.  The
original code below would probably work without issue.

A better demo of the problem would be:
<?xml version="1.0"?>
<root>
 <node value="test"/>
 <node value="test" value2="test"/>
</root>

pseudo-code:

obj = xmlXPathEval("'test",doc);
/* obj returns as expected */
xmlXPathRegisterVariable(ctx,"var",xmlXPathObjectCopy(obj));
obj = xmlXPathEval("$var",ctx);
/* obj returns as expected */
obj = xmlXPathEval("//*[ value2=$var]",ctx);
/* obj returned as expected, since there was only 1 node the filter using
the variable was applied to */
obj = xmlXPathEval("//*[ value=$var]",ctx);
/* this would crash */

My fix (I believe) was to correct xmlXPathCompOpEval to use a copy of the
variable value rather than the value directly.  I have done a fair amount of
testing using this and all my expressions using variables return the
expected results.  

        case XPATH_OP_VARIABLE: {
            if (op->ch1 != -1)
                xmlXPathCompOpEval(ctxt, &comp->steps[op->ch1]);
            if (op->value5 == NULL)
--              valuePush(ctxt,
--                  xmlXPathVariableLookup(ctxt->context, op->value4));
++              valuePush(ctxt,xmlXPathObjectCopy(
xmlXPathVariableLookup(ctxt->context, op->value4)) );


Daniel - Can you confirm that this is the right approach.

-----Original Message-----
From: Vakoc, Mark [mailto:Mark_Vakoc jdedwards com]
Sent: Tuesday, June 26, 2001 9:36 AM
To: 'xml gnome org'
Subject: [xml] Using variables with XPath expressions



Hi All,

I have a series of XPath expressions that are fairly time-intensive and each
is dependent on the outcome of the previous.  I'm trying to use variables to
simplify the expressions and minimize unnecessary processing.  The following
consistently dumps consistently on 2.3.11:

<?xml version="1.0"?>
<root title="test">
 <node value="test"/>
</root>

And the code (simplified to illustrate the point):

ctx = xmlXPathNewContext(doc);
obj = xmlXPathEval("string(/root/@test)",doc);
/* obj returns as expected */
xmlXPathRegisterVariable(ctx,"var",xmlXPathObjectCopy(obj));
obj = xmlXPathEval("$var",ctx);
/* obj returns as expected */
obj = xmlXPathEval("//node[ value=$var]",ctx);
/* the above crashes */

Any ideas?

_______________________________________________
xml mailing list
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]