From: Stefan Bambach (bambach@triplex.de)
Date: Fri Nov 03 2000 - 05:50:45 EST
Hi Daniel and others,
Thursday, October 26, 2000, 3:38:45 PM, you wrote:
DV> On Thu, Oct 26, 2000 at 04:31:52PM +0200, Stefan Bambach wrote:
>> Is there any functionallity in this way:
>> xmlXPathNewContext(xmlNodePtr node); ??????????
DV> you can still do
DV> ctxt = xmlXPathNewContext(doc);
DV> ctxt->node = the_node_you_want_to-start_from;
DV> Daniel
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
Detailed information:
=====================
taking your example from testXPath.c:
<?xml version=\"1.0\"?>
<EXAMPLE prop1=\"gnome is great\" prop2=\"& linux too\">
<head>
<title>Welcome to Gnome</title>
</head>
<chapter>
<title>The Linux adventure</title>
<p>bla bla bla ...</p>
<image href=\"linus.gif\"/>
<p>...</p>
</chapter>
<chapter>
<title>Chapter 2</title>
<p>this is chapter 2 ...</p>
</chapter>
<chapter>
<title>Chapter 3</title>
<p>this is chapter 3 ...</p>
</chapter>
<chapter>
<title>Chapter 4</title>
<p>this is chapter 4 ...</p>
</chapter>
<chapter>
<title>Chapter 5</title>
<p>this is chapter 5 ...</p>
</chapter>
</EXAMPLE>
Python Source Code:
1.) doc = open('test.xml') # loads xml document
2.) chapters = doc.xpath('//chapter') # gets list of 5 chapter nodes
3.) for chapter in chapters: # iterate over chapters list
4.) p_tags = chapter.xpath('p') # gets p nodes for each chapter
5.) for p in p_tags: # iterate over p_tags list
6.) print p.values() # print p-value
executing this code should result in:
this is chapter 2 ...
this is chapter 3 ...
this is chapter 4 ...
this is chapter 5 ...
Problem:
========
You see that the 2nd xpath expression is only 'p', no '/p' or something
like this. I got some kind of error message from libxml:
CHECK_TYPE(XPATH_NODESET); throws error because ctxt->value is NULL;
see next lines for details:
----------------
Error xpath.c:2461: Invalid type
p
^
xmlXPathEval: evaluation failed
Object is empty (NULL)
----------------
I got the same error message with "./testXPath p" !
It seems that libxml generally throws an error if you have such
relative expressions as your xpath. You have the same problem, if you
want to use XSLT for the output of 'test.xml':
<xsl:template match="chapter">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="p">
<xsl:value-of select="."/>
</xsl:template>
Workaround:
===========
ctxt = xmlXPathNewContext((xmlDocPtr) node);
With this, it works, but it does a 'changeroot' of course.
- Now '/' matches to given node of xmlXPathNewContext and '/p' matches
to my needed p-tag.
- The xpath expressions root element is now my node element. You can
not match the really root element of the document.
- You cannot match TAGS above my new root element.
- 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 :-)
Summary / Questions:
====================
Giving node ptr to xmlXPathNewContext does his job too, except for the
wrong xpath expression I have to give to xmlXPathEval ('/p') :-( This
way the xmlXPathRoot will be called, the context->node ptr will be
set, the expression throws NO error and I get my wanted node ptrs
back (problems see workaround).
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.
Is there a chance that my 'p' expression will be accepted, such as
XSLT match expression do ?
Is there another way solving this problem ?
Is such xpath expression ('p') generally forbidden in XPath standard ?
Is it defined in XPath standard or is there perhaps another standard
that does this ?
thx. ciao.
Stefan Bambach
-- Stefan Bambachtriplex-agentur fuer neue medien GmbH herzog-heinrich-strasse 11-13 80336 muenchen
tel: 089-209 138 29 fax: 089-209 138 10
mailto:bambach@triplex.de http://www.triplex.de
---- Message from the list xml@rpmfind.net Archived at : http://xmlsoft.org/messages/ to unsubscribe: echo "unsubscribe xml" | mail majordomo@rpmfind.net
This archive was generated by hypermail 2b29 : Fri Nov 03 2000 - 09:44:24 EST