public String getNamespaceForPrefix ( String prefix , Element namespaceContext ) { int type ; Node parent = namespaceContext ; String namespace = null ; if ( prefix . equals ( "xml" ) ) { namespace = QName . S_XMLNAMESPACEURI ; // Hardcoded, per Namespace spec } else if ( prefix . equals ( "xmlns" ) ) { // Hardcoded in the DOM spec, expected to be adopted by // Namespace spec. NOTE: Namespace declarations _must_ use // the xmlns: prefix; other prefixes declared as belonging // to this namespace will not be recognized and should // probably be rejected by parsers as erroneous declarations. namespace = "http://www.w3.org/2000/xmlns/" ; } else { // Attribute name for this prefix's declaration String declname = ( prefix == "" ) ? "xmlns" : "xmlns:" + prefix ; // Scan until we run out of Elements or have resolved the namespace while ( ( null != parent ) && ( null == namespace ) && ( ( ( type = parent . getNodeType ( ) ) == Node . ELEMENT_NODE ) || ( type == Node . ENTITY_REFERENCE_NODE ) ) ) { if ( type == Node . ELEMENT_NODE ) { // Look for the appropriate Namespace Declaration attribute, // either "xmlns:prefix" or (if prefix is "") "xmlns". // TODO: This does not handle "implicit declarations" // which may be created when the DOM is edited. DOM Level // 3 will define how those should be interpreted. But // this issue won't arise in freshly-parsed DOMs. // NOTE: declname is set earlier, outside the loop. Attr attr = ( ( Element ) parent ) . getAttributeNode ( declname ) ; if ( attr != null ) { namespace = attr . getNodeValue ( ) ; break ; } } parent = getParentOfNode ( parent ) ; } } return namespace ; }
Given an XML Namespace prefix and a context in which the prefix is to be evaluated return the Namespace Name this prefix was bound to . Note that DOM Level 3 is expected to provide a version of this which deals with the DOM s early binding behavior .