Thiwanka
Thiwanka

Reputation: 99

how to validate maximum length only in one field in xsd

this is my sample xml.

<ReportedBy>
       <GivenName>amila</GivenName>
       <FamilyName />
       <MiddleInitials />          
</ReportedBy>

<AdmittingDoctor>
     <Uid>BISSEJ</Uid>
     <GivenName>JEAN-CLAUDE(ROH)</GivenName>
     <FamilyName>BISSERBE</FamilyName>
</AdmittingDoctor>

this is my xsd.

<xs:element name="ReportedBy">
    <xs:complexType>
        <xs:sequence>
            <xs:element ref="GivenName" minOccurs="0"/>
            <xs:element ref="FamilyName" minOccurs="0"/>
            <xs:element ref="MiddleInitials" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="AdmittingDoctor">
    <xs:complexType>
        <xs:sequence minOccurs="0">
            <xs:element ref="Uid" minOccurs="0"/>
            <xs:element ref="GivenName" minOccurs="0"/>
            <xs:element ref="FamilyName" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:simpleType name="reviewGivenName">  
    <xs:restriction base="xs:string">   
        <xs:maxLength value="15"/>  
    </xs:restriction>  
</xs:simpleType>
<xs:element name="GivenName" nillable="true">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="reviewGivenName">
                <xs:attribute type="xs:string" name="updatable"/>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:element>

in this xsd i have validated maximum length of the field "GivenName" using the above mentioned method.because of that maximum length of GivenName field is validated in both "AdmittingDoctor" and "ReportedBy" sections.

but i want to validate maximum length of "GivenName" field of "AdmittingDoctor" section only.how can i change above xsd to full fill my requirement? any help will be grateful.

Upvotes: 0

Views: 2013

Answers (1)

jhinghaus
jhinghaus

Reputation: 855

If you just want to limit the size, why not just use a simpleType:

 <xs:simpleType name="reviewGivenName">  
    <xs:restriction base="xs:string">   
        <xs:maxLength value="15"/>  
    </xs:restriction>  
 </xs:simpleType>  

<xs:element name="AdmittingDoctor">
    <xs:complexType>
        <xs:sequence minOccurs="0">
            <xs:element name="Uid" minOccurs="0"/>
            <xs:element name="GivenName" type="tns:reviewGivenName" minOccurs="0"/>
            <xs:element name="FamilyName" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="ReportedBy">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="GivenName" minOccurs="0"/>
            <xs:element name="FamilyName" minOccurs="0"/>
            <xs:element name="MiddleInitials" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

Upvotes: 3

Related Questions