首页 > 解决方案 > xs:any 可以在 XSD 中的 xs:all 元素中吗?

问题描述

我正在尝试从提供的 XSD 生成数据合同。 Svcutil.exe向我抛出这个错误:

'http://www.w3.org/2001/XMLSchema:any'在这种情况下不支持该元素。”

在 XSD 中查看,类型元素any出现了两次。这是它第一次出现。

 <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>

从我对此的研究来看,似乎xs:any不能在一个xs:all元素中。但我找不到明确表明这一点的规范或等效物。

可以xs:any出现在一个xs:all?还是无效?

标签: xmlxsdxsd-validationxml-validationsvcutil.exe

解决方案


XSD 1.0

xs:any 不能xs:allXSD 中:

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

xs:any可以在 a xs:choiceor中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>

因此,您可以改为将您的包装xs:allxs:choiceorxs:sequence中,例如:

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

并且您的 XSD 遵守允许的内容模型。

XSD 1.1

的,xs:any 可以xs:allXSD 中:

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

但是,请注意 XSD 处理器必须符合XSD 1.1;您发布的错误表明您的工具仅支持XSD 1.0


推荐阅读