Groups | Blog | Home
all groups > dotnet xml > october 2007 >

dotnet xml : XSD doesn't validate XPath


Eve
10/18/2007 6:21:01 AM
I'm fairly new to xsd. I'm validating xml against xsd using
XmlReaderSettings. It looks like my xsd does not verify the element path: My
schema definition allows for both OrderProperties and OrderPartners elements,
however Partner can be specified only under OrderPartners. In other words,
the correct path is OrderPartners/Partner, not OrderProperties/Partner. My
xml contains OrderProperties/Partner ONLY and my xml validation code does not
give me an error (I'm sure the code is working since it's giving me other
errors such as invalid value for an attribute or missing element). I'm trying
to figure out if there's something wrong with my validation code or the xsd.
Before posting any code, I want to confirm what seems to me to be the case:
1. It is possible to make xsd to impose specific XPath.
2. If xsd requires certain XPath, I have no control over whether the XPath
is validated or not.

Eve
10/18/2007 7:04:01 AM
The xsd I'm using is rather complex, I didn't write it, it's third party and
I prefer not to dig deep into it. I just need to get a general idea of how
xsd works, specifically if it's possible for xsd to "prevent" xml from having
an element in the wrong place, which according to what you wrote, Martin
("You can certainly specify which child elements are allowed on a certain
element" - this is exactly what I mean when I say, "xsd to impose specific
XPath"). If that's the case, why don't I get any errors if I specify a child
element that is invalid? Again:
OrderPartners/Partner is correct
OrderProperties/Partner is not


[quoted text, click to view]
Eve
10/18/2007 7:46:02 AM
This is my validation code:

public class XMLValidator
{
// Validation Error Count
static int ErrorsCount = 0;

// Construct a validation error message
static string ErrorMessage = "";

private static void ValidationHandler(object sender,
ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Error || e.Severity ==
XmlSeverityType.Warning)
{
ErrorsCount++;
ErrorMessage += String.Format(ErrorsCount + ". Line {0},
Position {1} \"{2}\"",
e.Exception.LineNumber, e.Exception.LinePosition,
e.Exception.Message) + "\r\n";
}

}

public String Validate(string XMLDoc, string XSDSchema)
{
String ValidationMessage = null;

// Create the XmlSchemaSet class
XmlSchemaSet sc = new XmlSchemaSet();

// Add the schema to the collection
sc.Add(null, XSDSchema);

// Set the validation settings
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler
(ValidationHandler);
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.CloseInput = true;

// Create the XmlReader object
XmlReader reader = XmlReader.Create(XMLDoc, settings);

// Parse the file
while (reader.Read()) ;

if (ErrorsCount > 0)
{
// XML validation failed
ValidationMessage = ErrorMessage;
}
return ValidationMessage;
}
}

[quoted text, click to view]
Eve
10/18/2007 8:43:04 AM
Thanks for your time and devotion to solving my "problem"! What you just did
proves that the reason I'm not getting any error message with my third-party
xsd is the xsd doesn't check if an element has an invalid child element. If
you look at my original message, that is all I needed to know - whether I
have to change my code to catch it or the schema definition needs to be
changed. Now I know it's the schema that was wrong. Thank you!

[quoted text, click to view]
Martin Honnen
10/18/2007 3:29:57 PM
[quoted text, click to view]

XSD schemas are not based on XPath (besides for key/unique constrainst).
So I am not sure why you ask about "xsd to impose specific XPath".
You can certainly specify however which child elements are allowed on a
certain element simply using complexType and sequence for instance.

You might want to post your XML and XSD so that we can better understand
what your current problem is.


--

Martin Honnen --- MVP XML
Martin Honnen
10/18/2007 4:33:29 PM
[quoted text, click to view]

How do you validate exactly? Do you use an XML editor? If so which one?
Or do you use an API, which one exactly?
Have you tried a different editor or parser to check whether the
validation problem is occuring there too?


--

Martin Honnen --- MVP XML
Martin Honnen
10/18/2007 5:18:08 PM
[quoted text, click to view]

[snip]

Your code is fine and I have used it with a simple XML document and
schema and it outputs an error message.

Here is the XML:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<OrderProperties>
<Partner />
</OrderProperties>
</root>

Here the schema:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderProperties">
<xs:complexType>
<xs:sequence>
<xs:element name="property" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

and here the output of your code:

1. Line 4, Position 6 "The element 'OrderProperties' has invalid child
element
'Partner'. List of possible elements expected: 'property'."



--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button