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

时间:2022-10-02 17:19:59

I'm creating a new dataexchange service at my company. We would like to extend an existing object that is defined in our core.xsd definitions file. Here is an example of what I need to do:

我正在我的公司创建一个新的dataexchange服务。我们想扩展在我们的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>

I think this makes perfect sense. I want to override the parent element and make it required. However, a valid xml file would be this. Where there is now an extra department name!?

我觉得这很有道理。我想覆盖父元素并使其成为必需。但是,有效的xml文件就是这个。现在有一个额外的部门名称!?

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

How can I do this so that the XML file would become:

我该怎么做才能使XML文件成为:

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

Thanks, Craig

谢谢,克雷格

1 个解决方案

#1


7  

You need to use restriction instead of extension. This would be a full valid schema for the scenario you indicated (I've liberally used namespaces to make it valid).

您需要使用限制而不是扩展。这将是您指定的场景的完整有效架构(我已经大量使用命名空间使其有效)。

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

#1


7  

You need to use restriction instead of extension. This would be a full valid schema for the scenario you indicated (I've liberally used namespaces to make it valid).

您需要使用限制而不是扩展。这将是您指定的场景的完整有效架构(我已经大量使用命名空间使其有效)。

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