HighTechTalks DotNet Forums  

XSD doesn't validate XPath

Dotnet XML microsoft.public.dotnet.xml


Discuss XSD doesn't validate XPath in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Eve
 
Posts: n/a

Default XSD doesn't validate XPath - 10-18-2007 , 09:21 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.

Thank you for any comments.

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: XSD doesn't validate XPath - 10-18-2007 , 09:29 AM






Eve wrote:
Quote:
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.
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
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Eve
 
Posts: n/a

Default Re: XSD doesn't validate XPath - 10-18-2007 , 10:04 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


"Martin Honnen" wrote:

Quote:
Eve wrote:
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.

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
http://JavaScript.FAQTs.com/


Reply With Quote
  #4  
Old   
Martin Honnen
 
Posts: n/a

Default Re: XSD doesn't validate XPath - 10-18-2007 , 10:33 AM



Eve wrote:
Quote:
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
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
http://JavaScript.FAQTs.com/


Reply With Quote
  #5  
Old   
Eve
 
Posts: n/a

Default Re: XSD doesn't validate XPath - 10-18-2007 , 10:46 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.ProcessIdentityConstraint s |
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;
}
}

"Martin Honnen" wrote:

Quote:
Eve wrote:
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

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
http://JavaScript.FAQTs.com/


Reply With Quote
  #6  
Old   
Martin Honnen
 
Posts: n/a

Default Re: XSD doesn't validate XPath - 10-18-2007 , 11:18 AM



Eve wrote:
Quote:
This is my validation code:
[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
http://JavaScript.FAQTs.com/


Reply With Quote
  #7  
Old   
Eve
 
Posts: n/a

Default Re: XSD doesn't validate XPath - 10-18-2007 , 11:43 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!

"Martin Honnen" wrote:

Quote:
Eve wrote:
This is my validation code:

[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
http://JavaScript.FAQTs.com/


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.