mrgreatter
mrgreatter

Reputation: 11

.NET XML validator doesn't give accurate error message for prohibited attributes

I am creating a validator in .NET8 for railML data format (based on XML) using XDocument.Validate() and XMLSchemaSet, but I encountered a situation when if I place an attribute in an element that, for example, doesn't allow it or any attributes or doesn't allow extensions from schemas in that place (doesn't have xsi:any or xsi:anyAttribute etc) it provides an error message like this: "The 'nameOfAttribute' attribute is not declared.". In comparison if you place an incorrect child element, this is the kind of message I get:

The element 'categories' in namespace 'http://www.railml.org/schemas/2011' has invalid child element 'track' in namespace 'http://www.railml.org/schemas/2011'. List of possible elements expected: 'category' in namespace 'http://www.railml.org/schemas/2011'.

And it gives same error message even in the situation when the child element is an extension without declaration.

The problem is that when attribute's schema or declaration is missing it gives the same kind of message as when it is not allowed, which also creates a situation when if I use an extension where I shouldn't be able to and it is missing schema then it is not possible to tell difference between issue of declaration and it breaking the rules of railML. It is very important for me to know when attribute doesn't belong to the element and the priority should be on that, instead of it being undeclared.

Here is the correct xml file:

<?xml version="1.0" encoding="UTF-8"?>
<railml xmlns="http://www.railml.org/schemas/2011" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.railml.org/schemas/2011 http://www.railml.org/schemas/2011/railML-2.1/railML.xsd" 
        version="2.1">
  <timetable id="tt1">
    <categories>
      <category id="c1" code="IC" name="InterCity" description="Fast large city connection" trainUsage="passenger"/>
      <category id="c2" code="RE" name="RegionalExpress" description="Fast small city connection" trainUsage="passenger"/>
    </categories>
  </timetable>
</railml>

Here is the changed part with incorrect attribute and child element in categories:

  <timetable id="tt1">
>     <categories id="c3">
      <category id="c1" code="IC" name="InterCity" description="Fast large city connection" trainUsage="passenger"/>
      <category id="c2" code="RE" name="RegionalExpress" description="Fast small city connection" trainUsage="passenger"/>
>       <track id='tr_1' name='Some name' type='mainTrack'></track>
    </categories>
  </timetable>

I also tested different validators, for example in Liquid Studio Microsoft .NET gives the same kind of declaration message, but Xerces actually tells "Critical cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'categories'.", which is more accurate to the situation. When I test same code in Altova XMLSpy it tells me this:

Attribute 'id' is not allowed in element 'categories id="c3"' Error location: railml / timetable / categories / @id Details cvc-complex-type.3.2.1: Complex type definition 'rail:eCategories' of element 'categories id="c3"' does not allow attribute 'id' and has no attribute wildcard. cvc-type.3.2: Element categories id="c3" is not valid with respect to type definition 'rail:eCategories'. cvc-elt.5: The element categories id="c3" is not valid with respect to the actual type definition 'rail:eCategories'. cvc-assess-elt.1.1: Strict assessment of element categories id="c3" with governing element declaration 'rail:categories' failed. Element 'track id="tr_1"' is not allowed under element 'categories id="c3"'. Reason: The following elements are expected at this location (see below) 'rail:category' Error location: railml / timetable / categories / track Details cvc-complex-type.1.4: Element 'track id="tr_1"' unexpected by type 'rail:eCategories' of element 'categories id="c3".'

XMLSpy also notices difference between prohibited elements and undeclared, even if all of them are undeclared extensions.

Which is more like something I would expect. So if attribute is places against railML rules it should always be noted as not allowed, even if it is something like undeclared extension. Is there any way to get similar result with adjusting/manipulating this approach with XDocument.Validate or with any other .NET validator? And what is the reason for such error message and why is there no difference between these two cases for .NET XDocument.Validate?

Upvotes: 1

Views: 56

Answers (0)

Related Questions