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



Hi Daniel!

First of all, thanks a lot for your quick reply!

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?
 
So, what's the way around this problem?

  Opening a basic book on data structure algorithms ? 
  Alternately use the XPath module and search for "//your_node_name[1]"

Can the second solution also be used when one wants to replace a
fragment? By "replace" I mean not only exchanging a node's content, but
also reinserting it at the *same* position within the tree.

AFAIK, when using XPath, I get an xmlXPathObjectPtr (at least when using
xmlXPathEvalExpression() or xmlXPathCompiledEval()). After having looked
at xpath.c and testXPath.c, using the search engine in order to search for
both "xmlXPath", "xmlNodeSet", and ""xmlXPathObjectPtr" unfortunately I
still haven't found a way to obtain an xmlNodeSet from an
xmlXPathObjectPtr and an xmlNodePtr (a reference to one
node) from an xmlNodeSet. This is important to me since

a) I know my XPath expression will return a node set containing exactly
one node
b) I need to obtain this node in order to replace it by a new one using
xmlReplaceNode()

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
xmlXPathNodeSetItem() for this purpose? I am asking these questions since
I'm not sure whether accessing "nodesetval" is a "clean" solution. The
"clean" solution is to use a function providing access to a node set and
its elements (provided that there is one).

[...] 
  It's a complex operation, it has limitations (there is NO semantic defined
in the XML specifications about parsing a fragment of a document, this does
not exist, don't try it).

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? 

[...] 
  You apparently missed the xmlCopyNode() and related operations.

Ok, I was looking through the documentation again and found
xmlSetListDoc() and xmlSetTreeDoc(). Unfortunately, even after having
looked at both function implementations at tree.c, I'm not sure I
understand the difference. xmlSetTreeDoc() calls xmlSetListDoc(), and
xmlSetTreeDoc() calls xmlSetListDoc(). So, in what respect do they differ?

(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.)

Thanks in advance for any info!

Greetings,

        Holger






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