Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Override Xsd element inside of parent/extended element

Tags:

xml

xsd

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:

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

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

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

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

Thanks, Craig

like image 589
Craig Avatar asked Dec 19 '12 12:12

Craig


People also ask

How an element can be defined within as XSD?

Each element definition within the XSD must have a 'name' property, which is the tag name that will appear in the XML document. The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document.

What is minInclusive in XSD?

minInclusive. Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) minLength.

What is Nillable in XSD?

The nillable attribute can be defined on an xsd:element within an XML schema. It specifies that the xsi:nil attribute is valid for the element. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute and is included in the document, however, its values are nullified.

What is complexType in XSD?

The complexType element defines a complex type. A complex type element is an XML element that contains other elements and/or attributes.


1 Answers

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>
like image 133
Petru Gardea Avatar answered Nov 23 '22 08:11

Petru Gardea