加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 如何覆盖父/扩展元素中的Xsd元素

发布时间:2020-12-16 07:43:15 所属栏目:百科 来源:网络整理
导读:我正在我公司创建一个新的数据交换服务.我们想扩展在我们的core.xsd定义文件中定义的现有对象.这是我需要做的一个例子: xs:complexType name="parentType" xs:sequence xs:element name="departmentName" type="core:DEPARTMENT_NAME" minOccurs="0" maxOcc
我正在我公司创建一个新的数据交换服务.我们想扩展在我们的core.xsd定义文件中定义的现有对象.这是我需要做的一个例子:
<xs:complexType name="parentType">
  <xs:sequence>
    <xs:element name="departmentName" type="core:DEPARTMENT_NAME" 
                minOccurs="0" maxOccurs="1" />    
  </xs:sequence>
</xs:complexType>

<xs:complexType name="childType">
  <xs:complexContent>
    <xs:extension base="parentType">
      <xs:sequence>
        <xs:element name="departmentName" 
                    type="core:DEPARTMENT_NAME" 
                    minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

我认为这是完美的.我想覆盖父元素并使其成为必需.但是,一个有效的xml文件就是这样.现在有一个额外的部门名称!

<childType>
  <departmentName>HR</departmentName>
  <departmentName>IT</departmentName>
</childType>

我如何做到这一点,使XML文件变成:

<childType>
  <departmentName>IT</departmentName>
</childType>

谢谢,
克雷格

您需要使用限制而不是扩展名.这将是您指定的方案的完整有效模式(我已经自由地使用命名空间使其有效).
<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:core="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="parentType">
        <xs:sequence>
            <xs:element name="departmentName" type="core:DEPARTMENT_NAME" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="childType">
        <xs:complexContent>
            <xs:restriction base="parentType">
                <xs:sequence>
                    <xs:element name="departmentName" type="core:DEPARTMENT_NAME"/>
                </xs:sequence>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>

    <xs:simpleType name="DEPARTMENT_NAME">
        <xs:restriction base="xs:string"/>
    </xs:simpleType>

    <xs:element name="childType" type="childType"/>
</xs:schema>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读