addCollection Method

 

[This feature was implemented only for MSXML 6.0.]

Adds schemas from another collection into the current collection and replaces any schemas that collide on the same namespace URI.

JScript Syntax

objXMLDOMSchemaCol.addCollection(objXMLDOMSchemaCollection);  

Parameters

objXMLDOMSchemaCollection
The collection containing the schemas to add.

Example

var xmldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.6.0");  
var SchemaCache = new ActiveXObject("Msxml2.XMLSchemaCache.6.0");  
var SchemaCache2 = new ActiveXObject("Msxml2.XMLSchemaCache.6.0");  
  
xmldoc.async = false;  
xmldoc.validateOnParse = false;  
SchemaCache.add("x-schema:books", "collection.xsd");  
SchemaCache2.addCollection(SchemaCache);  
SchemaCache2.add("x-schema:books", "NewBooks.xsd");  
xmldoc.schemas = SchemaCache2;  
// The document will load only if a valid schema is attached to the xml  
// file. The new schema will override the old one  
xmldoc.load("collection.xml");  
if (xmldoc.parseError.errorCode != 0) {  
   var myErr = xmldoc.parseError;  
   WScript.Echo("You have error " + myErr.reason);  
} else {  
   WScript.Echo(xmldoc.xml) ;  
}  

C/C++ Syntax

HRESULT addCollection(IXMLDOMSchemaCollection * otherCollection);  

Parameters

otherCollection[in]
The collection containing the schemas to add.

C/C++ Return Values

S_OK
The value returned if the collection is added successfully.

E_FAIL is returned if:

  • The collection is read-only.

  • The document is not a recognized schema.

  • An error occurs when compiling the schema.

  • The ready state of the document is not 4.

E_POINTER
The value returned if input pointer is invalid.

Example

#include "msxml6.h"  
  
void AddCollectionSample();  
  
int APIENTRY _tWinMain(HINSTANCE hInstance,  
                     HINSTANCE hPrevInstance,  
                     LPTSTR    lpCmdLine,  
                     int       nCmdShow)  
{  
    if(SUCCEEDED(::CoInitialize(NULL)) {  
       AddCollectionSample();  
       ::CoUninitialize();  
   }  
    return 0;  
}  
  
void AddCollectionSample()  
{  
    IXMLDOMDocument2Ptr pIXMLDOMDocument2;  
    IXMLDOMSchemaCollection2Ptr pIXMLDOMSchemaCollection2Ptr;  
    IXMLDOMSchemaCollection2Ptr pIXMLDOMSchemaCollection2Ptr2;  
    int nResult;  
  
    try  
    {  
    // Create the DOM  
        nResult = pIXMLDOMDocument2.CreateInstance(__uuidof(MSXML2::DOMDocument60));  
        (nResult == 0) ? 0: throw nResult;  
  
    // Create the Schema Collections  
        nResult = pIXMLDOMSchemaCollection2Ptr.CreateInstance(__uuidof(MSXML2::XMLSchemaCache60));  
        (nResult == 0) ? 0: throw nResult;  
        nResult = pIXMLDOMSchemaCollection2Ptr2.CreateInstance(__uuidof(MSXML2::XMLSchemaCache60));  
        (nResult == 0) ? 0: throw nResult;  
  
    // Add the schema to the collection  
        nResult = pIXMLDOMSchemaCollection2Ptr->add(_T("x-schema:books"), _T("collection.xsd"));  
        (nResult == 0) ? 0: throw nResult;  
  
    // attach all schemas from "pIXMLDOMSchemaCollection2Ptr"  
        nResult = pIXMLDOMSchemaCollection2Ptr2->addCollection(pIXMLDOMSchemaCollection2Ptr.GetInterfacePtr());  
        (nResult == 0) ? 0: throw nResult;  
  
    // override the old schema  
        nResult = pIXMLDOMSchemaCollection2Ptr2->add(_T("x-schema:books"), _variant_t(_T("Newbooks.xsd")));  
        (nResult == 0) ? 0: throw nResult;  
  
    // Attach schemas  
        pIXMLDOMDocument2->schemas = pIXMLDOMSchemaCollection2Ptr2.GetInterfacePtr();  
  
        pIXMLDOMDocument2->async = false;  
        pIXMLDOMDocument2->validateOnParse = true;  
  
    // Load the document into the DOM  
        nResult = pIXMLDOMDocument2->load(_T("collection.xml"));  
        (nResult == -1) ? 0: throw nResult;  
  
        ::MessageBox(NULL, pIXMLDOMDocument2->xml, _T("Loaded Document"), MB_OK);  
    } catch(...)  
    {  
        ::MessageBox(NULL, _T("Sample Failed"), _T("Error"), MB_OK);  
    }  
}  

Resource Files

The examples in this topic use the following files.

collection.xml

<?xml version='1.0'?>  
<Collection xmlns="x-schema:books">  
   <Book>  
      <Title>Lover Birds</Title>  
      <Author>Cynthia Randall</Author>  
      <Publisher>Lucerne Publishing</Publisher>  
   </Book>  
</Collection>  

collection.xsd

<?xml version="1.0" encoding="UTF-8"?>  
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">  
   <xs:element name="Collection1">  
      <xs:complexType>  
     <xs:sequence>  
        <xs:element name="OtherBook">  
           <xs:complexType>  
          <xs:sequence>  
            <xs:element name="Title" type="xs:string"/>  
            <xs:element name="Author" type="xs:string"/>  
            <xs:element name="Publisher" type="xs:string"/>  
          </xs:sequence>  
           </xs:complexType>  
        </xs:element>  
     </xs:sequence>  
      </xs:complexType>  
   </xs:element>  
</xs:schema>  

NewBooks.xsd

<?xml version="1.0" encoding="UTF-8"?>  
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">  
   <xs:element name="Collection">  
      <xs:complexType>  
     <xs:sequence>  
        <xs:element name="Book">  
           <xs:complexType>  
          <xs:sequence>  
            <xs:element name="Title" type="xs:string"/>  
            <xs:element name="Author" type="xs:string"/>  
            <xs:element name="Publisher" type="xs:string"/>  
          </xs:sequence>  
           </xs:complexType>  
        </xs:element>  
     </xs:sequence>  
      </xs:complexType>  
   </xs:element>  
</xs:schema>  

Output

The examples in this topic output the following in a message box.

<?xml version='1.0'?>  
<Collection xmlns="x-schema:books">  
  <Book>  
    <Title>Lover Birds</Title>  
      <Author>Cynthia Randall</Author>  
      <Publisher>Lucerne Publishing</Publisher>  
  </Book>  
</Collection>  

Remarks

There is no guarantee that the two collections will return exactly the same schema. The collection is free to clone them, if necessary.

Adding a collection to itself has no effect.

Remarks

In previous versions of MSXML, a schema already in the cache for a specified namespace was replaced by the schema from the new location. In MSXML6.0, when you call the addCollection method the declarations are merged with an existing schema with the same namespace. Using this feature, MSXML 6.0 supports "partial schemas". You can load several schemas, all with the same target namespace, into one schema in the schema cache.

Calling this method will cause all the schemas imported by the added schemas to also be added into the cache as "top-level" schemas.

Schema imports are considered to be "lax" - this means that any namespace or type already added to the schema cache can be referenced by another schema in the cache even if there is no explicit import in the referencing schema. You must set validateOnLoad Property to false to avoid issues around the order of calls to this method.

The add operation is atomic. All the schemas must be added successfully to the cache, or else none are.

MSXML 6.0 has removed support for XDR schemas, whereas XDR is supported in MSXML 3.0. If this method is called with an XDR schema, the call will fail.

Versioning

Implemented in: MSXML 6.0

Applies to

IXMLDOMSchemaCollection-XMLSchemaCache