sql-serverxsdxml-schema-collectionxmlschema

Reusable simpleType definition in SQL Server schema collection


I'm trying to create a reusable simpleType in an SQL Server XML schema collection but I'm getting an error. For example:

<?xml version="1.0" encoding="UTF-16"?>
<xs:schema
    attributeFormDefault="unqualified"
    elementFormDefault="qualified"
    targetNamespace="SomethingUnique"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="dateTimeOrEmpty">
        <xs:union memberTypes="xs:dateTime">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value=""/>
                </xs:restriction>
            </xs:simpleType>
        </xs:union>
    </xs:simpleType>

    <xs:element name="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="SomeDate"/>
                    <xs:simpleType>
                        <xs:union memberTypes="xs:dateTime">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:enumeration value=""/>
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:union>
                    </xs:simpleType>
                <xs:element name="OtherDate" type="dateTimeOrEmpty" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

If I try to create an XML schema collection with this schema I get this error

Reference to an undefined name 'dateTimeOrEmpty'

Is it possible to reuse the named simpleType definition or do I have to retype the entire simpleType definition for each element I was to use it on?


Solution

  • Looks like a namespace problem. I think I understood this at one time, but now I just have to fiddle with it until it works. EG

    CREATE XML SCHEMA COLLECTION foo AS  
    N'<?xml version="1.0" encoding="UTF-16"?>
    <xs:schema
        attributeFormDefault="unqualified"
        elementFormDefault="qualified"
        targetNamespace="SomethingUnique"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:s="SomethingUnique"
        >
    
        <xs:simpleType name="dateTimeOrEmpty">
            <xs:union memberTypes="xs:dateTime">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value=""/>
                    </xs:restriction>
                </xs:simpleType>
            </xs:union>
        </xs:simpleType>
    
        <xs:complexType name="RootType">
            <xs:sequence>
                    <xs:element name="SomeDate" type="s:dateTimeOrEmpty"/>
                    <xs:element name="OtherDate" type="s:dateTimeOrEmpty" />
                </xs:sequence>
        </xs:complexType>
    
        <xs:element name="Root" type="s:RootType"/>
    
    </xs:schema>'