Re: [xml] Exchanging same XML fragment in multiple XML documents



On Wed, Nov 06, 2002 at 01:09:03PM +0100, Holger Rauch wrote:
On Tue, 5 Nov 2002, Daniel Veillard wrote:
[...] 
  Strange, I'm 100% sure I can write completely generic tree traversing
algorithms with just "children", "parent", "next".

You obviously mean that when starting from the root of the tree, it is
enough to first check all nodes on one level of the tree using "next", and
if next == NULL, proceed to the next level using "children" and do this
for all levels of the tree?

  obviously, no :-). You really should buy that book on computer data
structures and algorithms. What you're asking for really is not libxml2
specific. If you have troubles with tree traversal algorithms, then I'm
afraid you will continue having troubles understanding the semantic of
a number of the operations provided by the libxml2 API. Admmmitedly the
documentation doesn't go as far as describing things as tree traversal,
document order, etc.

How can I obtain individual nodes of a node set, provided that I've got an
xmlXPathObjectPtr? Do I have to access "nodesetval" directly? Can I use

  The first thing to do when you have such a question is to lookup
the definition of xmlXPathObjectPtr, isn't it ?

typedef struct _xmlXPathObject xmlXPathObject;
typedef xmlXPathObject *xmlXPathObjectPtr;
struct _xmlXPathObject {
    xmlXPathObjectType type;
    xmlNodeSetPtr nodesetval;
...
}

  isn't that obvious ?
  object->nodesetval
of course you need first to check the type,
then to get to the node(s) assuming the nodesetval ain't NULL:

typedef struct _xmlNodeSet xmlNodeSet;
typedef xmlNodeSet *xmlNodeSetPtr;
struct _xmlNodeSet {
    int nodeNr;                 /* number of nodes in the set */
    int nodeMax;                /* size of the array as allocated */
    xmlNodePtr *nodeTab;        /* array of nodes in no particular order */

If you want me to refrain from using
xmlParseBalancedChunkMemory(), what alternative do you suggest, provided
that I want to repeatedly replace a fragment in various input documents? 

  copy it in each document using xmlDocCopyNode()

/**
 * xmlDocCopyNode:
 * @node:  the node
 * @doc:  the document
 * @recursive:  if 1 do a recursive copy.
 *
 * Do a copy of the node to a given document.
 *
 * Returns: a new #xmlNodePtr, or NULL in case of error.

understand the difference. xmlSetTreeDoc() calls xmlSetListDoc(), and
xmlSetTreeDoc() calls xmlSetListDoc(). So, in what respect do they differ?

  one operates on a single node subtree, and the other does the same on the
full list 

/**
 * xmlSetTreeDoc:
 *
 * update all nodes under the tree to point to the right document

/*
 * xmlSetListDoc:
 *
 * update all nodes in the list to point to the right document

 
(Though I realized that since xmlParseBalancedChunkMemory() fills in a
node list I should probably use xmlSetListDoc() instead of
xmlSetTreeDoc(). But since you advise me to stay away from document
fragments, I don't know where I'm at.)

  Honnestly, you seems easilly confused by the semantic of the operations
so I suggest you stay within the realms of the APIs you understand clearly
and xmlParseBalancedChunkMemory() semantic is very complex, even has some
gray areas, I don't suggest you use it.

Daniel

-- 
Daniel Veillard      | Red Hat Network https://rhn.redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/



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