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

dotnet xml : XML Validation: required attributes


Stan Kitsis [MSFT]
12/21/2005 4:01:05 PM
Either you didn't do what you thought you did or the validator has a
problem. Which validator did you use and what are your schema/instance?

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.


[quoted text, click to view]

uttara
12/21/2005 4:31:44 PM
Hello All,
I was trying to validate an XML document against a XSD which works
fine. Then I tried to put in a version attribute on the root element and
set it to a fixed value of '1.0' in the schema. Guess what? The
validator completely ignores the attribute inspite of the fact that I
have set the attribute to be required and to have a fixed value of '1.0'.
Any ideas why that could be happening. Any help is appreciated.
uttara
12/22/2005 12:55:02 PM
Stan,
I won't be utterly surprised if I missed out something. I am using the
XMLValidatingReader. Here is the schema I am using:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Schema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Products">
<xs:complexType>
<xs:sequence>
<xs:element name="ItemCode" minOccurs="1"
maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="SKU" type="xs:string"
minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="code" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="schemaVersion" type="xs:decimal"
use="required" fixed="1.0" />
</xs:complexType>
</xs:element>
</xs:schema>

Here is the instance
<?xml version="1.0" encoding="utf-8" ?>
<Products schemaVersion="1.1">
<ItemCode code="10098"></ItemCode>
<ItemCode code="10047">
<SKU>0023</SKU>
<SKU>0025</SKU>
</ItemCode>
</Products>

Thanks again,
Uttara

[quoted text, click to view]
Stan Kitsis [MSFT]
12/22/2005 1:22:20 PM
I get the following warning using .net 2.0: "The value of the
'schemaVersion' attribute does not equal its fixed value."

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.


[quoted text, click to view]

uttara
12/22/2005 3:32:22 PM
Hmm...what if we take out the schemaVersion attribute from the root
element so it looks like this:
<?xml version="1.0" encoding="utf-8" ?>
[quoted text, click to view]
Stan Kitsis [MSFT]
12/22/2005 4:10:31 PM
This should be invalid since the version attribute is required.

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.

[quoted text, click to view]

Chris Lovett
12/22/2005 11:33:03 PM
The following code does produce the warning:
"The value of the 'schemaVersion' attribute does not equal its fixed
value."


static void Main(string[] args) {
Directory.SetCurrentDirectory("..\\..");

ValidationEventHandler handler = new
ValidationEventHandler(OnValidationEvent);
XmlTextReader reader = new XmlTextReader("test.xml");
XmlValidatingReader validator = new XmlValidatingReader(reader);
validator.Schemas.Add(XmlSchema.Read(new
StreamReader("test.xsd"), handler));
validator.ValidationType = ValidationType.Schema;
validator.ValidationEventHandler += handler;
while (validator.Read()) {
}
return;
}

static void OnValidationEvent(object sender, ValidationEventArgs e)
{
Console.WriteLine(e.Message);
}



[quoted text, click to view]

AddThis Social Bookmark Button