Problems with Schema Collection and Namespaces

Unanswered Problems with Schema Collection and Namespaces

  • Friday, October 15, 2010 8:22 PM
     
     

    Our company is going to start exchanging XML documents and I'm trying to understand how to correctly use XML data types in SQL Server 2005.

    There is a published xsd which I think I'm supposed to store in a Schema Collection so that Sql Server can use it to validate typed XML variables and columns.
    There also are some examples XML documents available for testing.

    Because the xsd and the samples are relatively huge (1-2 megabytes each),  I have distilled both down to the minimum necessary fields both for my own sanity while testing and for use in examples to forums such as this.

    I believe I am down to my last problem which centers on understanding namespaces.

    The actual XML documents do not and will not have any namespace parameters within them.
    But I am only able to succesfully validate my testing samples when I include an xmlns parameter.
    What am I doing wrong?
    How can I get a sample without an xmlns parameter to successfully validate?

    Here is what I have:
    [code]
    USE LSExport
    GO

    IF EXISTS (SELECT * FROM sys.xml_schema_collections WHERE [name] = 'MyPrivateSchemaCollection')
     DROP XML SCHEMA COLLECTION dbo.MyPrivateSchemaCollection
    GO

    DECLARE @testSchema XML
    SET @testSchema =
    '<?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema  xmlns="http://www.mismo.org/residential/2009/schemas"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        attributeFormDefault="unqualified"
        elementFormDefault="qualified"
        targetNamespace="http://www.mismo.org/residential/2009/schemas">
     <xsd:element name="MESSAGE"
         type="MESSAGE" />
     <xsd:complexType name="ABOUT_VERSION">
      <xsd:sequence>
       <xsd:element minOccurs="0"
           name="AboutVersionIdentifier"
           type="MISMOIdentifier">
       </xsd:element>
      </xsd:sequence>
     </xsd:complexType>
     <xsd:complexType name="MESSAGE">
      <xsd:sequence>
       <xsd:element minOccurs="0"
           name="ABOUT_VERSION"
           type="ABOUT_VERSION">
          </xsd:element>
      </xsd:sequence>
      <xsd:attribute name="MISMOReferenceModelIdentifier"
          type="xsd:string">
      </xsd:attribute>
     </xsd:complexType>
     <xsd:complexType name="MISMOIdentifier">
      <xsd:simpleContent>
       <xsd:extension base="xsd:string">
        <xsd:attribute name="IdentifierOwnerURI"
            type="xsd:anyURI">
        </xsd:attribute>
       </xsd:extension>
      </xsd:simpleContent>
     </xsd:complexType>
    </xsd:schema>
    '
    CREATE XML SCHEMA COLLECTION dbo.MyPrivateSchemaCollection AS @testSchema
    GO

    DECLARE @testXML XML (DOCUMENT [dbo].[MyPrivateSchemaCollection])

    -- this works
    SELECT @testXML =
    '<?xml version="1.0" encoding="UTF-8"?>
    <MESSAGE xmlns="http://www.mismo.org/residential/2009/schemas"
       MISMOReferenceModelIdentifier="3.0.0.263.12">
     <ABOUT_VERSION>
      <AboutVersionIdentifier>FRE_1.0.1</AboutVersionIdentifier>
     </ABOUT_VERSION>
    </MESSAGE>
    '
    select @testXML

    -- this doesnt
    SELECT @testXML =
    '<?xml version="1.0" encoding="UTF-8"?>
    <MESSAGE MISMOReferenceModelIdentifier="3.0.0.263.12">
     <ABOUT_VERSION>
      <AboutVersionIdentifier>FRE_1.0.1</AboutVersionIdentifier>
     </ABOUT_VERSION>
    </MESSAGE>
    '
    select @testXML
    [/code]

    The error I get on the second assignment to @testXML is:
    Msg 6913, Level 16, State 1, Line 17
    XML Validation: Declaration not found for element 'MESSAGE'. Location: /*:MESSAGE[1]

    Thank you in advance for any insights or directions to more documentation regarding this problem.

All Replies

  • Friday, October 15, 2010 8:31 PM
     
     

    To be mysterious:  there is no "MESSAGE" element in your schema.  There is a "MESSAGE" element that is in "http://www.mismo.org/residential/2009/schemas" namespace.  In the first xml document, you have the xmlns declaration  xmlns="http://www.mismo.org/residential/2009/schemas" which states that any name that is in the document is in that namespace.  Since your schema has identified MESSAGE as being in that namespace (because it too has  xmlns="http://www.mismo.org/residential/2009/schemas") the SQL XML parser says good.  There is a "MESSAGE" element from the above namespace.

    In the second document you don't have the namespace declaration ( xmlns="http://www.mismo.org/residential/2009/schemas").  That means that "MESSAGE" is just a plain old element "MESSAGE" that isn't in any namespace at all.  It is therefore not the same as "MESSAGE" that is in the namespace.

    It is like the term "President."  President of what?  If your company has a President, is that the same position as the President of the United States?  I have to specify a namespace for the term President.


    Russel Loski, MCT
  • Friday, October 15, 2010 8:42 PM
     
     

    I keep feeling like Bud Abbott asking Who's on First.

     

    Why does there need to be a namespace declared within the document at all?

     

    I have the @testXML variable declared as typed XML

    and bound to the XML schema collection

    which in turn contains the namespace declaration.

     

    Why doesn't the validation of the variable use the XML schema collection that I am telling it to use?

     

    As I said, we are never going to get any XML documents that look like the first example WITH the namespace declaration.

    We are only going to receive XML documents that look like the second example WITHOUT the namespace declaration.

     

    So how do I validate the second example using the XML schema collection?

     

    What is wrong with the way I am asking this question?

     

     

  • Friday, October 15, 2010 8:58 PM
     
     

    By the way, I have also tried directly specifiying a default namespace by appending

    ;

     

    WITH XMLNAMESPACES(DEFAULT 'http://www.mismo.org/residential/2009/schemas')

    to the front of the second SELECT but that didn't help either.

  • Friday, October 15, 2010 9:02 PM
     
     

    Sorry can't help you.


    Russel Loski, MCT
  • Friday, October 15, 2010 9:15 PM
     
      Has Code

    In your schema collection declaration remove the two references to http://www.mismo.org/residential/2009/schemas.

    <xsd:schema 
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      attributeFormDefault="unqualified" 
      elementFormDefault="qualified"
      >
    
    

    The first assignment will bomb and the second will work for exactly the same reason.  By removing reference, you are saying that this schema is about MESSAGE that does not have a namespace.  The first query has a namespace.

    If you use a schema, that schema specifies what it is about.  What the namespace is.


    Russel Loski, MCT
  • Friday, October 15, 2010 9:33 PM
     
     

    I see how that fixes the problem, but the schema is a published item.

    And changing the schema causes the first example to error out instead.

    So far, I only had to change "lax" to "skip" within the schema in order to be able to load it into a Schema Collection.

    Is it only because I am using Sql Server that namespace handling is a problem?

    Is everyone who uses Sql Server going to have to modify the schema in order to use it with XML data types in Sql Server?

    I thought that using XML was supposed to make things easier.

    Thanks

     

  • Tuesday, October 19, 2010 6:57 PM
     
     

    Do I need to instead think in terms of "repairing" the XML documents that I receive if they do not contain the namespace declaration?

    At least then I could still use the public schema definition.

     

  • Tuesday, October 19, 2010 7:05 PM
     
     
    Or you could repair the xml schema that you add to the xml schema collectio, removing the xmlns and targetNamespace attributes.
    Russel Loski, MCT