This is Interesting: Free Magazines for Graphics designers and webmasters
Home > Archive > Microsoft XML > March 2007 > TargetNamespace not getting validated
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
TargetNamespace not getting validated
|
|
|
| Hello Guys,
I am pasting a code which I have written using MSXML6.0 in C++ for XML
Schema Validation
I need to know as to why this does not fail for an incorrect targetnamespace
inside the XML file.
//Code
int Validate(wchar_t* xml,wchar_t* xsd,wchar_t* targetns)
{
HRESULT hr = S_OK;
if(xml && xsd)
{
IXMLDOMDocument* pXml = NULL,*pXsd = NULL;
IXMLDOMElement* pXsdRoot = NULL;
IXMLDOMSchemaCollection* pSchemaCache = NULL;
IXMLDOMDocument2* pXml2 = NULL;
IXMLDOMParseError *pErr = NULL;
VARIANT vSchemas1,vSchemas2,vXMLFile,vXSDFile;
VariantInit(&vSchemas1);//VariantClear can release the XsdRoot
VariantInit(&vSchemas2);
VariantInit(&vXMLFile);
VariantInit(&vXSDFile);
hr = CoCreateInstance(CLSID_DOMDocument60,
NULL,
CLSCTX_INPROC_SERVER,
IID_IXMLDOMDocument,
(void**)&pXml);
if(FAILED(hr) || !pXml)
goto Clean;
hr = CoCreateInstance(CLSID_DOMDocument60,
NULL,
CLSCTX_INPROC_SERVER,
IID_IXMLDOMDocument,
(void**)&pXsd);
if(FAILED(hr) || !pXsd)
goto Clean;
//now both interfaces found...
//lets load the xsd first
hr = pXsd->put_async(VARIANT_FALSE);
if(FAILED(hr))
goto Clean;
//load the XSD.
VARIANT_BOOL bSuccess = FALSE;
BSTR bXSDFile = SysAllocString(xsd);
vXSDFile.vt = VT_BSTR;
vXSDFile.bstrVal = bXSDFile;
hr = pXsd->load(vXSDFile,&bSuccess);
if(FAILED(hr))
goto Clean;
if(bSuccess == VARIANT_FALSE)
goto Clean;
hr = pXsd->get_documentElement(&pXsdRoot);
if(FAILED(hr)||!pXsdRoot)
goto Clean;
//Create a Schema Collection which you can associate to XML
hr = CoCreateInstance(CLSID_XMLSchemaCache60,
NULL,
CLSCTX_SERVER,
IID_IXMLDOMSchemaCollection,
(void**)&pSchemaCache);
if(FAILED(hr)||!pSchemaCache)
goto Clean;
vSchemas1.vt = VT_DISPATCH;
vSchemas1.punkVal = pXsdRoot;
BSTR x = SysAllocString(targetns);
hr = pSchemaCache->add(x,vSchemas1);
if(FAILED(hr))
goto Clean;
hr = pXml->QueryInterface(IID_IXMLDOMDocument2,(void**)&pXml2);
if(FAILED(hr)||!pXml2)
goto Clean;
vSchemas2.vt = VT_DISPATCH;
vSchemas2.punkVal = pSchemaCache;
hr = pXml2->putref_schemas(vSchemas2);
if(FAILED(hr)) goto Clean;
BSTR bXMLFile = SysAllocString(xml);
vXMLFile.vt = VT_BSTR;
vXMLFile.bstrVal = bXMLFile;
hr = pXml->load(vXMLFile,&bSuccess);
if(SUCCEEDED(hr) && bSuccess == VARIANT_TRUE)
{
wprintf(L"***XML %s Valid w.r.t %s\n",xml,xsd);
}
else
{
hr = pXml->get_parseError(&pErr);
if(FAILED(hr) && !pErr)
goto Clean;
DisplayError(pErr);
}
Clean:
pErr && pErr->Release();
VariantClear(&vSchemas2);
pXml2 && pXml2->Release();
SysFreeString(x);
VariantClear(&vSchemas1);
pXsd && pXsd->Release();
pXml && pXml->Release();
}
return (int)hr;
}
Thanks in Advance,
Naren.
| |
| Martin Honnen 2007-03-30, 7:17 pm |
| Naren wrote:
> I am pasting a code which I have written using MSXML6.0 in C++ for XML
> Schema Validation
> I need to know as to why this does not fail for an incorrect targetnamespace
> inside the XML file.
If you author a schema for a certain namespace (e.g.
http://example.com/2007/ns1) and add that to the schema cache, then try
to validate a document with the root element being in another namespace
(e.g. http://example.com/2007/ns2) the parser is supposed to look for a
schema for that namespace but does not find one. It then is supposed to
parse in so called lax validation mode meaning it will not attempt to
validate elements for which it does not find a schema definition.
So what happens is that MSXML looks at the namespace of the root element
and does not find a schema for that. That is not an error in terms of
the schema validation process. Some parsers (e.g. XmlReader in .NET 2.0)
emit a warning in that case that no schema was found but I don't think
MSXML 6 does that, it does not have a mechanism to emit warnings.
If you expect to be passed XML documents where the namespace is wrong
then consider to check that in your application, simply compare (JScript
notation)
xmlDocument.documentElement.namespaceURI
against
schemaDocument.documentElement.getAttribute('targetNamespace')
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
|
|
|
| | Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |
|