You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Gary Pennington <Ga...@uk.sun.com> on 2000/11/14 18:35:02 UTC

Question regarding id attributes and DOM

Hi,

Firstly, I'm not sure if this is the right forum for this message, if it
isn't then apologies for intruding and any RTFMs or pointer to the
correct location would be appreciated.

I have a question about document validation and ID attributes using DOM.

Here's the scenario.

I have a document, I do a validating parse it and it's fine. I then
start to modify the document (accessing it via DOM) and insert new
elements. These elements contain ID attributes and I want to make sure
that I don't insert multiple elements containing the same ID. However,
if I invoke

getDocument().getElementById(name)

where getDocument is a method which returns the document which was
originally validated and name is the name of an element ID I inserted
SINCE the document was originally created after parsing, then I get null
returned. If the name is an element in the original xml file that was
parsed, then it is found and everything is okay.

More detail,

I add my new elements by cloning a "default" element and then modifiying
the ID attribute prior to insertion:-

        //Find the "magic" processor set
        Element magicProc=getDocument().getElementById("procSet-1");
        //clone it
        Element newSet=(Element)magicProc.cloneNode(false);
        //Modify it
        newSet.setAttribute("id", name);
        //Add to tree
        try {
            magicProc.getParentNode().insertBefore(newSet,magicProc);
            //Since identifier is introduced, must putIdentifier (a
bug?)
            ((DocumentImpl)getDocument()).putIdentifier(name, newSet);
        }
        catch(DOMException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        //update tree
        serialize();

In the code above, I have added a workaround which is to manually add
the identifiter into the document's set of identifiers. If I don't do
this workaround, then the element ID is not added to the document's set
of identifiers. The question I have is :-

Should an identifier be added to the document when the element is added
which contains an ID attribute or should I have to do my little
workaround as above? In other words am I struggling with undefined
behaviour, my lack of understanding of DOM or a bug?

I can't justify the overhead of re-parsing the document since I know
that the only thing modifiying the DOM is my program and I'm taking care
not to invalidate the document. Also, I have application specific data
saved as userData in the various nodes of the tree and it would be a
pain to copy that data around between documents following re-validation.

Yours,

Gary


Re: Question regarding id attributes and DOM

Posted by Elena Litani <hl...@jtcsv.com>.
Hi, Gary,

>Gary Pennington wrote:
>Should an identifier be added to the document when the element is added
>which contains an ID attribute or should I have to do my little
>workaround as above? In other words am I struggling with undefined
>behaviour, my lack of understanding of DOM or a bug?

As I understand the Dom's specs, this is not a bug and you should do
your workaround to make things work. I will try to clarify what I mean.

First of all, the major problem is that DOM core is not aware of
attribute types. (See DOM Level 2 Recommendation: "..Because the DOM
Core is not aware of attribute types, it treats all attribute values as
simple strings, even if the DTD or schema declares them as having
tokenized types". )

In current implementation when DOM parser parses elements and its
attributes from existing xml file, it handles attributes 'ID' for
elements encountered, by assuming that 'ID' is an attribute of type
'ID'. And it kinna "registers" ID attribute to the specific element.
Thus getElementByID would work on all attributes declared in xml
instance document. And thus, your "magic" workaround should also work!

However, if you start modifying your document - you don't really have a
way to specify the attribute 'type' (see DOM specs). You can only
specify 'name' and 'value' for new attribute.

If you read, specs for getElementByID () you will see the following
note:
"The DOM implementation must have information that says which attributes
are of type ID. Attributes with the name "ID" are not of type ID unless
so defined. Implementations that do not know whether attributes are of
type ID or not are expected to return null."

This note clearly states that  "Attributes with the name "ID" are NOT of
type ID unless so defined" and that "Implementations that do not know
whether attributes are of type ID or not are expected to return null".
That is why (in current Dom Implementation) you are getting null
returned. 



Hope it answers your questions.
Elena.

Gary Pennington wrote:
> 
> Hi,
> 
> Firstly, I'm not sure if this is the right forum for this message, if it
> isn't then apologies for intruding and any RTFMs or pointer to the
> correct location would be appreciated.
> 
> I have a question about document validation and ID attributes using DOM.
> 
> Here's the scenario.
> 
> I have a document, I do a validating parse it and it's fine. I then
> start to modify the document (accessing it via DOM) and insert new
> elements. These elements contain ID attributes and I want to make sure
> that I don't insert multiple elements containing the same ID. However,
> if I invoke
> 
> getDocument().getElementById(name)
> 
> where getDocument is a method which returns the document which was
> originally validated and name is the name of an element ID I inserted
> SINCE the document was originally created after parsing, then I get null
> returned. If the name is an element in the original xml file that was
> parsed, then it is found and everything is okay.
> 
> More detail,
> 
> I add my new elements by cloning a "default" element and then modifiying
> the ID attribute prior to insertion:-
> 
>         //Find the "magic" processor set
>         Element magicProc=getDocument().getElementById("procSet-1");
>         //clone it
>         Element newSet=(Element)magicProc.cloneNode(false);
>         //Modify it
>         newSet.setAttribute("id", name);
>         //Add to tree
>         try {
>             magicProc.getParentNode().insertBefore(newSet,magicProc);
>             //Since identifier is introduced, must putIdentifier (a
> bug?)
>             ((DocumentImpl)getDocument()).putIdentifier(name, newSet);
>         }
>         catch(DOMException e) {
>             e.printStackTrace();
>             System.exit(-1);
>         }
>         //update tree
>         serialize();
> 
> In the code above, I have added a workaround which is to manually add
> the identifiter into the document's set of identifiers. If I don't do
> this workaround, then the element ID is not added to the document's set
> of identifiers. The question I have is :-
> 
> Should an identifier be added to the document when the element is added
> which contains an ID attribute or should I have to do my little
> workaround as above? In other words am I struggling with undefined
> behaviour, my lack of understanding of DOM or a bug?
> 
> I can't justify the overhead of re-parsing the document since I know
> that the only thing modifiying the DOM is my program and I'm taking care
> not to invalidate the document. Also, I have application specific data
> saved as userData in the various nodes of the tree and it would be a
> pain to copy that data around between documents following re-validation.
> 
> Yours,
> 
> Gary
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org