2

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?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Lewis Cianci
  • 926
  • 1
  • 13
  • 38

2 Answers2

4

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.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • So, what does it mean if a vendor has given me an XSD with a structure like this? Does it mean the XSD itself is invalid, or that it is just doesn't conform with the Microsoft spec for XSD that can be converted with svcutil.exe? I'm trying to differentiate between whether the XSD is flat out invalid, or if it just isn't valid for the purpose I am using it for (svcutil). Thank you for your answer also :) – Lewis Cianci Jul 23 '18 at 00:02
  • I've attempted to read the specification that you have linked, and it's great. I'm trying to find a line in it or something that specifically asserts that xs:any can't appear in xs:all. I'm trying to tell a vendor that their XSD is invalid and the spec is going to prove that pretty well. I know you're not my slave and if I could read the spec and get it out myself I definitely would. Thanks again for your help :) – Lewis Cianci Jul 23 '18 at 00:10
  • Thanks, thats really helpful. It's a brand new world of XML/XSD validation for me. – Lewis Cianci Jul 23 '18 at 02:49
  • How do you know what version the XSD is? The top of the XSD says this "". Is Version="1.0" enough to certify that this is XSD 1.0? – Lewis Cianci Jul 23 '18 at 02:53
  • No, the version in the XML declaration is the version of the XML, not of the XSD. You can specify the intended version for the XSD via [**`xs:schema/@version`**](https://stackoverflow.com/q/44214615/290085), however your XSD processor (`svcutil.exe` in this case) itself has to support XSD 1.1, and evidently it does not. – kjhughes Jul 23 '18 at 02:58
  • *I'm trying to find a line in it or something that specifically asserts that xs:any can't appear in xs:all.* You'll find this rule in the "schema for schemas" (s4s), appendix A. The s4s is normative and its rules aren't generally repeated in the narrative prose. It asserts (see type `xs:allModel`) that the only child allowed for xs:all is xs:element. But if you want to convince the schema author that it's invalid, they're more likely to be persuaded by the results of running it through 3 or 4 different schema processors. – Michael Kay Jul 23 '18 at 08:08
0

Whether or not xs:any can appear under an xs:all type in an XSD, I still don't know. But I do know now that according to https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/data-contract-schema-reference you can't use svcutil to generate data contracts from the XSD's.

xs:complexType contents specifically lists xs:all as forbidden.... so my XSDs' would never generate a data contract with svcutil.exe.

Lewis Cianci
  • 926
  • 1
  • 13
  • 38