xmlxsdxsd-validationxml-validationsvcutil.exe

Can xs:any be in a xs:all element in XSD?


I'm trying to generate data contracts from supplied XSD's. Svcutil.exe is throwing this error at me:

The 'http://www.w3.org/2001/XMLSchema:any' element is not supported in this context."

Looking in the XSD, element of type any appear twice. This is the first time it appears.

 <xs:element minOccurs="0" maxOccurs="1" name="Markup">
        <xs:complexType>
          <xs:all>
            <xs:any processContents="lax" />
          </xs:all>
        </xs:complexType>
      </xs:element>
    </xs:all>
  </xs:complexType>

From my research on this, it seems like xs:any can't be in an xs:all element. But I can't find a specification or equivalent that definitively shows this.

Can xs:any appear in an xs:all? Or is it not valid?


Solution

  • XSD 1.0

    No, xs:any cannot be in a xs:all in XSD:

    <all
      id = ID
      maxOccurs = 1 : 1
      minOccurs = (0 | 1) : 1
      {any attributes with non-schema namespace . . .}>
      Content: (annotation?, element*)
    </all>
    

    But xs:any can be in a xs:choice or xs:sequence:

    <choice
      id = ID
      maxOccurs = (nonNegativeInteger | unbounded)  : 1
      minOccurs = nonNegativeInteger : 1
      {any attributes with non-schema namespace . . .}>
      Content: (annotation?, (element | group | choice | sequence | any)*)
    </choice>
    
    <sequence
      id = ID
      maxOccurs = (nonNegativeInteger | unbounded)  : 1
      minOccurs = nonNegativeInteger : 1
      {any attributes with non-schema namespace . . .}>
      Content: (annotation?, (element | group | choice | sequence | any)*)
    </sequence>
    

    So you can instead wrap your xs:all in either xs:choice or xs:sequence, e.g:

      <xs:element minOccurs="0" maxOccurs="1" name="Markup">
        <xs:complexType>
          <xs:sequence>
            <xs:any processContents="lax" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    

    and your XSD adhere to the allowed content models.

    XSD 1.1

    Yes, xs:any can be in a xs:all in XSD:

    <all
      id = ID
      maxOccurs = (0 | 1) : 1
      minOccurs = (0 | 1) : 1
      {any attributes with non-schema namespace . . .}>
      Content: (annotation?, (element | any | group)*)
    </all>
    

    However, note that the XSD processor must be XSD 1.1 conformant; the error you've posted suggests that your tool only supports XSD 1.0.