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>


[5954 byte] By [aero1] at [2008-2-18]
# 1
I believe the implementation in the SQL Engine is correct per the spec:

http://www.w3.org/TR/xmlschema-1/#Type_Derivation

A complex type which extends another does so by having additional content model particles at the end of the other definition's content model, or by having additional attribute declarations, or both.

Note: This specification allows only appending, and not other kinds of extensions. This decision simplifies application processing required to cast instances from derived to base type. Future versions may allow more kinds of extension, requiring more complex transformations to effect casting.

That being said, there appears to be a workaround which is to derive first by restriction to alter the existing particles, and then derive that by extension to add the new particles. I am attaching the modification that I made:
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="ChildTypeBase" abstract="true">
<xs:complexContent mixed="true">
<xs:restriction base="p:ParentType">
<xs:attribute name="attr2" type="xs:boolean" default="false" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="ChildType">
<xs:complexContent mixed="true">
<xs:extension base="ChildTypeBase">
<xs:attribute name="attr3" type="xs:string" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
'
JohnGallardo-MSFT at 2007-10-6 > top of Msdn Tech,SQL Server,SQL Server XML...
# 2
Thanks John - I will check that out

One question remains though - why the difference in behaviour with the .Net classes?

aero1 at 2007-10-6 > top of Msdn Tech,SQL Server,SQL Server XML...
# 3
Hi,

The XSD implementation in SQL Server 2005 does not rely on System.Xml or MSXML. This is why you might see minor differences. We have been compiling them and I will try to make the list available in the near future.

The System.Xml team confirmed to me that the behavior you experienced is a known bug. Your schema should have been rejected in VB.Net.

John already provided you with a very good workaround. Please let us know if there is more we can do to help.

Denis Ruckebusch

DenisRuckebusch at 2007-10-6 > top of Msdn Tech,SQL Server,SQL Server XML...
# 4
Initial testing indicates that the workround works fine.

It would certainly be very helpful to have a list available of the differences between the XSD implementations on SQL and system.xml.

Many thanks to John and Denis

aero1 at 2007-10-6 > top of Msdn Tech,SQL Server,SQL Server XML...

SQL Server

Site Classified