[xml] Xpath/Xpointer question...

Date view Thread view Subject view Author view

From: Dave Madole (dmadole@cddb.com)
Date: Mon Dec 04 2000 - 15:17:04 EST


This is an Xpath question, not specifically libxml oriented, but as the
answer would have to be
expressed using libxml (and I can't find the answer anywhere else, as
these things seem to be
documented sparingly) so I'll ask it here. If anyone can suggest a
better list for the question, I'm
all ears.

I have XPath working fine, but I can't find a location path/ predicate
to give me what I want.

Lets say I have a document:

<?xml version="1.0"?>
<doc>
    <sec part_id="I">
        <para part_id="a">This is section I, paragraph 1
            <clause part_id="1">This is section I, paragraph a, clause
1</clause>
            <clause part_id="2">This is section I, paragraph a, clause
2</clause>
 </para>
        <para part_id="b">This is section I, paragraph b</para>
    </sec>
   <sec part_id="II">
        <para part_id="a">This is section II, paragraph a
            <clause part_id="1">This is section II, paragraph a, clause
1</clause>
 </para>
        <para part_id="b">This is section II, paragraph b</para>
    </sec>
</doc>

The path (most explicitly):

//sec/para/clause/self::*[@part_id='1']

Gives me:

<?xml version="1.0"?>
<doc>
    <clause part_id="1">This is section I, paragraph a, clause
1</clause>
    <clause part_id="1">This is section II, paragraph a, clause
1</clause>
</doc>

(I built the "doc" node, of course).

Is there any way to get more complete fragments to put into the new doc:

<doc>
    <sec part_id="I">
        <para part_id="a">
            <clause part_id="1">This is section I, paragraph a, clause
1</clause>
         </para>
     </sec>
<sec part_id="II">
        <para part_id="a">
            <clause part_id="1">This is section II, paragraph a, clause
1</clause>
         </para>
     </sec>
</doc>

i.e., just the ancestors without the siblings (of course I get the whole
tree if I specify )
without writing a function (going back up through the tree and pruning
all the siblings). I've tried all kinds of things (I won't list them
here) but they either give me what I showed above or too much.

This is the code chunk (pretty much copied from "testXPath" I'm using to
build the new tree, which is currently at positioned "currnode").

 ctxt = xmlXPathNewContext(olddoc);
 res = xmlXPathEval(BAD_CAST str, ctxt);
 fprintf(stderr, "nodeNr %d i %d\n",res->nodesetval->nodeNr,i);
 for (i = 0; i < res->nodesetval->nodeNr; i++) {
          newnode = xmlCopyNode(res->nodesetval->nodeTab[i],1);
          xmlAddChild(currnode,newnode);
 }

Or is this what we need XSLT for?

Thanks,

Dave Madole

----
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 : Mon Dec 04 2000 - 15:43:42 EST