By default, when creating an element or an attribute, you can give it any value you want. In fact, you create an element on one section of the document and give it a value, then create the same element in another section of the document but not give it a value. Here is an example: <?xml version="1.0" encoding="utf-8" ?>
<students>
<student>
<firstname>John</firstname>
<lastname>Sandt</lastname>
<gender>Male</gender>
</student>
<student>
<firstname>Sherrie</firstname>
<lastname>Springfield</lastname>
<gender />
</student>
</students>
If you have an element or an attribute that usually receive the same value, you can specify a default value for it. To do this, when creating the schema of the element or attribute, add a flag named default and assign the desired value to it. Here is an example: <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:element name="gender" type="xs:string" default="Male" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Consider the following XML document: <?xml version="1.0" encoding="utf-8" ?>
<products>
<product>
<number>283974</number>
<dateacquired>2011-05-12</dateacquired>
<name>Shirt with Epaullette</name>
<size>14</size>
<unitprice>59.95</unitprice>
</product>
<product>
<number>485117</number>
<dateacquired>2011-05-12</dateacquired>
<name>Striped Pants</name>
<size>Medium</size>
<unitprice>78.50</unitprice>
</product>
</products>
Notice that the product element has two child nodes. When creating an XML document, you must want to require that the a certain element have at least a certain number of child elements. In that case, if the element has less than that number of child elements, the document would be in violation of the rule and therefore would be valid. To specify that an element must have at least a certain number of child node, when creating its schema tag, add an attribute named minOccurs and assign the desired number to it. Here is an example: <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="4" name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="number" type="xs:unsignedInt" />
<xs:element name="dateacquired" type="xs:date" />
<xs:element name="name" type="xs:string" />
<xs:element name="size" type="xs:string" />
<xs:element name="unitprice" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This code indicates that the products element must have at least 4 child nodes. Since the products element in the above XML document has only two child nodes, the element violates the rule and would produce an error.
When creating an XML document, by default, you can create as many elements as you want and each element can have as many child nodes as necessary. If you want, you can limit the number of child nodes an element can have. To do this, when creating the schema tag of the element, add an attribute named maxOccurs and assign the desired value to it. The value can be either an insigned integer or unbounded. This can be done as follows: <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="4" name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="number" type="xs:unsignedInt" />
<xs:element name="dateacquired" type="xs:date" />
<xs:element name="name" type="xs:string" />
<xs:element name="size" type="xs:string" />
<xs:element name="unitprice" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
In this case, the products element must not have more than 4 child nodes. In the XML document, if you create more child nodes for the element than the number specified in maxOccurs, the document would be violating the riule and would be considered invalid. Since the minOccurs and the maxOccurs flags are not mutually exclusive, you can use them both on the same element. Here is an example: <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="6" name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="number" type="xs:unsignedInt" />
<xs:element name="dateacquired" type="xs:date" />
<xs:element name="name" type="xs:string" />
<xs:element name="size" type="xs:string" />
<xs:element name="unitprice" type="xs:decimal" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Consider the following XML document: <?xml version="1.0" encoding="utf-8" ?>
<products>
<product number="283974">
<dateacquired>2011-05-12</dateacquired>
<name>Shirt with Epaullette</name>
<unitprice>59.95</unitprice>
</product>
<product>
<dateacquired>2011-05-12</dateacquired>
<name>Striped Pants</name>
<unitprice>78.50</unitprice>
</product>
</products>
Notice that the first product element has an attribute named number while the second product element does not have that attribute. When creating the schema of an element, you can ask that a certain attribute be required. To take care of this, when creating the schema of the attribute, add an attribute named use and assign a value named required to it. Here is an example: <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="dateacquired" type="xs:date" />
<xs:element name="name" type="xs:string" />
<xs:element minOccurs="0" name="size" type="xs:string" />
<xs:element name="unitprice" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="number" type="xs:unsignedInt" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Once this flag is set, whenever the element of that attribute is created, that attribute must be added. Otherwise, the element would violate the rule.
Once again, consider the above XML document. Notice that the first product element has an attribute named number but the second product element does not have that attribute. When creating the schema of an element, you can add a certain attribute to a certain element but omit that attribute for the same element in another section of the document. In this cas, the attribute is said to be optional. To indicate this, when creating the schema of the attribute, add the use attribute and assign a value named optional to it. Here is an example: <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="dateacquired" type="xs:date" />
<xs:element name="name" type="xs:string" />
<xs:element name="unitprice" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="number" type="xs:unsignedInt" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The optional value is the default value of the use flag.
If you want to prevent the use of an element from a certain element in your XML document, create its tag in the XML schema document. Add both the minOccurs and the maxOccurs attributes and set the value of each to 0. Here is an example: <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="product">
<xs:complexType>
<xs:sequence>
<xs:element name="dateacquired" type="xs:date" />
<xs:element name="name" type="xs:string" />
<xs:element name="unitprice" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="number" type="xs:unsignedInt" use="optional" />
</xs:complexType>
</xs:element>
<xs:element name="closedcaption" minOccurs="0" maxOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
To prevent the use of a certain attribute, in the schema document, create its tag and add the use attribute. Assign a value as prohibited to itnamed onsider the above
Consider the following document: <?xml version="1.0" encoding="utf-8" ?>
<students>
<student>
<firstname>John</firstname>
<lastname>Sandt</lastname>
<gender>Male</gender>
</student>
<student>
<firstname>Sherrie</firstname>
<lastname>Springfield</lastname>
<gender>Female</gender>
</student>
</students>
Notice that the first gender element has a value of Male and the second has a value of Female. We could have created one more gender element and give it any value. In a typical XML document, especially in the absence of a schema, you can give any value to an element. In some cases, you can specify a constant value that must always be used for an element. To do this, when creating the schema tag of the element, add an attribute named fixed and assign the desired value to it. Here is an example: <?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="student">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:element name="gender" type="xs:string" fixed="Female" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Once this is done, if a new element is created and it uses a value other than the one specified in the fixed flag, the document would be violating the rule. The fixed flag can also be applied to an attribute, in which case you would require that an attribute always carry a certain value. In fact, you can add the use flag and specify whether the attribute must be required or it would be optional. |
|
|||||||||||||||||
|
|