Re: Re[2]: [xml] XPath Lack in libxml2-2.2.7 ?

Date view Thread view Subject view Author view

From: TOM (ptittom@free.fr)
Date: Fri Nov 03 2000 - 07:29:01 EST


On 03/11/2000 11:50:45 Stefan Bambach wrote:
> DV> you can still do
> DV> ctxt = xmlXPathNewContext(doc);
> DV> ctxt->node = the_node_you_want_to-start_from;
>
> I tried it. But it doesn't work. You know that I'm writing an
> object oriented Python XML Wrapper. So what I really want to do is
> an relative xpath expression this way:
>
> tag.xpath('p') # REALIZE: ONLY 'p', NO '/p' OR SOMETHING LIKE THIS

A relative XPath expression does not have any sense ouside a context.
This context consists of (as describe in the Rec) :
 - a context node
 - a context node list (which contains the context node)
 - 2 integers : the size of the list and the position of the context
node in this list
 - a set of functions (I guess the core XPath library is sufficient for
you, so forget this "field")
 - a set of namespaces (not yet done in libxml)

> Python Source Code:
> 1.) doc = open('test.xml') # loads xml document
> 2.) chapters = doc.xpath('//chapter') # gets list of 5 chapter
> nodes

The context node is the root node (/), the context node list contains
only /, the 'value' field contains the result of the expression
evaluation.

> 3.) for chapter in chapters: # iterate over chapters
> list

Set the precedent value as the context node list and iterate on it (set
each node as the context node and grow the proximityPosition).

> 4.) p_tags = chapter.xpath('p') # gets p nodes for each
> chapter

...and evaluate the expression with that context.

What you want to do is much like what's done when evaluating a
predicate. Take a look at xmlXPathEvalPredicate() in xpath.c. The only difference
is that you don't want to know if the expression evaluates to true or
false, you want the result of the expression evaluation : you just need to
skip the call to xmlXPathEvaluatePredicateResult().

> Workaround:
> ===========
> ctxt = xmlXPathNewContext((xmlDocPtr) node);
[...]
> - The syntax of the relative xpath expression is NOT compatible
> with XSLT. So it works for me, but I what to release my pyxml
> module and it should be as compatible as possible with XSLT path
> expressions before I release it :-)

What does it mean ? There isn't "XSLT path expression". The difference
between the match, test and select XSLT attributes is how you use the
result :
 - match : is the current node contained in the result node-set ?
 - test : does the result evaluates to true or false ?
 - select : iterate over the result node-set.
When processing XSLT, you need to evaluate the match attribute of each
template relative to the current context and look for the current node in
the result node-set. If you find it, you've found the appropriate
template, otherwise you need to search agin with other templates.

If you're interested in developping an XSLT processor based on libxml,
subscribe to the xslt@ml.free.fr list by sending a mail to
xslt-request@ml.free.fr with subscribe in the subject or body.

> Your xmlXPathRoot(ctxt) does the job. It copies the doc Ptr to
> node Ptr. So if I specify the node by myself in your way (see on
> top of this mail) it should work for this, but it seems that a bug
> in libxml forbit that kind of needed xpath expression ('p') in
> parsing expression.

The bug is in your reasoning : you want to evaluate a relative path
expression... relative to nothing.

HTH

Tom.

----
Message from the list xml@rpmfind.net
Archived at : http://xmlsoft.org/messages/
to unsubscribe: echo "unsubscribe xml" | mail  majordomo@rpmfind.net


Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Fri Nov 03 2000 - 09:44:24 EST