2081 2

BIT251 ยท 2081 ยท 10 marks

Explain the XML syntax rules.Create a XML file that contains a root element named and โ€œOfficeโ€. Inside this root element create four "Employee" elements. Each "Employee" element must contain a "Skills" element and it can contain up to Three "Skills" elements. Now create an XML file for this scenario

Explain the XML syntax rules.Create a XML file that contains a root element named and โ€œOfficeโ€. Inside this root element create four "Employee" elements. Each "Employee" element must contain a "Skills" element and it can contain up to Three "Skills" elements. Now create an XML file for this scenario and write XSD showing the use of minOccurrence and maxOccurrence indicators.[5+5]

Web Technology IChapter 10: XML and Related TechnologiesXML elements and attributes

Quick answer

XML (eXtensible Markup Language) follows strict syntax rules. The key rules are: minOccurs="1" and maxO.

XML Syntax Rules and XSD with minOccurs/maxOccurs


(a) XML Syntax Rules

XML (eXtensible Markup Language) follows strict syntax rules. The key rules are:

1. Every XML Document Must Have a Root Element

All elements must be contained within a single root element.

<root> ... </root>

2. All Elements Must Have a Closing Tag

Unlike HTML, every opened tag must be properly closed.

<name>John</name>   <!-- correct -->
<name>John          <!-- incorrect -->

3. XML Tags are Case-Sensitive

<Employee> and <employee> are treated as different tags.

<Employee>John</Employee>   <!-- correct -->
<Employee>John</employee>   <!-- incorrect -->

4. Elements Must be Properly Nested

Tags must be opened and closed in the correct order (no overlapping).

<b><i>text</i></b>   <!-- correct -->
<b><i>text</b></i>   <!-- incorrect -->

5. XML Attribute Values Must be Quoted

All attribute values must be enclosed in single or double quotes.

<Employee id="101">   <!-- correct -->
<Employee id=101>     <!-- incorrect -->

The XML declaration should appear at the top of the document.

<?xml version="1.0" encoding="UTF-8"?>

7. Special Characters Must Use Entity References

Reserved characters must be escaped:

CharacterEntity Reference
<&lt;
>&gt;
&&amp;
"&quot;
'&apos;

8. Comments Syntax

<!-- This is a comment -->

9. White Space is Preserved

Unlike HTML, XML preserves white space within element content.

10. XML Names Cannot Start with a Number or Special Character

Tag names must begin with a letter or underscore.


(b) XML File for the Scenario

XML File (office.xml)

<?xml version="1.0" encoding="UTF-8"?>
<Office>

    <Employee id="E001">
        <Name>Alice Johnson</Name>
        <Department>Development</Department>
        <Skills>Java Programming</Skills>
        <Skills>Database Management</Skills>
        <Skills>Web Development</Skills>
    </Employee>

    <Employee id="E002">
        <Name>Bob Smith</Name>
        <Department>Design</Department>
        <Skills>UI Design</Skills>
        <Skills>Photoshop</Skills>
    </Employee>

    <Employee id="E003">
        <Name>Carol White</Name>
        <Department>Testing</Department>
        <Skills>Manual Testing</Skills>
        <Skills>Automation Testing</Skills>
        <Skills>Performance Testing</Skills>
    </Employee>

    <Employee id="E004">
        <Name>David Brown</Name>
        <Department>Management</Department>
        <Skills>Project Management</Skills>
    </Employee>

</Office>

Note:

  • Employee E001 has 3 Skills (maximum)
  • Employee E002 has 2 Skills
  • Employee E003 has 3 Skills (maximum)
  • Employee E004 has 1 Skill (minimum)

XSD File (office.xsd) - Showing minOccurs and maxOccurs

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- Definition of the root element "Office" -->
    <xs:element name="Office">
        <xs:complexType>
            <xs:sequence>

                <!-- Employee element must appear exactly 4 times -->
                <xs:element name="Employee" minOccurs="4" maxOccurs="4">
                    <xs:complexType>

                        <!-- Attribute for Employee ID -->
                        <xs:attribute name="id" type="xs:string" use="required"/>

                        <xs:sequence>

                            <!-- Name element must appear exactly once -->
                            <xs:element name="Name"
                                        type="xs:string"
                                        minOccurs="1"
                                        maxOccurs="1"/>

                            <!-- Department element must appear exactly once -->
                            <xs:element name="Department"
                                        type="xs:string"
                                        minOccurs="1"
                                        maxOccurs="1"/>

                            <!-- Skills element must appear at least 1 time
                                 and at most 3 times -->
                            <xs:element name="Skills"
                                        type="xs:string"
                                        minOccurs="1"
                                        maxOccurs="3"/>

                        </xs:sequence>
                    </xs:complexType>
                </xs:element>

            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

Explanation of minOccurs and maxOccurs

IndicatorMeaningUsed In XSD Above
minOccursMinimum number of times an element must appearSkills must appear at least 1 time
maxOccursMaximum number of times an element can appearSkills can appear at most 3 times
minOccurs="0"Element is optionalNot used here
maxOccurs="unbounded"Element can repeat unlimited timesNot used here

Key Points:

  • minOccurs="1" and `maxO

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.