4

I need to restrict the Age element in PersonalDetailsType, such that it accepts only integer values. Changes should not be made in canonical.xsd. All the modification has to be made in Extn.xsd

I tried many ways, but not able to put restriction.So, thought to come here.At the bottom you can find the required XML that should obey Extn.xsd.

Thanks in advance..

canonical.xsd

      <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema attributeFormDefault="unqualified" 
    elementFormDefault="qualified" 
    targetNamespace="http://www.example.org/cononical" 
    xmlns:con="http://www.example.org/cononical"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:element name="Employees">
        <xs:complexType>
          <xs:sequence>
          <xs:element ref="NameDeatils"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>

<xs:element name="NameDeatils" type="con:PersonalDetailsType"/>       

<xs:complexType name ="PersonalDetailsType" >
                <xs:sequence >
                  <xs:element type="xs:string" name="Name"/>
                  <xs:element ref="con:Age"/>
                </xs:sequence>
       </xs:complexType>



       <xs:element name="Age" type="con:AgeType"></xs:element>

       <xs:simpleType name="AgeType">
       <xs:restriction base="con:BaseType">
       </xs:restriction>
       </xs:simpleType>

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

    </xs:schema>

Extn.xsd

    <xs:schema attributeFormDefault="unqualified" 
targetNamespace="http://www.example.org/cononicalExtn"
xmlns:conExtn="http://www.example.org/cononicalExtn"
xmlns:con="http://www.example.org/cononical"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:import namespace="http://www.example.org/cononical"  schemaLocation ="canonical.xsd"/>

  <xs:element name="EmployeeDetails">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="AddressDetails">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:short" name="Flat"/>
              <xs:element type="xs:string" name="Place"/>
              <xs:element ref ="conExtn:PersonalInfo"/> 
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>


   <xs:complexType name="PersonalInfoType">
    <xs:sequence>
          <xs:element ref ="con: NameDeatils” />
    </xs:sequence>
    </xs:complexType>


  <xs:element name="PersonalInfo" type="conExtn:PersonalInfoType" /> 

</xs:schema>

Required XML

<EmployeeDetails>
    <AddressDetails>
        <Flat>123</Flat>
        <Place>India</Place>
        <PersonalInfo>
    <NameDeatils>
            <Name>Ram</Name>
            <Age>12</Age>
    </NameDeatils>
        </PersonalInfo>
    </AddressDetails>
</EmployeeDetails>
Habin
  • 792
  • 1
  • 11
  • 21
  • Do you *have* to defined `` as derived from `con:AgeType`? Can't you just defined it as `xs:positiveInteger`? – Andreas Aug 24 '15 at 18:42
  • I'm not suppose to change in canonical.xsd – Habin Aug 25 '15 at 03:24
  • Then your question is confusing, because you're defining a new `PersonalInfoType` type, distinct from the `PersonalDetailsType` type defined in canonical.xsd. As such, you can define it any way you like. – Andreas Aug 25 '15 at 03:28

1 Answers1

1

You cannot redefine the Age type to have a different type than canonical (See XSD: How to redefine the data type of a simpleType eg. from xs:string to xs:integer)

You can use xs:redefine to restrict AgeType to be 'more integer like' (i.e. no alphabet characters, min length=1 etc) but that's about as far as you can go.

If the Age element was defined as a more generic type, like xs:string, then you cannot redefine at all because xs:redefine only works on types, not element (see Is it possible to redefine something defined as xsd:element?)

You may have better luck using an Override:

Difference between xs:redefine and xs:override in XML schema 1.1

Community
  • 1
  • 1
Trent Bartlem
  • 2,213
  • 1
  • 13
  • 22