XSD 允许同一元素的 simpleType 和 complexType 内容? [英] XSD allowing both simpleType and complexType content for same element?

查看:25
本文介绍了XSD 允许同一元素的 simpleType 和 complexType 内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我有不同的 XML,它们将具有不同类型的属性.有时元素 HEADER 可能只有一个节点,或者某些 XML 可能在 HEADER 节点中包含元素并在其中包含值.

I have a situation where I have different XMLs that will have different types of properties. Sometimes the element HEADER could have just a node or some XMLs could have elements within the HEADER node and values inside.

示例 1(HEADER 仅包含文本):

Example 1 (HEADER with just text):

<Details HeaderLabel="DETAILS">
   <HEADER Label="Header">2.5%</HEADER>
</Details>

示例 2(HEADER 有两个子元素):

Example 2 (HEADER with two child elements):

<Details HeaderLabel="DETAILS">
   <HEADER Label="Header">
       <HEAD Label="H1a">2.88%</HEAD>
       <HEAD Label="H2b">3.24%</HEAD>
   </HEADER>
</Details>

XSD 是这样工作的:这将验证 示例 1:

The XSD works as so: This will validate for example 1:

<xs:element name="HEADER">
   <xs:complexType>
      <xs:simpleContent>
         <xs:extension base="xs:string">
            <xs:attribute name="Label" type="xs:string" use="required" />
          </xs:extension>
       </xs:simpleContent>
    </xs:complexType>
 </xs:element>

这将验证 示例 2:

<xs:element name="HEADER">
   <xs:complexType>
      <xs:sequence>
         <xs:element maxOccurs="unbounded" name="HEAD">
           <xs:complexType>
             <xs:simpleContent>
               <xs:extension base="xs:string">
                 <xs:attribute name="Label" type="xs:string" use="required" />
               </xs:extension>
             </xs:simpleContent>
           </xs:complexType>
         </xs:element>
       </xs:sequence>
     <xs:attribute name="Label" type="xs:string" use="required" />
   </xs:complexType>
 </xs:element>

我尝试使用 xs:choice 但它似乎效果不佳,或者我可能对如何在这种情况下实现选择没有清晰的了解.

I tried using xs:choice but it didn't seem to work well or maybe I don't have a clear understanding on how to implement choice in this situation.

推荐答案

在 XSD 中,你不能同时允许简单和复杂的内容,除非你愿意通过 mixed="true" 来混合元素和文本(在这种情况下不需要示例 1).您可以然后使用 XSD 1.1 断言来排除两者同时出现.

In XSD, you cannot allow both simple and complex content unless you're willing to have mix elements and text via mixed="true" (in this case Example 1 is not needed). You could then used XSD 1.1 assertions to exclude both from appearing simultaneously.

<xs:element name="HEADER">
<xs:complexType mixed="true">
  <xs:sequence>
    <xs:element minOccurs="0" maxOccurs="unbounded" name="HEAD">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute name="Label" type="xs:string" use="required" />
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
  <xs:attribute name="Label" type="xs:string" use="required" />
</xs:complexType>
</xs:element>

但是,您在这里逆流而上.相反,接受您确实有两个具有两种不同内容模型的不同实体,并以不同的方式命名不同的实体:SIMPLE_HEADERCOMPLEX_HEADER 浮现在脑海中.然后你可以在 Details 上使用 xs:choice/maxOccurs="unbounded" 让简单和复杂的 headers 可以自由穿插.

However, you're swimming against the current here. Instead, accept that you really have two different entities with two different content models and name the different entities differently: SIMPLE_HEADER and COMPLEX_HEADER comes to mind. Then you can use xs:choice/maxOccurs="unbounded" on Details to allow simple and complex headers to be freely interspersed.

这篇关于XSD 允许同一元素的 simpleType 和 complexType 内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆