Question

Goal: create a complexType that allows element "skip" to contain any element, any number of times, in any order and validates my xml without error.

Schema: skip.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema
  xmlns="http://www.example.com/main"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://www.example.com/main" 
  attributeFormDefault="unqualified" 
  elementFormDefault="qualified"
  >

  <xs:complexType name="skip">
    <xs:sequence>
      <xs:any minOccurs="1" 
              maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>

</xs:schema>

I import skip.xsd into my main.xsd

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema
  xmlns:esc="http://www.escmatrix.com/main"
  xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  targetNamespace="http://www.example.com/main" 
  attributeFormDefault="unqualified" 
  elementFormDefault="qualified"
  >

    <xs:include 
      schemaLocation="http://www.example/skip" />

    <xs:element name="tasks">
      <xs:complexType>
        <xs:choice minOccurs="0" 
                   maxOccurs="unbounded" >
          <xs:element name="skip" 
                      type="esc:skip" />
        </xs:choice>
      </xs:complexType>
    </xs:element>
<xs:schema>

I need to validate xml that can have the form:

<skip>
   <anyelement1></anyelement1>
   <anyelement2></anyelement2>
   <anyelementN></anyelementN>
</skip>

Problem: Error when validating; "cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'anyelement1' "

Was it helpful?

Solution

It would be surprising if the schema documents you show produced the error message you quote; I would expect your first problem to be that your schema document is (you say) named skip.xsd, but the schemaLocation attribute on the relevant xs:include points to http://example.com/skip, and your second problem to be that your skip type is in namespace http://www.example.com/main but the declaration for the skip element refers to a type in namespace http://escmatrix.com/main. But I guess these are just clerical errors made in cutting down your example code for the question.

The error message is pretty clear: your wildcard is being interpreted as a strict wildcard, so elements which match it are expected to have declarations, and the document is invalid if they do not. Your wildcard is a strict one because you don't specify a processContents attribute for it, and the default value of processContents is strict. If you want to allow undeclared elements, you will need to specify either processContents="lax" or processContents="skip" -- use a lax wildcard if elements for which declarations do exist should be validated against those declarations, use a skip wildcard if you don't want them to be validated at all, in which case the skip element will be valid as long as its children are well formed. You say you want a schema that "validates my xml without error", by which I guess you mean "accepts my XML as valid in all cases" (the processor is already correctly and successfully checking the validity of your input without failure, so it's already validating your XML without error), so it looks as if you want a skip wildcard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top