Stylus Studio XML Editor

Table of contents

Appendices

B.2 Namespace Prefix Lookup

Namespace Prefix Lookup

The following describes in pseudo code the algorithm used in the lookupPrefix method of the Node interface. Before returning found prefix the algorithm needs to make sure that the prefix is not redefined on an element from which the lookup started. This methods ignores DOM Level 1 nodes.

NOTE: 

This method ignores all default namespace declarations. To look up default namespace use isDefaultNamespace method.

DOMString lookupPrefix(in DOMString namespaceURI)
{
  if (namespaceURI has no value, i.e. namespaceURI is null or empty string) {
     return null;
  }
  short type = this.getNodeType(); 
  switch (type) { 
        case Node.ELEMENT_NODE: 
        { 
             return lookupNamespacePrefix(namespaceURI, this); 
        } 
        case Node.DOCUMENT_NODE:
        { 
             return getDocumentElement().lookupNamespacePrefix(namespaceURI); 
        } 
        case Node.ENTITY_NODE : 
        case Node.NOTATION_NODE: 
        case Node.DOCUMENT_FRAGMENT_NODE: 
        case Node.DOCUMENT_TYPE_NODE: 
            return null;  // type is unknown  
        case Node.ATTRIBUTE_NODE:
        {
             if ( Attr has an owner Element ) 
             { 
                 return ownerElement.lookupNamespacePrefix(namespaceURI); 
             } 
             return null; 
        } 
        default:
        { 
           if (Node has an ancestor Element )
           // EntityReferences may have to be skipped to get to it 
           { 
                    return ancestor.lookupNamespacePrefix(namespaceURI); 
           } 
            return null; 
        } 
     } 
 } 


DOMString lookupNamespacePrefix(DOMString namespaceURI, Element originalElement){ 
        if ( Element has a namespace and Element's namespace == namespaceURI and 
             Element has a prefix and 
             originalElement.lookupNamespaceURI(Element's prefix) == namespaceURI) 
        { 
             return (Element's prefix); 
        } 
        if ( Element has attributes)
        { 
            for ( all DOM Level 2 valid local namespace declaration attributes of Element )
            {
                if (Attr's prefix == "xmlns" and 
                   Attr's value == namespaceURI and 
                   originalElement.lookupNamespaceURI(Attr's localname) == namespaceURI) 
                   { 
                      return (Attr's localname);
                   } 
            }
        } 

        if (Node has an ancestor Element ) 
           // EntityReferences may have to be skipped to get to it 
        { 
            return ancestor.lookupNamespacePrefix(namespaceURI, originalElement); 
        } 
        return null; 
    }