Re: Getting document's root element

Steve,
If you have a pointer to any element "elt" in a document, you can do the
following to find the outermost 'svg':

if (elt.ownerSVGElement == null)
    outermostSVGElement = elt;
else {
    SVGElement ancestorSVG = elt;
    while (1) {
        SVGElement newAncestorSVG = ancestorSVG.ownerSVGElement;
       if (newAncestorSVG == null)
           break;
        else
           ancestorSVG = newAncestorSVG;
    }
    outermostSVGElement = ancestorSVG;
}

If you have a standalone SVG document, and you have a handle to the
SVGDocument element, then the outermost 'svg' is:

SVGSVGElement mySVGSVGElement = mySVGDocument.rootElement;
 

At 03:22 PM 7/13/00 -0700, Steve Dickson wrote:
>In the spec of 2000/03/03, SVGDocument::rootElement is defined in section
>6.10 as "the closest ancestor 'svg' element.  If this element is an
>outermost 'svg' element (i.e. either it is the root element of the
>document or if its parent is in a different namespace), then this
>attribute will be null".  

You have found an error in the spec here.

There is at most one Document object. If the XML file is contained within
<svg>...</svg>, then the root element will be an SVGSVGElement,and there
will be an SVGDocument object. Otherwise, such as when SVG is contained
within XHTML, the root element will be something else (e.g., an
HTMLHTMLElement), and there will be an HTMLDocument rather than an
SVGDocument (i.e., there will be no SVGDocument).
 
Thus, if there is an SVGDocument object, its "rootElement" attribute will
always point to the SVGSVGElement object which is the outermost 'svg' file.
No if's and's or but's.

The current wording on 'rootElement' is leftover from many months ago
before we sorted out the relationship between SVGDocument and
SVGSVGElement. What a good friend of mine calls a 'hysterical artifact'.

We will clean up the write-up on 'rootElement'. Sorry about the error.

>How then can the user get a handle on the
>document's root element?  I'm thinking of the situation where a user is
>accessing the SVG document using a script.  They can easily get a handle
>on the SVGDocument itself, 

So long as the file is an SVG file, not SVG embedded inside something else.

>but there doesn't seem to be a way to get a
>handle on the document's root element as an SVGSVGElement.  They could
>fake it by giving the root element a unique id and then calling
>getElementById(), or by traversing the tree using DOM calls, but both of
>these methods would return the root element as a DOM element, not as an
>SVGElement.  There must be a way to retrieve the root element of an
>SVGDocument as an SVGElement, but I can't find it.  Am I missing something
>really obvious?
>S.
> 

Jon Ferraiolo
SVG Editor
Adobe Systems Incorporated

Received on Friday, 14 July 2000 13:27:03 UTC