Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


XML is passing Schema validation when it shoudln't

From: Christopher <cpisz@------.--.--->
To: 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.



transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent