Hi, Stefan,

  1). the DOMParser, --does--, differentiate,
       a) attribute with empty value
       b) attribute does NOT show up.

   2). The DOM --allowes-- you manipulate attribute value through setValue
() .

   3). You may need to download the nightly build to do this.

The followings is the dtd, xml and code snippet:

dtd:
========
<?xml encoding="ISO-8859-1"?>

<!-- @version:  -->

<!ELEMENT root (child)*>
<!ELEMENT child EMPTY>
<!ATTLIST child attr CDATA "defValue">

xml instance document
====================
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE root SYSTEM "nullattr.dtd">

<!-- @version: -->

<root>

  <child attr="" />
  <child attr="myValue" />
  <child />

</root>


test code
=============
      DOM_Document doc2 = parser->getDocument();
      DOM_NodeList childList= doc2.getElementsByTagName("child");

      int len=childList.getLength();
      for (int i=0; i<len; i++)
      {
          DOM_Node tmp=childList.item(i);
          DOM_Element child=(DOM_Element &) tmp;
          DOM_Attr attr= child.getAttributeNode("attr");

          cout<<"b4:<"<<attr.getValue().transcode()<<">"<<endl;
          attr.setValue("");
          cout<<"atter<"<<attr.getValue().transcode()<<">"<<endl;
      }

output
=========
b4<>
after<>
b4<myValue>
after<>
b4<defValue>
after<>

Regards,

Peiyong Zhang
____________________________________________
XML Parsers Development
IBM Toronto Laboratory email: [EMAIL PROTECTED]
Phone: (416)448-4088; Fax: (416)448-4414; T/L: 778-4088



Stefan Brauneis <[EMAIL PROTECTED]>@darmstadt.gmd.de on 05/22/2001
06:26:46 AM

Please respond to [EMAIL PROTECTED]

Sent by:  [EMAIL PROTECTED]


To:   "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
cc:
Subject:  How to set an attribute value to NULL?


Hi all,

well, I don't know exactly but maybee this is more a problem of
understanding xml in my mind then a real xerces prob, but anyway I
would be apreciate for your help.

I would like to do the following:

I have an element in an xml-doc which has an attribute with a default
value given in the dtd.
But when I parse the doc with the attribute-value given as an empty
string (like attr="") the attribute will be overwritten by the default
value! But I don't want this to happen. I thought that an attribute is
only substituted by the default when it is not present at all in the
element. Or in other words, why doesn't the parser distinguish
between an empty attribute value and the absence of the attribute at
all?

An other problem is pretty similar:
When I parse the same document I am trying to manipulate the above
mentioned attribute value, it is not possible to set it to NULL or an
empty string! What is happining is that the value is not manipulated at
all!

Well, to recapitulate: My question is
1. Is it possible to tell the parser to distinguish between an empty
attribute value and no attribute at all?
2. Is it possible to set an attribute value to an empty string or NULL?
3. If 1 & 2 are answered with no: Is this an xml probleme, a xerces
problem or my own problem ? ;-)

I would be very grateful for any help or advice and I have attached some
code snippets to explain my problems in this way.

cu
    stefan<!ELEMENT root (child)*>

<!ELEMENT child EMPTY>
<!-- If the mentioned behavior could be achieved with xml: 
     How does
this dtd has to look like? -->
<!ATTLIST child  attr CDATA "someData">


<!DOCTYPE root SYSTEM "example.dtd">
<root>
  <!-- I would like the parser to keep this! But he changes the attr=""
to attr="someData" -->
  <child attr=""/>
</root>


#include <iostream>
#include <unistd.h>
#include <framework/LocalFileInputSource.hpp>
#include <util/PlatformUtils.hpp>
#include <parsers/DOMParser.hpp>
#include <dom/DOMString.hpp>
#include <dom/DOM_Element.hpp>

int main(void)
{

  // Please don't blame me for my coding style
  // this is just a quick hack to explain
  // my problems!
  try
    {
      XMLPlatformUtils::Initialize();
    }
  catch (...)
    {
      cout<<"Some error occured"<<endl;
      _exit(0);
    }
  DOMParser parser;

  try
    {
      parser.parse("example.xml");
    }
  catch(...)
    {
      cout<<"Some error occured"<<endl;
      _exit(0);
    }

  DOM_Document doc= parser.getDocument();
  DOM_NodeList childList= doc.getElementsByTagName("child");

  DOM_Node tmp=childList.item(0);
  DOM_Element child=(DOM_Element &) tmp;

  DOM_Attr attr= child.getAttributeNode("attr");

  cout<<"This is attr before manipulation: "<<attr.getValue().transcode()
<<endl;

  //!!!!This doesn't seem to work!!!!Why?
  attr.setValue("");

  cout<<"This is attr after manipulation: "<<attr.getValue().transcode()
<<endl;

}




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to