Freigeben über


schemas Property

 

Finds schema documents during load.

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.3.0");
var SchemaCache = new ActiveXObject("Msxml2.XMLSchemaCache.3.0");

xmldoc.async = false;
xmldoc.validateOnParse = false;
SchemaCache.add("", "bookschema.xdr");
xmldoc.schemas = SchemaCache;
// The document will load only if a valid schema is attached to the xml
// file.
xmldoc.load("books.xml");
if (xmldoc.parseError.errorCode != 0) {
   var myErr = xmldoc.parseError;
   WScript.Echo("You have error " + myErr.reason);
} else {
   WScript.Echo(xmldoc.xml) ;
}

Visual Basic Syntax

Set objXMLDOMSchemaCollection = objIXMLDOMDocument2.schemas  
objXMLDOMDocument.schemas = objXMLDOMSchemaCollection  

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_schemasonly)
The value returned if no schema collection is set.

E_POINTER (for get_schemasonly)
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

The following example works with MSXML 3.0. It uses XDR schemas, which are not supported in MSXML 6.0.

#include "stdio.h"

#import <msxml3.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 schema file
        IXMLDOMDocumentPtr pSchemaDoc;
        hr = pSchemaDDoc.CreateInstance(__uuidof(DOMDocument30));
        pSchemaDDoc->async =  VARIANT_FALSE; 
        
        hr = pSchemaDDoc->load("bookschema.xdr");
        
        //check on the parser error        
        if(hr!=VARIANT_TRUE)
        {            
            return checkParseError(pSchemaDDoc->parseError);
        }
        
        //create schemachache
        IXMLDOMSchemaCollectionPtr pSchemaCache;
        hr = pSchemaCache.CreateInstance(__uuidof(XMLSchemaCache30));

        //add schema to schema cache
        hr = pSchemaCache->add("",pSchemaDDoc.GetInterfacePtr());

        // load the XML file
        // ****** you need to use IXMLDOMDocument2 interface *********
        IXMLDOMDocument2Ptr pXMLDoc;
        hr = pXMLDoc.CreateInstance(__uuidof(DOMDocument30));
        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("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);
}

XML Data File: bookschema.xml

The C\C++ sample uses the following XML file.

<?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>

Schema File: bookschema.xdr

The C\C++ file uses the following XDR schema.

<?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 re-enables DTD processing.

Versioning

Implemented in:

MSXML 3.0, MSXML 6.0

Applies to

IXMLDOMDocument2

See Also

IXMLDOMSchemaCollection-XMLSchemaCache
load Method1
loadXML Method1
validate Method1