Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 427
Q8Y Avatar asked Apr 05 '12 08:04

Q8Y


People also ask

What is a complex type in XSD?

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.

What is an XML complex type element?

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.

What is XSD schema?

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.

What is XML Schema?

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.


1 Answers

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>
like image 158
Nick Ryan Avatar answered Oct 19 '22 14:10

Nick Ryan