Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries [Thread Prev] >Thread Next - Re: XML is passing Schema validation when it shoudln't XML is passing Schema validation when it shoudln'tTo: NULL Date: 11/2/2008 4:39:00 PM I don't understand why my XML doc is passing validation when it
shoudln't.
I also don't understand these namespaces and why some examples use one
or another.
I just want to dictate the variable types that are allowed in
different fields and I can't seem to accomplish that.
---------------------------------------------------------------------------
schema:
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:dt='urn:schemas-microsoft-com:datatypes'>
<xsd:element name="economy">
<xsd:complexType>
<xsd:sequence>
<!-- Good Elements -->
<xsd:element name="good" minOccurs="1"
maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" dt:type="string"/>
<xsd:element name="value" dt:type="float"/>
<xsd:element name="tech" dt:type="i4"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
--------------------------------------------------------------------
xml document:
<?xml version="1.0" encoding="utf-8"?>
<economy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:goods_schema.xsd">
<!-- Goods
name - Name of the good
value - Base monetary value of the good. This value is modified by
other factors to calculate a buy and sell price
tech - Required tech level for a planet to produce this good
-->
<good>
<name>Energy</name>
<value>12</value>
<tech>1</tech>
</good>
<good>
<name>Ice</name>
<value>24</value>
<tech>this_should_give_an_error</tech>
</good>
<good>
<name>Water</name>
<value>30</value>
<tech>1</tech>
</good>
</economy>
-----------------------------------------------------------
code:
DataManager::DataManager(std::string dataXMLFilePath, std::string
dataXMLSchemaPath)
{
// NOTE - Be careful with these not so smart pointers that MS gave
us.
// They will not release on thier own after CoUninitialize
has been called.
// It is better to release them yourself. I just use them
because the syntax
// is tons better than thier raw interface.
// Create a DOM instance
MSXML2::IXMLDOMDocument3Ptr xmlDoc = NULL;
MSXML2::IXMLDOMDocument2Ptr xsdDoc = NULL;
MSXML2::IXMLDOMSchemaCollectionPtr schemaCollection = NULL;
MSXML2::IXMLDOMParseErrorPtr parseError = NULL;
MSXML2::IXMLDOMNodePtr node = NULL;
MSXML2::IXMLDOMNodeListPtr nodeList = NULL;
BaseException error("",
"DataManager::DataManager(std::string
dataXMLFilePath)",
"DataManager.cpp");
try
{
CoInitialize(NULL);
// Create an interface to the the XML document
if( FAILED(xmlDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60))) )
{
error.m_msg = "Failed to instantiate MSXML's DOMDocument60
object.";
throw error;
}
xmlDoc->async = VARIANT_FALSE;
xmlDoc->validateOnParse = VARIANT_FALSE;
if( xmlDoc->load(dataXMLFilePath.c_str()) != VARIANT_TRUE )
{
error.m_msg = "Failed to load xml document " +
dataXMLFilePath + ".";
throw error;
}
// Create an interface to the XSD schema document
if( FAILED(xsdDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60))) )
{
error.m_msg = "Failed to instantiate MSXML's DOMDocument60
object.";
throw error;
}
xsdDoc->async = VARIANT_FALSE;
xsdDoc->validateOnParse = VARIANT_FALSE;
if( xsdDoc->load(dataXMLSchemaPath.c_str()) != VARIANT_TRUE )
{
error.m_msg = "Failed to load xsd document " +
dataXMLSchemaPath + ".";
throw error;
}
// Create a schema cache instance.
if( FAILED(schemaCollection.CreateInstance(__uuidof(MSXML2::XMLSchemaCache60))) )
{
error.m_msg = "Failed to create schema collection object.";
throw error;
}
// Add the loaded schema definition to the schema collection.
if( FAILED(schemaCollection->add("",xsdDoc.GetInterfacePtr())) )
{
error.m_msg = "Could not add schema to xml document interface
" + dataXMLSchemaPath + ".";
throw error;
}
// Associate the schema collection with the XML document
xmlDoc->schemas = schemaCollection.GetInterfacePtr();
// Validate the xml document
parseError = xmlDoc->validate();
if( parseError->errorCode != 0 )
{
error.m_msg = "XML document: " + dataXMLFilePath + "\n";
error.m_msg += "Schema: " + dataXMLSchemaPath + "\n";
error.m_msg += "Did not pass validation: " + parseError-
>Getreason();
throw error;
}
-----------------------------------------------------------
My check at the end does not throw an error, even though I have a tech
element with letters in it instead of a number.
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
