Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Reference

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.

[Script]

Script Syntax

ObjNSManager.declarePrefix(prefix, namespaceURI);

Parameters

prefix
The namespace prefix.
namespaceURI
The namespace URI.

Return Value

None.

Example

   var oNSMgr;
   
   try
   {
      oNSMgr = new ActiveXObject("Msxml2.MXNamespaceManager.5.0");
      oNSMgr.declarePrefix( "sample", "urn");
      oNSMgr.declarePrefix ("a", "abc");
      alert(oNSMgr.getDeclaredPrefixes.length); // 3
      alert(oNSMgr.getDeclaredPrefixes[0]); // "sample"
      alert(oNSMgr.getURI("sample")); // "urn"
      alert(oNSMgr.getPrefixes("urn")[0]); // "sample"
   } catch(e)
   {
      alert("Error \n" + e);
   }
[Visual Basic]

Visual Basic Syntax

ObjNSManager.declarePrefix(prefix, namespaceURI)

Parameters

prefix
The namespace prefix.
namespaceURI
The namespace URI.

Return Value

None.

Example

   Dim oNSMgr As New Msxml2.MXNamespaceManager50
   oNSMgr.declarePrefix "sample", "urn "
   oNSMgr.declarePrefix "a", "abc"
   MsgBox oNSMgr.getDeclaredPrefixes.length ' 3
   MsgBox oNSMgr.getDeclaredPrefixes.Item(0) ' "sample"
   MsgBox oNSMgr.getPrefixes("urn:some uri").Item(0) ' "sample"
   MsgBox oNSMgr.getURI("sample") ' "urn"
[C/C++]
HRESULT declarePrefix(
    BSTR prefix,
    BSTR namespaceURI);

Parameters

prefix[in]
The namespace prefix.
namespaceURI[in]
The namespace URI.

C/C++ Return Values

S_OK
The value returned if successful.
S_FALSE
The value returned if the prefix declared after the last pushContext and allowOverride property is VARIANT_TRUE (prefix is overridden).
E_FAIL
The value returned if the prefix declared after the last pushContext and 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::MXNamespaceManager50));
      PtrIMXNamespaceManager->declarePrefix(_T("sample"),_T("urn"));
      PtrIMXNamespaceManager->declarePrefix(_T("a"),_T("abc"));
      
      _itot(PtrIMXNamespaceManager->getDeclaredPrefixes()->length, _ 
                                                           Buffer, _
                                                     BufferLength);
      DisplayMessageToUser(Buffer);

      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 declarePrefix method replaces the previous declaration if a prefix has already been declared in the local context and allowOverride is enabled. 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.

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

allowOverride property

[Script,Visual Basic]

Applies to: IVBMXNamespaceManager

[C/C++]

Applies to: IMXNamespaceManager

Other Resources Other Resources

W3C Namespaces in XML Recommendation