【问题标题】:same element name with different complex types具有不同复杂类型的相同元素名称
【发布时间】:2016-09-19 15:09:50
【问题描述】:

此代码是组合在一起的大量 xsd 文件的一部分..

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Headers"
    targetNamespace="http://HttpNamespace/types"
    elementFormDefault="qualified"
    xmlns="http://HttpNamespace/types"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:saml ="http://HttpNamespace/saml"

>
  <xs:import namespace="http://HttpNamespace/saml" schemaLocation="Saml.xsd" />

  <xs:complexType name="ConnectionHeader">
    <xs:attribute name="key" fixed="Connection" />
    <xs:attribute name="value" >
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="keep-alive" />
          <xs:enumeration value="close" />
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>

  <xs:complexType name="ContentTypeHeader">
    <xs:attribute name="key" fixed="Content-Type" />
    <xs:attribute name="value" >
      <xs:simpleType>
        <xs:restriction base="xs:string">
          <xs:enumeration value="application/json" />
          <xs:enumeration value="application/soap+xml" />
          <xs:enumeration value="text/xml" />
        </xs:restriction>
      </xs:simpleType>
    </xs:attribute>
  </xs:complexType>


 <xs:complexType name="AuthorizationHeader">
   <xs:sequence>
    <xs:element name="Saml" minOccurs="0" maxOccurs="1" type="saml:SamlContent"/>
   </xs:sequence>
    <xs:attribute name="key" fixed="Authorization"/>
 </xs:complexType>


  <xs:complexType name="HeadersType">
    <xs:sequence>
      <xs:element name="Header" maxOccurs="unbounded">
        <xs:complexType>
          <xs:choice>
              <xs:element name="Header" minOccurs="0" maxOccurs="1" type="ConnectionHeader" />
              <xs:element name="Header" minOccurs="0" maxOccurs="1" type="ContentTypeHeader" />
              <xs:element name="Header" minOccurs="0" maxOccurs="1" type="AuthorizationHeader" />
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

</xs:schema>

正如你所看到的,在复杂类型 HeadersType 中,我试图在 3 种具有相同元素名称“Header”的复杂类型之间进行选择。所有 3 种类型都在这段代码 sn-p 中定义。

但是编辑一直给我抛出错误:

元素 Header 的多个定义导致内容模型 变得模棱两可

所以我的问题是我如何定义具有不同复杂类型的同一元素的多项选择?

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    你不能。约束“元素声明一致”表示如果两个兄弟元素(或具有相同父元素的两个替代元素)具有相同的名称,则它们必须具有相同的类型。无论您使用的是 XSD 1.0 还是 XSD 1.1,都是如此。

    您要么必须更改内容模型(例如,使用不同的元素名称),要么必须使用当前允许的类型的联合定义 Header 元素,并使用其他方式来验证约束(例如 XSD 1.1 断言)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-13
      相关资源
      最近更新 更多