'Re: "Re: [xml] manipulating attributes/values"'



Hi,

on 4/13/2004 9:35 AM Daniel Veillard wrote:
On Tue, Apr 13, 2004 at 05:21:55PM +1000, Roger Reynolds wrote:

One such item is the oraxml function setAttribute(node, name, value)
which will either create a new attribute node under the specified node,
having the specified name and value, or if the node already has an attribute
node with the specified name, replace it's current value with the newly
specified value.

[...]

Easy...

I suppose I need to manually traverse the child nodes of my node, looking
for
an appropriately named attribute node, and then I can use  xmlNodeSetContent
if I
find one,  or I can create a new attribute node (property) if I don't find
one...

That sound about right?


  yes


I appologize in advance for not being as familiar with libxml as I might be.
I have been looking over the code and examples most of the day...


  xmlSetProp() and xmlSetNsProp()
  http://xmlsoft.org/html/libxml-tree.html#xmlSetNsProp

  the tree manipulations routines are usually in the tree module, 

Daniel

"xmlSetProp" and "xmlSetNsProp" might slightly differ from the 
implementation of the setAttribute function of oraxml. Since I don't 
know if oraxml is DOM conformant, I just want to show what issues need 
to be handled *if* it was DOM conformant:

1. If the attribute is a namespace declaration attribute you need to 
create a xmlNs and add it to the nsDef chained list of a xmlNode with 
"xmlNewNs". Note that in libxml2, NS declaration attributes are not 
located in the "properties" partition.

2. You may want to check with "xmlHasProp" if the attribute is already 
existent on the element. If it is and you use a DTD, you should also 
check if it is a fixed attribute, since they are read only:
(attr->type == XML_ATTRIBUTE_DECL) && ((xmlAttributePtr)attr->def == 
XML_ATTRIBUTE_FIXED))

3. Create a new attribute with "xmlNewProp(element, attributeName, 
NULL)" (or xmlNewNsProp, if the attribute is bound to a namespace). Note 
that the value of the attribute is NULL, since the explicit setting of 
an attribute should produce a singe unparsed TEXT_NODE only. 
"xmlNewProp" does a "xmlStringGetNodeList" on the value, thus producing 
TEXT_NODEs and ENTITY_REFERENCE_NODEs - this is not wanted in DOM.

4. Set the value of the attribute; this could look like the following 
(sorry for the Pascal version - I'm too lazy to convert it to C today):

procedure idom_setAttrValue(aAttr: xmlAttrPtr; const aValue: DOMString);
begin
   if (aAttr.children <> nil) then begin
     xmlFreeNodeList(aAttr.children);
     aAttr.children := nil;
     aAttr.last := nil;
   end;
   if (aValue <> '') then
     xmlAddChild(xmlNodePtr(aAttr), xmlNewDocText(aAttr.doc, 
pxmlChar(UTF8Encode(aValue))));
end;


Ah, and don't forget to look at the namespace handling of libxml2; it 
will automatically add namespace declaration attributes during the work 
on the tree if needed. This differs from the DOM as well, where missing 
NS declaration attributes are created with the "normalizeDocument" 
method only.

So the best would be to run some tests on the oraxml functions to 
evaluate how they behave, and then reproducing the behaviour with bits 
of libxml2. But maby you are lucky, and oraxml works exactly like 
libxml2 ;-)

Hope this was usefull somehow...


Greetings and good luck,

Kasimier




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