schemas Property

MSXML 5.0 SDK

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

schemas Property

Finds schema documents during load.

[Script]

Script Syntax

var objXMLDOMSchemaCollection = objIXMLDOMDocument2.schemas;
objXMLDOMDocument.schemas = objXMLDOMSchemaCollection;

Example

The following script example attaches a schema to an XML document.

var xmldoc = new ActiveXObject("Msxml2.FreeThreadedDOMDocument.5.0");
var SchemaCache = new ActiveXObject("Msxml2.XMLSchemaCache.5.0");

xmldoc.async = false;
xmldoc.validateOnParse = false;
SchemaCache.add("x-schema:books", "c:\\books.xsd");
xmldoc.schemas = SchemaCache;
// The document will load only if a valid schema is attached to the xml
// file.
xmldoc.load("c:\\books.xml");
if (xmlDoc.parseError.errorCode <> 0) {
   var myErr = xmlDoc.parseError;
   alert("You have error " + myErr.reason);
} else {
   alert(xmldoc.xml) ;
}
[Visual Basic]

Visual Basic Syntax

Set objXMLDOMSchemaCollection = objIXMLDOMDocument2.schemas
objXMLDOMDocument.schemas = objXMLDOMSchemaCollection
[C/C++]

C/C++ Syntax

HRESULT get_schemas (VARIANT* otherCollection);
HRESULT putref_schemas (VARIANT otherCollection);

Parameters

otherCollection [out, retval][in]
The schema collection that is returned. This is the same object that was previously set, or Null if none has been set.

C/C++ Return Values

S_OK
The value returned if the method executes successfully and a schema collection is set.
S_FALSE (for get_schemas only)
The value returned if no schema collection is set.
E_POINTER (for get_schemas only)
The value returned if pSchemaCollection is Null.
E_FAIL (for putref_schemas only)
The value returned if an IXMLSchemaCollection interface cannot be obtained from SchemaCollection with formatted IErrorInfo.

Null if no schema collection is currently set. You will always get the same collection object you enter.

Example

#include "stdio.h"

#import <msxml5.dll>
using namespace MSXML2;

int checkParseError(IXMLDOMParseErrorPtr pError);
void dump_com_error(_com_error &e);

int main(int argc, char* argv[])
{
   
   CoInitialize(NULL);
   HRESULT hr;

   try{
      
      IXMLDOMParseErrorPtr  pError;
      
            
      //load the XSD file
      IXMLDOMDocumentPtr pXSDDoc;
      hr = pXSDDoc.CreateInstance(__uuidof(DOMDocument50));
      pXSDDoc->async =  VARIANT_FALSE; 
      
      hr = pXSDDoc->load("c:\\temp\\bookschema.xdr");
      
      //check on the parser error      
      if(hr!=VARIANT_TRUE)
      {         
         return checkParseError(pXSDDoc->parseError);
      }
      
      //create schemachache
      IXMLDOMSchemaCollectionPtr pSchemaCache;
      hr = pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache50));

      //add schema to schem cache
      hr = pSchemaCache->add("x-schema:books", pXSDDoc.GetInterfacePtr());   

      // load the XML file
      // ****** you need to use IXMLDOMDocument2 interface *********
      IXMLDOMDocument2Ptr pXMLDoc;
      hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument50));
      pXMLDoc->validateOnParse = VARIANT_FALSE;
      pXMLDoc->async =  VARIANT_FALSE; 

      //associate xml doc with schemacache
      pXMLDoc->schemas = pSchemaCache.GetInterfacePtr();
      
      // relative path works in debugger, modify it with absolute path as you need
      hr = pXMLDoc->load("c:\\temp\\booksample.xml");      
      
      //check on the parser error      
      if(hr!=VARIANT_TRUE)
      {
         return checkParseError(pXMLDoc->parseError);
      }

      //call validate 
      pError = pXMLDoc->validate();
      
      if(pError->errorCode != S_OK)
      {
         _bstr_t parseError = _bstr_t("Error code: ")+ _bstr_t(pError->errorCode) +_bstr_t("\n") + _bstr_t("Reason: ")+ pError->Getreason();
         MessageBox(NULL, (char*)parseError, "Parse Error",MB_OK);
         return -1;
      }
      else
         MessageBox(NULL,"Valiation succeeded", "Results",MB_OK);
   
   }
   catch(_com_error &e)
   {
      dump_com_error(e);
   }
   return 0;
}

int checkParseError(IXMLDOMParseErrorPtr pError)
{
   _bstr_t parseError =_bstr_t("At line ")+ _bstr_t(pError->Getline()) + _bstr_t("\n")+ _bstr_t(pError->Getreason());
   MessageBox(NULL,parseError, "Parse Error",MB_OK);
   return -1;
   
}

void dump_com_error(_com_error &e)
{
   printf("Error\n");
   printf("\a\tCode = %08lx\n", e.Error());
   printf("\a\tCode meaning = %s", e.ErrorMessage());
   _bstr_t bstrSource(e.Source());
   _bstr_t bstrDescription(e.Description());
   printf("\a\tSource = %s\n", (LPCSTR) bstrSource);
   printf("\a\tDescription = %s\n", (LPCSTR) bstrDescription);
}

File Name: c:\temp\booksample.xml

<?xml version='1.0'?>
<COLLECTION xmlns="x-schema:books"
   xmlns:dt="urn:schemas-microsoft-com:datatypes">
   <DATE dt:dt="datetime">1998-10-13T15:56:00</DATE>
   <BOOK>
      <TITLE>Lover Birds</TITLE>
      <AUTHOR>Cynthia Randall</AUTHOR>
      <PUBLISHER>Lucerne Publishing</PUBLISHER>
   </BOOK>
   <BOOK>
      <TITLE>The Sundered Grail</TITLE>
      <AUTHOR>Eva Corets</AUTHOR>
      <PUBLISHER>Lucerne Publishing</PUBLISHER>
   </BOOK>
   <BOOK>
      <TITLE>Splish Splash</TITLE>
      <AUTHOR>Paula Thurman</AUTHOR>
      <PUBLISHER>Scootney</PUBLISHER>
   </BOOK>
</COLLECTION>

File Name: c:\temp\bookschema.xdr

<?xml version="1.0"?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data">
   <ElementType name="TITLE" />
   <ElementType name="AUTHOR" />
   <ElementType name="PUBLISHER" />
   <ElementType name="DATE" />
   <ElementType name="BOOK" model="closed">
      <element type="TITLE" />
      <element type="AUTHOR" />
      <element type="PUBLISHER" />
   </ElementType>
   <ElementType name="COLLECTION" model="closed">
      <element type="BOOK" />
   </ElementType>
</Schema>

Remarks

The schemas property provides a way to associate preloaded schemas with any namespace. Set the schemas property before loading the document to identify which schemas are used by the document. The schemas property provides a way to override the existing schemas that are used by the document. Setting a new schema collection has no effect on the current document until the next load, loadXML, or validate call. The schemas loaded by the document during load are not added automatically to this collection.

Setting any non-Null schema collection automatically disables document type definition (DTD) processing because you cannot use both DTD and XML schema processing on the same document. This means DTDs will be ignored. Setting the schemas collection to Null reenables DTD processing.

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

IXMLDOMSchemaCollection/XMLSchemaCache | load Method | loadXML Method | validate Method

Applies to: IXMLDOMDocument2