declarePrefix Method

 

Binds the namespace prefix with the namespace URI in the local context. You can set the allowOverride property to control whether duplicate prefixes are allowed in the local context.

JScript Syntax

ObjNSManager.declarePrefix(prefix, namespaceURI);  

Parameters

prefix
The namespace prefix.

namespaceURI
The namespace URI.

Return Value

None.

Example

var oNSMgr, str;  
  
try  
{  
   oNSMgr = new ActiveXObject("Msxml2.MXNamespaceManager.6.0");  
   oNSMgr.declarePrefix( "sample", "urn");  
   oNSMgr.declarePrefix ("a", "abc");  
  
   str = "There are " + oNSMgr.getDeclaredPrefixes.length + " prefixes.\n\n";   
  
   // For each declared prefix:  
   for (var i=0; i < oNSMgr.getDeclaredPrefixes.length; i++) {  
  
      // Get the declared prefix.  
      var sPrefix = oNSMgr.getDeclaredPrefixes[i];  
  
      // Use declared prefix to lookup its URI.  
      var sUri = oNSMgr.getURI(sPrefix);  
  
      // Use URI to do a reverse lookup of prefix.  
      var sPrefixRevLookup = oNSMgr.getPrefixes(sUri)[0];   
  
     // Append current entry to output string.  
      str = str + "Prefix [" + i + "]: " + sPrefix + "\n";  
      str = str + "URI [" + i + "]: " + sUri + "\n";  
      str = str + "Prefix for '" + sUri + "' is '" + sPrefixRevLookup + "'.\n\n";  
  
   } // end for   
  
   // Display output.  
   WScript.Echo(str);  
  
} catch(e)  
  
{  
   WScript.Echo("Error \n" + e);  
}  

Output

There are 3 prefixes.  
  
Prefix [0]: sample  
URI [0]: urn  
Prefix for 'urn' is 'sample'.  
  
Prefix [1]: a  
URI [1]: abc  
Prefix for 'abc' is 'a'.  
  
Prefix [2]: xml  
URI [2]: http://www.w3.org/XML/1998/namespace  
Prefix for 'http://www.w3.org/XML/1998/namespace' is 'xml'.  

C++ Syntax

HRESULT declarePrefix(  
    BSTR prefix,  
    BSTR namespaceURI);  

Parameters

prefix[in]
The namespace prefix.

namespaceURI[in]
The namespace URI.

Return Values

S_OK
The value returned if successful.

S_FALSE
The value returned if the prefix has already been declared in the local context, and the allowOverride property is VARIANT_TRUE (prefix is overridden).

E_FAIL
The value returned if the prefix has already been declared in the local context, and the allowOverride property is VARIANT_FALSE (prefix is not overridden).

E_INVALIDARG
The value returned if one or more of the following is true: if the namespace URI is empty, but the but prefix is not; if the prefix is invalid "xml" or "xmlns"; if the prefix is not a non-colon XML name (NCName), as defined by the W3C Namespaces in XML Recommendation.

Example

void SampleMXNameSpaceMgr_Prefixs()
{
   MSXML2::IMXNamespaceManagerPtr PtrIMXNamespaceManager;
   HRESULT hres;
   TCHAR Buffer[100];
   unsigned short WideCharBuffer[100];
   int BufferLength = 100;

   try
   {

   PtrIMXNamespaceManager.CreateInstance _
                          (__uuidof(MSXML2::MXNamespaceManager60));
      PtrIMXNamespaceManager->declarePrefix(_T("sample"),_T("urn"));
      PtrIMXNamespaceManager->declarePrefix(_T("a"),_T("abc"));

      BufferLength = 100;
      PtrIMXNamespaceManager->getDeclaredPrefix(2, WideCharBuffer, 
                              &BufferLength);
      DisplayMessageToUser(_bstr_t(WideCharBuffer));

      BufferLength = 100;
      PtrIMXNamespaceManager->getPrefix(L"urn", 1, WideCharBuffer, 
                                        &BufferLength);
      DisplayMessageToUser(_bstr_t(WideCharBuffer));

      BufferLength = 100;
      hres = PtrIMXNamespaceManager->getURI(L"sample", NULL, 
                                            WideCharBuffer,  
                                            &BufferLength);
      DisplayMessageToUser(_bstr_t(WideCharBuffer));
   }
   catch(...)
   {
      DisplayMessageToUser("Error");
   }
}

void DisplayMessageToUser(char *Msg)
{
   ::MessageBox(NULL, Msg, _T("Message"), MB_OK);
}

Remarks

The allowOverride property determines how the declarePrefix method handles duplicate prefixes. If allowOverride is set to False, the prefix is not overwritten. Otherwise, the prefix is overwritten in the local context.

The namespaceURI parameter should not be empty, unless it is used to redefine the default namespace with a namespace that contains an empty prefix. For example, the following is permitted:

nm.declarePrefix " "," "  

But the following is not:

nm.declarePrefix "a"," "  

The invalid prefixes "xml" and "xmlns" cannot be passed to this method.

An error is raised if the prefix is invalid, or if the namespace URI is empty, but its prefix is not.

Versioning

Implemented in:

MSXML 6.0

Applies to

IVBMXNamespaceManager

IMXNamespaceManager

See Also

allowOverride Property