1

I have the following simple XSD schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://test" elementFormDefault="qualified" attributeFormDefault="unqualified"
  xmlns:tns="http://test"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="AType">
        <xs:sequence>
            <xs:element name="info" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    <xs:element name="A" type="tns:AType"/>
</xs:schema>

Is the following simple XML document valid in relation to this schema?

<?xml version="1.0" encoding="UTF-8" ?>
<X xsi:type="AType"
  xmlns="http://test"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <info>text</info>
</X>

I tried it in an on-line validator that uses JAXP and the default J2SE 1.7 parser, and the XML document validated successfully (http://www.utilities-online.info/xsdvalidation/). On the other hand, xmllint says that

Schemas validity error : Element '{http://test}X': No matching global declaration
available for the validation root. test2.xml fails to validate

Are they using different validation modes? Is the mode used by the J2SE parser based on the XSD specification?

Amendment to the question:

Michael Kay points me (below) to particular locations in the XSD specification. I've tried to decipher what the XSD specification says. Do I understand correctly that:

  1. The three assessment approaches are said "primary", so that other (arbitrary) approaches are allowed?
  2. In (3), if there is no matching XSD definition for the root element, lax assessment should be used. But it seems that the spec doesn't exclude elements with namespace prefix from lax assessment. While then the J2SE online validatorn fails if the element in the above example is given a namespace prefix?
  3. Does lax validity assession apply to the root in my example above? The root has no "context-determined declaration", has it? The definition in the spec reads:

    ...an element information item's schema validity may be laxly assessed if its "context-determined declaration" is not skip by "validating" with respect to the "ur-type definition"...

All in all, is any validation approach allowed by the XSD spec? And if so, what does it mean then for a XML document to be schema-valid?
xarx
  • 627
  • 1
  • 11
  • 27
  • take a look at: http://stackoverflow.com/a/1094988/101715 – Yaneeve Nov 21 '13 at 12:58
  • Yaneeve: Thank you. Your reference just demonstrates that there is a big variability in how various parsers interpret what is the XSD validation. And I thought, naively, that this notion is clear and defined by the XSD spec. :-( – xarx Nov 21 '13 at 21:39

1 Answers1

0

Yes, the XSD specification does define more than one "validation mode" and it's possible for different tools to use different options. See

http://www.w3.org/TR/xmlschema-1/#validation_outcome

I would expect the default (if you don't specify an element or type name to validate against) to be (3), which takes you to schema-validity assessment (element) here:

http://www.w3.org/TR/xmlschema-1/#cvc-assess-elt

This essentially says that there must be either (a) a global element declaration matching the name of the outermost element in your instance, or (b) a global type definition matching its xsi:type attribute. And of course the instance must be valid against that element declaration or type. Since your instance has an xsi:type attribute, I would expect it to be validated against that type.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • I added my reaction to your answer to my original question, not here as a comment, because it is a little longer and requires formatting. – xarx Nov 21 '13 at 15:05
  • In theory, anyone providing a schema validation API (or tool) should describe exactly what it does by reference to particular processes defined in the XSD spec; there doesn't have to be an exact mapping. In practice, sadly, many tools seem to be documented on the assumption that it's obvious what validating a document means, which as you are discovering is far from being the case. – Michael Kay Nov 21 '13 at 20:06
  • I'll sum it up: The XSD specification is vague in what it means that a **whole** XML document is valid relatively to a XSD schema. There are some hints in the spec which the validation process could follow, but the parsers do the document validation arbitrarily. Hence one parser validates the document successfully, while another refuses it. – xarx Nov 21 '13 at 21:59
  • I'd like to support the previous summarization by quoting two statements from the XSD spec: Ch.5 "schema validity is not a binary predicate". Ch.5.2 "It is up to applications to decide what constitutes a successful (validation) outcome." – xarx Nov 21 '13 at 22:24