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>
XSD - Complex Types. Complex Element is an XML element which can contain other elements and/or attributes. We can create a complex element in two ways −. Define a complex type and then create an element using the type attribute.
A complex type element is an XML element that contains other elements and/or attributes. choice|sequence)?, ( (attribute|attributeGroup)*,anyAttribute?)))) (The ? sign declares that the element can occur zero or one time, and the * sign declares that the element can occur zero or more times inside the complexType element) Optional.
It means that all the XML Standards are defined in the XSD and they are written in XML. It is recommended by W3C to replace the Document Type Definition (DTD). The schema defines their types or built-in types.
It means that all the XML Standards are defined in the XSD and they are written in XML. It is recommended by W3C to replace the Document Type Definition (DTD). The schema defines their types or built-in types. XML Schema is also allowed to check whether a given XML document is valid with syntactic Confirmation.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With