You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Francois Rioux <ri...@gel.ulaval.ca> on 2001/07/05 19:34:22 UTC

creating element with ID in a document

Hi,
	with the DOM_Document function createElement, i am trying to create an 
element that has an attribute of type ID as defined in the DTD.
When I set this attribute, it is supposed to be added in the ID table of 
the document, but it does not work with Xerces 1.5. Is this feature not 
implemented yet or
is this a bug???

Here is a code example...

The DTD file (test.dtd):

<!ELEMENT root (foo)*>
<!ELEMENT foo EMPTY>
<!ATTLIST foo
	name ID #REQUIRED
 >

the XML file(test1.xml):

<!DOCTYPE root SYSTEM "test.dtd">

<root>
	<foo name="first"/>
</root>

and the source file:

#include "parsers/DOMParser.hpp"
#include "util/PlatformUtils.hpp"
#include "dom/DOM.hpp"
#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
	try
	{
		XMLPlatformUtils::Initialize();
	}
	catch(...)
	{
		cout<<"Initialisation exception..."<<endl;
		exit(1);
	}
	DOMParser *parser1 = new DOMParser;
	parser1->setDoValidation(true);
	parser1->parse("test1.xml");
	DOM_Document document1 = parser1->getDocument();
	DOM_Element element1 = document1.getDocumentElement();
	DOM_Element newElement= document1.createElement("foo");
	newElement.setAttribute("name", "second");	// This is the ID attribute...
	element1.appendChild(newElement);
	DOM_Node searched= document1.getElementById("second");	// Not supposed to 
be NULL...
	cout<<(searched.isNull()?"Null":"Not Null")<<endl;	// it is NULL...
	return 0;
}

	Thanks, Francois


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: creating element with ID in a document

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Francois Rioux" <ri...@gel.ulaval.ca> writes:

> 	with the DOM_Document function createElement, i am trying to
> 	create an element that has an attribute of type ID as defined
> 	in the DTD.
> 
> When I set this attribute, it is supposed to be added in the ID table
> of the document, but it does not work with Xerces 1.5. Is this feature
> not implemented yet or


Hi Francois,

When the document is parsed, the information is added to the ID
table. But when you create a new element, no effort is made to update
the ID table. 

It seems that you would need to reparse the output of the modified DOM
tree.

HTH,
jas.

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: creating element with ID in a document

Posted by Evert Haasdijk <ev...@zukkespijkers.nl>.
> -----Original Message-----
> From: Jason E. Stewart [mailto:jason@openinformatics.com]
> Sent: Thursday, July 05, 2001 20:49 PM
> To: xerces-c-dev@xml.apache.org
> Subject: Re: creating element with ID in a document
>
>
> "Evert Haasdijk" <ev...@zukkespijkers.nl> writes:
>
>
> > 2. modify the xerces sources and add a 'setAttributeWithID'
> function. That's
> > what I did when I encoutered the same problem (in xerces 1.4). If
> > you want me to, I can send you the modifications I made, although I
> > did make them to v1.4, so I'm not sure they'll work with 1.5.
>
> Sounds like a useful feature. If you submit it to the list as a patch
> agains the 1.4 code, then made the Toronto team will accept them into
> 1.5.
>
> Tinny?
>
> jas.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
>
>

Well, I did post a message about this when I first made the mod, but I
didn't get any reaction then. But here's the diff, anyway.


cvs diff -r 1.1 DocumentImpl.cpp DocumentImpl.hpp DOM_Document.cpp
DOM_Document.hpp (in directory D:\USR\OmegaPlus\xerces-c-src1_4_0\src\dom)
Index: DocumentImpl.cpp
===================================================================
RCS file:
/group/cvs/kiq/OmegaPlus/xerces-c-src1_4_0/src/dom/DocumentImpl.cpp,v
retrieving revision 1.1
retrieving revision 1.3
diff -r1.1 -r1.3
58c58,69
<  * $Id: DocumentImpl.cpp,v 1.1 2001/04/05 09:01:11 evert Exp $
---
>  * $Id: DocumentImpl.cpp,v 1.3 2001/04/27 10:05:53 evert Exp $
>  */
>
> /* ====================================================================
>  * MODIFIED!!!
>  * ====================================================================
>  *
>  * EH: Modified importNode() to update fNodeIdMap when importing an ID
>  * attribute.
>  *
>  * EH: added DocumentImpl::createIDAttribute() to be able to create ID
>  * attributes that work without having to re-parse.
233,234d243
<
<
238a248,254
> // EH: added DocumentImpl::createIDAttribute()
> AttrImpl *DocumentImpl::createIDAttribute(const DOMString &nam)
> {
>     if (errorChecking && !isXMLName(nam)) {
>         throw
DOM_DOMException(DOM_DOMException::INVALID_CHARACTER_ERR,null);
>     }
>     AttrImpl* attr = new AttrImpl(this,nam);
239a256,273
>     if (attr)
>     {
>       //
>       // tell the attr that it's an ID
>       //
>       attr->isIdAttr(true);
>
>       //
>       // make sure that we have a NodeIDMap; it will be updated by
>       // AttrImpl::setValue(), so don't call fNodeIDMap->add(newattr)
>       //
>       if (fNodeIDMap == 0)
>                     fNodeIDMap = new NodeIDMap(500);
>     }
>
>     return attr;
> };
> // EH: end of DocumentImpl::createIDAttribute()
560a595,610
>
>             // EH: start importing ID
>             // Attributes of type ID.  If this is one, add it to the
hashtable of IDs
>             //   that is constructed for use by GetElementByID().
>             //
>             AttrImpl *newattr = (AttrImpl *) newnode;
>             if (attr->isIdAttr())
>             {
>                 newattr->isIdAttr(true);
>
>                 if (fNodeIDMap == 0)
>                     fNodeIDMap = new NodeIDMap(500);
>                 fNodeIDMap->add(newattr);
>             }
>             // EH: end importing ID
>
Index: DocumentImpl.hpp
===================================================================
RCS file:
/group/cvs/kiq/OmegaPlus/xerces-c-src1_4_0/src/dom/DocumentImpl.hpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -r1.1 -r1.2
59a60,68
> /* ====================================================================
>  * MODIFIED!!!
>  * ====================================================================
>  *
>  * EH: added DocumentImpl::createIDAttribute() to be able to create ID
>  * attributes that work without having to re-parse.
>  */
>
>
61c70
<  * $Id: DocumentImpl.hpp,v 1.1 2001/04/05 09:01:11 evert Exp $
---
>  * $Id: DocumentImpl.hpp,v 1.2 2001/04/27 10:05:53 evert Exp $
182a192,193
>     // EH: added createIDAttribute()
>     virtual AttrImpl            *createIDAttribute(const DOMString &name);
Index: DOM_Document.cpp
===================================================================
RCS file:
/group/cvs/kiq/OmegaPlus/xerces-c-src1_4_0/src/dom/DOM_Document.cpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -r1.1 -r1.2
58c58
<  * $Id: DOM_Document.cpp,v 1.1 2001/04/05 09:01:11 evert Exp $
---
>  * $Id: DOM_Document.cpp,v 1.2 2001/04/27 10:05:53 evert Exp $
60a61,67
> /* ====================================================================
>  * MODIFIED!!!
>  * ====================================================================
>  *
>  * EH: added DOM_Document::createIDAttribute() to be able to create ID
>  * attributes that work without having to re-parse.
>  */
187a195,200
>
> // EH: added DocumentImpl::createIDAttribute()
> DOM_Attr               DOM_Document::createIDAttribute(const DOMString
&name) {
>         return DOM_Attr(((DocumentImpl *)fImpl)->createIDAttribute(name));
> };
> // EH: end DocumentImpl::createIDAttribute()
Index: DOM_Document.hpp
===================================================================
RCS file:
/group/cvs/kiq/OmegaPlus/xerces-c-src1_4_0/src/dom/DOM_Document.hpp,v
retrieving revision 1.1
retrieving revision 1.2
diff -r1.1 -r1.2
58c58
<  * $Id: DOM_Document.hpp,v 1.1 2001/04/05 09:01:11 evert Exp $
---
>  * $Id: DOM_Document.hpp,v 1.2 2001/04/27 10:05:53 evert Exp $
60a61,68
> /* ====================================================================
>  * MODIFIED!!!
>  * ====================================================================
>  *
>  * EH: added createIDAttribute() to be able to create ID
>  * attributes that work without having to re-parse.
>  */
>
316a325,330
>
>     /** Same as createAttribute(), but the new attribute is an ID.
>      *
>      * <b> NON-STANDARD DOM Extension </b> by EH
>      */
>     DOM_Attr     createIDAttribute(const DOMString &name);



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: creating element with ID in a document

Posted by "Jason E. Stewart" <ja...@openinformatics.com>.
"Evert Haasdijk" <ev...@zukkespijkers.nl> writes:


> 2. modify the xerces sources and add a 'setAttributeWithID' function. That's
> what I did when I encoutered the same problem (in xerces 1.4). If
> you want me to, I can send you the modifications I made, although I
> did make them to v1.4, so I'm not sure they'll work with 1.5.

Sounds like a useful feature. If you submit it to the list as a patch
agains the 1.4 code, then made the Toronto team will accept them into
1.5.

Tinny?

jas.

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


RE: creating element with ID in a document

Posted by Evert Haasdijk <ev...@zukkespijkers.nl>.
Francois,

Alas, the internal map of Ids is only updated during a parse. AFAIK, you
have two options:

1. write your document to a stream after creating the attribute, parsing
that and from then on working with the newly parsed doc (by no means ideal,
but apparently the only way to do this in DOM level 2)
2. modify the xerces sources and add a 'setAttributeWithID' function. That's
what I did when I encoutered the same problem (in xerces 1.4). If you want
me to, I can send you the modifications I made, although I did make them to
v1.4, so I'm not sure they'll work with 1.5.

Bonne chance, Evert


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org