Groups | Blog | Home
all groups > dotnet xml > december 2005 >

dotnet xml : Trouble validating XML w/ XSD.


Jonas Bush
12/20/2005 12:16:31 PM
I've got the some code to try and validate some xml. Against my schema,
the "Good" xml (below) produces a couple of warnings, which I don't
care about. The "Bad" xml (also below), produces warnings as well, but
*should* be producing errors. The XML validator at
http://apps.gotdotnet.com/xmltools/xsdvalidator/Default.aspx reports
that the "Good" xml produces warnings, but the "bad" xml produces
errors, which is what I want to reproduce in my code. The code and all
the xml/xsd are below. Any help would be greatly appreciated!

Jonas
spicyj2k@gmail.com

private static void TestValidation()
{
//string rss = @"c:\t1\po.xml";
string rss = @"c:\t1\pobad.xml";
string file = @"c:\t1\po.xsd";

StreamReader mem = new StreamReader(rss, Encoding.Default);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += new
ValidationEventHandler(valReader_ValidationEventHandler);
settings.ValidationType = ValidationType.Schema;
settings.XmlResolver = null;
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings;
settings.Schemas.Add(null, file);
XmlReader rdr = XmlReader.Create(mem, settings);

while (rdr.Read())
{
}

rdr.Close();
mem.Close();
}


private static void valReader_ValidationEventHandler(object sender,
ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Error)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.Severity.ToString());
}
}


"Invalid" XML:
<?xml version="1.0"?>
<thing xmlns="http://purl.org/dc/elements/1.1/">
<thing1>hello there!</thing1>
</thing>

XSD

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:complexType name="PurchaseOrderType">
<xsd:sequence>
<xsd:element name="shipTo" type="USAddress"/>
<xsd:element name="billTo" type="USAddress"/>
<xsd:element ref="comment" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="orderDate" type="xsd:date"/>
</xsd:complexType>
<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
<xsd:any namespace="##other" processContents="lax" minOccurs="0"
maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN"
fixed="US"/>
</xsd:complexType>
</xsd:schema>

"Valid" XML
<?xml version="1.0"?>
<purchaseOrder xmlns:dc="http://purl.org/dc/elements/1.1/"
orderDate="1999-10-20">
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
<dc:wtf>hello</dc:wtf>
</shipTo>
<billTo country="US">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>Hurry, my lawn is going wild!</comment>
</purchaseOrder>
Zafar Abbas
12/27/2005 1:11:43 PM
This is the case where .NET 1.1 used to throw an error but .NET 2.0 now
throws only a warning.

For a root element node, if a schema matching the namespace if the element
is not found, then lax validation kicks in according to XSD spec. This
element is then validated according to the content model of xs:anyType,
which contains an xs:any with processContents=lax.

Since the declarations for 'thing' and 'thing1' are not found, it only
produces warnings, rather than errors, since this is how the semantics of
xs:any with processContents=lax work.

Note that in the invalid xml, your root element is in a namespace for which
you do not have a schema. If you remove the namespace declaration
xmlns="http://purl.org/dc/elements/1.1/" and let <thing> be in the
targetNamespace of the schema, then you will see the errors.

Zafar


[quoted text, click to view]

Jonas Bush
12/27/2005 3:53:14 PM
To answer the last part, I can't remove the namespace declaration from
the "invalid" xml because that's how it comes into my application. :)
How does the validator at GDN produce errors given the documents up
there?
Zafar Abbas
12/28/2005 11:24:32 AM
The validator you tried uses .NET 1.1 - the reason of the errors.

[quoted text, click to view]

Martin Honnen
12/28/2005 3:16:46 PM


[quoted text, click to view]


[quoted text, click to view]

As explained, .NET 1.x and .NET 2.0 are different for that case, and
that validator probably was and is still implemented with .NET 1.x.


--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button