简体   繁体   中英

How can I add a restriction to a complextype in XML (XSD) schema?

Can anyone help me to add a restriction to this schema file (for OwnerEnglishName)? I know how to do it with a simpletype, while in a complextype I don't know how to do it. Can anyone help?

Thanks a lot.

Original XML:

<PACIDemoSignedDoc PaciSDocID="HouseOwnerSignedEndorsement">  
  <OwnerEnglishName OENID="Name"></OwnerEnglishName>
</PACIDemoSignedDoc>

Schema (without restriction):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="PACIDemoSignedDoc" type="PACIDemoSignedDocType" />
  <xs:complexType name="PACIDemoSignedDocType">
    <xs:sequence>
      <xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />
    </xs:sequence>
    <xs:attribute name="PaciSDocID" type="xs:string" />
  </xs:complexType>
  <xs:complexType name="OwnerEnglishNameType">
    <xs:attribute name="OENID" type="xs:string" />
  </xs:complexType>
</xs:schema>

The restriction code:

<xs:restriction base="xs:string">
  <xs:minLength value="5"/>
  <xs:maxLength value="100"/>
</xs:restriction>

This will do it:-

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="PACIDemoSignedDoc" type="PACIDemoSignedDocType" />
    <xs:complexType name="PACIDemoSignedDocType">
        <xs:sequence>
            <xs:element name="OwnerEnglishName" type="OwnerEnglishNameType" />
        </xs:sequence>
        <xs:attribute name="PaciSDocID" type="xs:string" />
    </xs:complexType>
    <xs:complexType name="OwnerEnglishNameType">
        <xs:simpleContent>
            <xs:restriction base="NameType">
                <xs:minLength value="5"/>
                <xs:maxLength value="10"/>
            </xs:restriction>
        </xs:simpleContent>
    </xs:complexType>
    <xs:complexType name="NameType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="OENID" type="xs:string" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>
</xs:schema>

Here is sample acceptable XML with this schema

<?xml version="1.0" encoding="UTF-8"?>
<PACIDemoSignedDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" PaciSDocID="gggg">
    <OwnerEnglishName OENID="9999">GGGGG</OwnerEnglishName>
</PACIDemoSignedDoc>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM