XML Schema extension behaving differently between SQL2005 and VB.Net 2.0
In summary my problem is:
- I have two schemas 'Parent' and 'child'
- The child schema extends the parent schema by adding one attribute and redefining an attribute from the parent
- VB.Net permits the two dependent schema definitions in a schemaset
- Creating the two schemas in a SQL2005 xml schem collection results in the error "Derivation by extension may not redefine attributes"
I would like SQL2005 to exhibit the same behaviour as VB.Net - but which is right and why am I seeing different behaviour?
- SQL and VB.net sample code is given below which reproduces the behaviour
(using release versions of VS2005 and SQL2005 developer edition)
=SQL 2005 =======
create xml schema collection TestCollection AS
N'<xs:schema targetNamespace="http://parent.xsd"
elementFormDefault="qualified"
xmlns="http://parent.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ParentType" mixed="true" abstract="true">
<xs:attribute name="attr1" type="xs:string" />
<xs:attribute name="attr2" type="xs:boolean" default="true" />
</xs:complexType>
</xs:schema>
<xs:schema targetNamespace="http://child.xsd"
elementFormDefault="qualified"
xmlns="http://child.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p="http://parent.xsd">
<xs:import namespace="http://parent.xsd" />
<xs:complexType name="ChildType">
<xs:complexContent mixed="true">
<xs:extension base="p:ParentType">
<xs:attribute name="attr2" type="xs:boolean" default="false" />
<xs:attribute name="attr3" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
'
The above results in the following error
Msg 6976, Level 16, State 1, Line 1
Invalid redefinition of attribute 'attr2' in type 'http://child.xsd:ChildType'. Derivation by extension may not redefine attributes.
==VB ========================
Imports System.Xml
Imports System.Xml.Schema
Module Module1
Sub Main()
Dim yy As New sandbox
yy.schematest()
End Sub
End Module
Public Class sandbox
Public Sub schematest()
Dim _schema As New Xml.Schema.XmlSchema()
Dim _schemaSet As New Xml.Schema.XmlSchemaSet
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
Dim veh As New System.Xml.Schema.ValidationEventHandler(AddressOf ValidationCallBack)
Dim xr As XmlReader
'add parent to schemaset
xr = XmlReader.Create("C:\temp\parent.xsd")
_schema = Xml.Schema.XmlSchema.Read(xr, veh)
_schemaSet.Add(_schema)
'add child to schemaset
xr = XmlReader.Create("C:\temp\child.xsd")
_schema = Xml.Schema.XmlSchema.Read(xr, veh)
_schemaSet.Add(_schema)
_schemaSet.Compile()
' no errors!
End Sub
Private Shared Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("Validation Error: {0}", e.Message)
End Sub
End Class
== TEST Files follow ============
===parent.xsd ===========
<xs:schema targetNamespace="http://parent.xsd"
elementFormDefault="qualified"
xmlns="http://parent.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ParentType" mixed="true" abstract="true">
<xs:attribute name="attr1" type="xs:string" />
<xs:attribute name="attr2" type="xs:boolean" default="true" />
</xs:complexType>
</xs:schema>
===child.xsd ============
<xs:schema targetNamespace="http://child.xsd"
elementFormDefault="qualified"
xmlns="http://child.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:p="http://parent.xsd">
<xs:import namespace="http://parent.xsd" />
<xs:complexType name="ChildType">
<xs:complexContent mixed="true">
<xs:extension base="p:ParentType">
<xs:attribute name="attr2" type="xs:boolean" default="false" />
<xs:attribute name="attr3" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>

