Reputation: 4729
Having following XML Schema with a element of type xs:anyType
.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="payloadAny" type="xs:anyType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Any example of an XML:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<payloadAny>
<anything>anyContent</anything>
</payloadAny>
</root>
We now discovered the problem when in the element is the xsi:type attribute like this:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<payloadAny>
<anything xsi:type="ForeignType">anyContent</anything>
</payloadAny>
</root>
This XML having the xsi:type
attribute does not validate anymore against the XML Schema with the xs:anyTyp
e element.
E [Xerces] cvc-elt.4.2: Cannot resolve 'ForeignType' to a type definition for element 'anything'.
When using the <xs:any/>
element instead of the <element type="xs:anyType"/>
, the validation can be disabled using the processContents attribute.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:complexType name="any">
<xs:sequence>
<xs:any processContents="skip"/>
</xs:sequence>
</xs:complexType>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="payloadAny" type="any"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The questions are now the following:
xsi:type
validation in a xs:anyType
element (like processContents="skip
)<xs:any/>
vs. <element type="xs:anyType"/>
Upvotes: 2
Views: 2187
Reputation: 25034
How to disable the
xsi:type
validation in axs:anyType
element (likeprocessContents="skip"
)
With the element and type structure you have defined in your schema, the validation of the xsi:type
attribute by the schema processor cannot be disabled. A declaration for xsi:type
is built in to every conforming XSD schema validator, and whenever an element with an xsi:type
attribute is validated either strictly or laxly, that declaration will be found and the xsi:type
attribute will be validated against it. A valid xsi:type
attribute has a QName value which identifies a type definition in the schema. If the QName value does not identify a type definition, the attribute is invalid.
From the error message you are reporting, it appears that your schema contains no type whose expanded name is {}ForeignType
.
One way to work around it would be to declare the type you appear to really want for the payloadAny
element, which is not xs:anyType
but a different type with a skip
wildcard. Your second schema example shows the principle, although your {}any
type differs from xs:anyType
in allowing only one element child, instead of allowing many elements and also character data. (If you only want one element as child of payloadAny
, then xs:anyType
is too loose.)
Note that the error reported is not a claim that the anything
element is invalid against the type ForeignType
, but a report that the xsi:type
attribute is itself invalid, because it violates clause 4.2 of the constraint Element Locally Valid. That in turn makes payloadAny
invalid.
Why is there such difference in a
<xs:any/>
vs.<element type="xs:anyType"/>
The built-in type xs:anyType
behaves (roughly) as though declared with the following declaration shown in the XSD 1.0 schema for schema documents:
<xs:complexType name="anyType" mixed="true">
<xs:annotation>
<xs:documentation>
Not the real urType, but as close an approximation as we can
get in the XML representation</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:any minOccurs="0"
maxOccurs="unbounded"
processContents="lax"/>
</xs:sequence>
<xs:anyAttribute processContents="lax"/>
There are no parameterization mechanisms for complex types analogous to the processContents
and other attributes on xs:any
. Adding such mechanisms for xs:anyType
would have required additional ad hoc mechanisms (is XSD not complicated enough for you? you want more?) and would not have increased the expressive power of the language.
As design decisions go, that one looks pretty much like a no-brainer. Even the XML Schema working group (never one to regard ad hoc machinery with much skepticism) thought such special-purpose machinery unnecessary.
In a schema language built on attribute grammars, where inherited attributes can serve to parameterize elements and/or types, such machinery would be a natural part of the language and having parameters for lax/strict/skip processing might make sense. But neither XSD nor any other schema language in wide use is built on attribute grammars.
Upvotes: 3