Download presentation
Presentation is loading. Please wait.
Published byBranden Riley Modified over 6 years ago
1
{ XML Technologies } BY: DR. M’HAMED MATAOUI Chapter III: XML Grammars
2
Overview of Presentation
Introduction to XML Technologies Fundamental Concepts of XML XML Grammars DTD Specification W3C-Schema Language RELAX NG XML document transformations (CSS, XSLT) XPath, XQuery Processing XML (DOM & SAX) Overview of Presentation [ Dr. M’hamed MATAOUI ]
3
Need for Normalization of data exchange
Role of validation Solution Need for Normalization of data exchange Company 2 Linux ? The validation will enhance the data exchange quality by forcing the data transmitter and the data consumer to check the consistency of structured data in XML. by consistency, is to be understood both vocabulary (elements, attributes, and namespace) but also the order and quantities (repetitions) Ultimately, validation amounts to establishing a visa on the XML document. ?! Company 3 Windows ?? Company 1 MacOS XML Grammars [ Dr. M’hamed MATAOUI ]
4
Role of validation Solution
Need for Normalization of data exchange The validation will enhance the data exchange quality by forcing the data transmitter and the data consumer to check the consistency of structured data in XML. by consistency, is to be understood both vocabulary (elements, attributes, and namespace) but also the order and quantities (repetitions) Ultimately, validation amounts to establishing a visa on the XML document. Most of XML tools, i.e parsers, offer validation components One or many validation methods (DTD, W3C Schema, RelaxNG, etc.) It is generally difficult to check the validity of large XML documents (mount the whole document structure on memory) XML Grammars [ Dr. M’hamed MATAOUI ]
5
Structure of an XML document
Well-formed and Valid Documents: a DTD or W3C schema, … XML Documents Must Have a Root Element All XML Elements Must Have a Closing Tag XML Tags are Case Sensitive XML Elements Must be Properly Nested XML Naming Rules XML Attribute Values Must be Quoted Entity References (characters with a special meaning) + Well-formed Valid Documents XML documents that conform to the syntax rules above are said to be "Well Formed" XML documents. A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules defined by a model XML Grammars [ Dr. M’hamed MATAOUI ]
6
Validation Method 1: DTD
Prolog (Optional declaration) XML header DTD Header <?xml … ?> <!DOCTYPE root_element [ … ]> DTD (Document Type definition) XML Grammars [ Dr. M’hamed MATAOUI ]
7
Validation Method 1: DTD
DTD (Document Type Definition): Internal External Syntax : <!DOCTYPE root_element SYSTEM "URI"> Example: <!DOCTYPE course SYSTEM "course.dtd"> Cette déclaration optionnelle sert à attacher une grammaire de type DTD (Document Type Definition) à votre document XML. Elle est introduite avant la première balise (racine) de votre document sous cette forme : racine est le premier élément (la première balise). L’URI peut être absolue ou relative au document. Il est généralement préférable soit d’utiliser une URI relative, pour pouvoir déplacer le document XML et sa grammaire sans difficulté, ou bien d’exploiter une URL disponible depuis n’importe quel endroit (sur Internet/Intranet, par exemple). XML Grammars [ Dr. M’hamed MATAOUI ]
8
Validation Method 1: DTD
Internal DTD (within XML document) <!DOCTYPE course [ <!ELEMENT course (Lecturer, plan)> <!ELEMENT Lecturer EMPTY> <!ELEMENT plan (chapter+)> <!ELEMENT chapter (#PCDATA)> <!ATTLIST course title CDATA #REQUIRED> <!ATTLIST Lecturer name CDATA #REQUIRED> ]> Content of an external DTD file XML Grammars [ Dr. M’hamed MATAOUI ]
9
Validation Method 1: DTD
XML File XML Grammars [ Dr. M’hamed MATAOUI ]
10
Validation Method 1: DTD
XML File DTD File (course.dtd) XML Grammars [ Dr. M’hamed MATAOUI ]
11
Element definition in a DTD
Any element “e” (element-name) to be used in the XML vocabulary needs to be defined via two forms: ANY, EMPTY <!ELEMENT element-name category> or <!ELEMENT element-name (element-content)> XML Grammars [ Dr. M’hamed MATAOUI ]
12
Element definition in a DTD
Any element “e” to be used in the XML vocabulary needs to be defined via: <!ELEMENT element-name EMPTY> <!-- must be empty:closed tag --> <!ELEMENT e ANY> <!-- may contain any content --> <!ELEMENT e cm> XML Grammars [ Dr. M’hamed MATAOUI ]
13
Element definition in a DTD
Examples with : EMPTY With this DTD declaration : « <!ELEMENT br EMPTY> » we can have these instances (must take into account ATTLIST definition): <br></br> <br/> <br att1="value1" /> <br att1="value1" att2="value2" … /> XML Grammars [ Dr. M’hamed MATAOUI ]
14
Element definition in a DTD
<!ELEMENT element-name (element-content)> XML Grammars [ Dr. M’hamed MATAOUI ]
15
Element definition in a DTD
Elements with only parsed character data (PCDATA) <!ELEMENT p (#PCDATA)> <p>bla bla bla …</p> <p align="center">bla bla bla …</p> XML Grammars [ Dr. M’hamed MATAOUI ]
16
Element definition in a DTD
Elements with Children (sequences) : <!ELEMENT element-name (child1,child2,...)> DTD: <!ELEMENT course (Lecturer, plan)> <!ELEMENT Lecturer EMPTY> <!ELEMENT plan (chapter+)> … We must respect order XML File 1: <course> <Lecturer …></Lecturer> <plan …> … </plan> </course> XML File 2: <course> <plan …> … </plan> <Lecturer …></Lecturer> </course> But Not XML Grammars [ Dr. M’hamed MATAOUI ]
17
Element definition in a DTD
Elements with Children (sequences) : <!ELEMENT element-name (child1|child2)> DTD: <!ELEMENT contact (tel| )> <!ELEMENT tel (#PCDATA)> <!ELEMENT (#PCDATA)> … XML File 1: <contact> <tel> </tel> </contact> XML File 3: <contact> <tel> </tel> </contact> XML File 2: <contact> </contact> But Not XML Grammars [ Dr. M’hamed MATAOUI ]
18
Element definition in a DTD
Elements with Children (sequences) : <!ELEMENT element-name (child1+)> DTD: <!ELEMENT plan (chapter+)> XML File 1: <plan> <chapter>Introduction to XML … </chapter> </plan> XML File 2: <plan> <chapter>Introduction to XML … </chapter> <chapter>Fundamental Concepts … </chapter> </plan> XML File 3: <plan> <!-- No chapter sub elements--> </plan> But Not XML Grammars [ Dr. M’hamed MATAOUI ]
19
Element definition in a DTD
Elements with Children (sequences) : <!ELEMENT element-name (child1*)> DTD: <!ELEMENT plan (chapter*)> XML File 1: <plan> <chapter>Introduction to XML … </chapter> </plan> XML File 2: <plan> <chapter>Introduction to XML … </chapter> <chapter>Fundamental Concepts … </chapter> </plan> XML File 3: <plan> <!-- No chapter sub elements--> </plan> But Also XML Grammars [ Dr. M’hamed MATAOUI ]
20
Element definition in a DTD
Elements with Children (sequences) : <!ELEMENT element-name (child1*)> DTD: <!ELEMENT plan (chapter?)> XML File 1: <plan> <chapter>Introduction to XML … </chapter> </plan> OR XML File 2: <plan> <!-- No chapter sub elements--> </plan> XML Grammars [ Dr. M’hamed MATAOUI ]
21
To do (Now!!!) Model using the DTD syntax (definition of XML elements) the following statement: A company is identified by the following information in order: name, manager, addresses and Employees A company should have only one manager A manager is identified in order by: name, telephone (optional) and (optional). A company can have one or more addresses A company should have at least one employee Employees of a company are identified by the following information in order: name, position, telephone (optional) and (optional). A position is defined by one of these two information : direction or office All leaf nodes are of textual nature (PCDATA) XML Grammars [ Dr. M’hamed MATAOUI ]
22
To do (Solution!!!) XML Grammars
<!ELEMENT company (company_name, manager, addresses, employees)> <!ELEMENT manager (name, telephone?, ?)> <!ELEMENT addresses (address+)> <!ELEMENT employees (employee+)> <!ELEMENT employee (name, position, telephone?, ?)> <!ELEMENT position (direction | office)> <!ELEMENT company_name (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT telephone (#PCDATA)> <!ELEMENT (#PCDATA)> <!ELEMENT address (#PCDATA)> <!ELEMENT direction (#PCDATA)> <!ELEMENT office (#PCDATA)> XML Grammars [ Dr. M’hamed MATAOUI ]
23
Attributes definition in a DTD
Declaring Attributes: <!ATTLIST element-name attribute-name attribute-type attribute-value> DTD: <!ATTLIST chapter lang CDATA "fr"> XML File 1: <plan> <chapter lang="en">Introduction to XML … </chapter> </plan> XML Grammars [ Dr. M’hamed MATAOUI ]
24
Attributes definition in a DTD
Declaring multiple Attributes: <!ATTLIST person person_name CDATA #REQUIRED person_id CDATA #REQUIRED person_adress CDATA #IMPLED > XML File: <person person_name="mohamed" person_id="001" person_adress="bordj el bahri, algiers"> … </person> XML Grammars [ Dr. M’hamed MATAOUI ]
25
Attributes definition in a DTD
<!ATTLIST element-name attribute-name attribute-type attribute-value> XML Grammars [ Dr. M’hamed MATAOUI ]
26
Attributes definition in a DTD
Example: <!ATTLIST course title CDATA #REQUIRED> <!ATTLIST element-name attribute-name attribute-type attribute-value> Example: <!ATTLIST chapter lang (fr | en | ar) #REQUIRED> Example: <!ATTLIST student_name student_no ID #REQUIRED> <lab_group> <person person_no="a100">Mhamed MATAOUI</person> <person person_no="a100">Faouzi Sebbak</person> </lab_group> False !!! Not allowed ( must define different IDs) Allows multiple ID values separated by whitespace XML Grammars [ Dr. M’hamed MATAOUI ] Example: <!ATTLIST person person_no ID #REQUIRED> <!ATTLIST person tutor_1 IDREF #IMPLIED> <!ATTLIST person tutor_2 IDREF #IMPLIED> <lab_group> <person person_no="a100">Mhamed MATAOUI</person> <person person_no="a101">Faouzi Sebbak</person> <person person_no="a102" tutor_1="a100" tutor_2="a101">Mohamed Abdelhadi</person> </lab_group> Example: <!ATTLIST person person_no ID #REQUIRED> <!ATTLIST person tutors IDREFs #IMPLIED> <lab_group> <person person_no="a100">Mhamed MATAOUI</person> <person person_no="a101">Faouzi Sebbak</person> <person person_no="a102" tutors="a100 a102">Mohamed Abdelhadi</person> </lab_group>
27
Attributes definition in a DTD
<?xml version="1.0"?> <!DOCTYPE student [ <!ELEMENT student (#PCDATA)> <!ATTLIST student student_no NMTOKEN #REQUIRED> ]> <student student_no="259">Mohamed Abdelhadi</student> The first character of an NMTOKEN value must be a letter, digit, '.', '-', '_', or ':' An NMTOKEN (name token) is any mixture of Name characters. It cannot contain whitespace. Attributes definition in a DTD The attribute type of NMTOKENS allows the attribute value to be made up of multiple NMTOKENSs, separated by a space. <?xml version="1.0"?> <!DOCTYPE student [ <!ELEMENT student (#PCDATA)> <!ATTLIST student lang NMTOKENS #REQUIRED> ]> <student lang="ar fr en">Mohamed Abdelhadi</student> <!ATTLIST element-name attribute-name attribute-type attribute-value> XML Grammars [ Dr. M’hamed MATAOUI ] NOTATION Example: <?xml version="1.0"?> <!DOCTYPE code [ <!ELEMENT code (#PCDATA)> <!NOTATION vrml PUBLIC "VRML 1.0"> <!ATTLIST code lang NOTATION (vrml) #REQUIRED> ]> <code lang="vrml">Some VRML instructions</code>
28
Attributes definition in a DTD
<!ATTLIST element-name attribute-name attribute-type attribute-value > XML Grammars [ Dr. M’hamed MATAOUI ]
29
Structure of an XML document
similar to the principle of Object-oriented programming (objects and properties) Choosing between elements and attributes: Q: sub elements can contain attributes data, so when choosing elements and when choosing attributes??? R1: Elements can be repeated, attributes Not !!! <persons> <person>Mohamed Abdelhadi </person> <person>Mhamed Mataoui </person> … </persons> À préparer un exemple similaire pour expliquer cet aspect <persons person="Mohamed Abdelhadi" person="Mhamed Mataoui" …> </persons> Data in sub-Elements Data in Attributes XML Grammars [ Dr. M’hamed MATAOUI ]
30
Structure of an XML document
Choosing between elements and attributes: <persons person1="Mohamed Abdelhadi" person2="Mhamed Mataoui" …> </persons> this design would lose all interest in structuring that XML gives!!!! Some rules for choosing: a small sized data; unlikely to evolve towards a more complex structure; not repeated. Attribute can be used, otherwise, elements must be used XML Grammars [ Dr. M’hamed MATAOUI ]
31
DTD General Entities The <!ENTITY> declaration for general entities has the following syntax: You can also declare external general entities. This enables you to use an entity from a remote file. You declare external general entities using the following syntax: Example of FPI (Formal Public Identifier): "-//W3C//DTD HTML 4.0 Transitional//EN" XML Grammars [ Dr. M’hamed MATAOUI ]
32
Example <!ENTITY author "Mohamed"> <books> <book>
<title>XML Information Retrieval</title> <author>&author;</author> </book> XML Grammars [ Dr. M’hamed MATAOUI ]
33
DTD Parameter Entities
This is an entity that is used within the DTD itself. In other words, you can create an entity that can be used within your DTD declarations themselves When creating a parameter entity, you need to insert a percentage sign (%) between ENTITY, and the name of the entity: You can also declare external parameter entities. You do this using the following syntax: XML Grammars [ Dr. M’hamed MATAOUI ]
34
Example <!ENTITY % author "<!ELEMENT author EMPTY>">
XML Grammars [ Dr. M’hamed MATAOUI ]
35
Concluding Remarks about DTD
XML Grammars [ Dr. M’hamed MATAOUI ]
36
Programming: Parsing with JAVA
Test all of these examples (XML file combined with a DTD): Multiple attributes declaration CDATA Enumerated ID example (id must be a name) IDREF (exist, does not exist) , IDREFS REQUIRED, IMPLIED, FIXED Entity : general entity, parameter entity Entities Notation XML Grammars [ Dr. M’hamed MATAOUI ]
37
Validation Method 2: W3C Schema
XML Schema XML Schema is an XML-based alternative to DTD With XML Schema, W3C provides an XML type definition language that goes beyond the capabilities of the “native” DTD concept: XML Schema descriptions are valid XML documents themselves XML Schema provides a rich set of built-in data types Users can extend this type system via user-defined types XML element (and attribute) types may even be derived by inheritance XML Grammars [ Dr. M’hamed MATAOUI ]
38
Validation Method 2: W3C Schema
Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
39
Validation Method 2: W3C Schema
Reference to a DTD : an XML Schema : Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
40
Validation Method 2: W3C Schema
Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
41
Validation Method 2: W3C Schema
indicates that the elements and data types used in the schema come from the " namespace. It also specifies that the elements and data types that come from the " namespace should be prefixed with xs: Validation Method 2: W3C Schema The <schema> element may contain some attributes. A schema declaration often looks something like this: indicates that the elements defined by this schema (note, to, from, heading, body.) come from the " namespace. indicates that the default namespace is " indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified. Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
42
Validation Method 2: W3C Schema
Root Element specifies the default namespace declaration. This declaration tells the schema-validator that all the elements used in this XML document are declared in the " namespace. Validation Method 2: W3C Schema Referencing a Schema in an XML Document XML Schema Instance namespace This attribute has two values, separated by a space. The first value is the namespace to use. The second value is the location of the XML schema to use Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
43
Validation Method 2: W3C Schema
Defining a Simple Element syntax for defining a simple element is: A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes. The text can be of many different types. It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself. You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern <xs:element name="nnn" type="ttt"/> XML Schema has a lot of built-in data types. The most common types are: xs:string xs:decimal xs:integer xs:boolean xs:date xs:time XML Grammars [ Dr. M’hamed MATAOUI ]
44
Validation Method 2: W3C Schema
Simple Element (Example): <lastname>Refsnes</lastname> <age>36</age> <dateborn> </dateborn> XML Schema definition : <xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
45
Validation Method 2: W3C Schema
Simple Element (Default and Fixed Values): A default value is automatically assigned to the element when no other value is specified <xs:element name="color" type="xs:string" default="red"/> A fixed value is also automatically assigned to the element, and you cannot specify another value. <xs:element name="color" type="xs:string" fixed="red"/> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
46
Validation Method 2: W3C Schema
Define an Attribute: <xs:attribute name="aaa" type="ttt"/> where aaa is the name of the attribute and ttt specifies the data type of the attribute (same data types as elements). Example: <xs:attribute name="lang" type="xs:string"/> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
47
Validation Method 2: W3C Schema
Attributes (Default and Fixed Values): A default value is automatically assigned to the element when no other value is specified <xs:attribute name="lang" type="xs:string" default="EN"/> A fixed value is also automatically assigned to the element, and you cannot specify another value. <xs:attribute name="lang" type="xs:string" fixed="EN"/> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
48
Validation Method 2: W3C Schema
Optional and Required Attributes: Attributes are optional by default. To specify that the attribute is required, use the "use" attribute: <xs:attribute name="lang" type="xs:string" use="required"/> With XML Schemas, you can also add your own restrictions to your XML elements and attributes. These restrictions are called facets. Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
49
Validation Method 2: W3C Schema
Restrictions/Facets: Restrictions on Values <xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType> </xs:element> The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120: Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
50
Validation Method 2: W3C Schema
Restrictions/Facets: Restrictions on a Set of Values <xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element> The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW: Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
51
Validation Method 2: W3C Schema
Restrictions/Facets: <xs:element name="car" type="carType"/> <xs:simpleType name="carType"> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> <xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element> Equivalent Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element. Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
52
Validation Method 2: W3C Schema
Restrictions/Facets: Restrictions on a Series of Values The example below defines an element called "letter" with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z: <xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpleType> </xs:element> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
53
Validation Method 2: W3C Schema
Restrictions/Facets: Restrictions on a Series of Values The next example defines an element called "initials" with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z: <xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z][A-Z]"/> </xs:restriction> </xs:simpleType> </xs:element> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
54
Validation Method 2: W3C Schema
Examples of Patterns The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z: <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> The only acceptable value is ONE of the following letters: x, y, OR z: <xs:pattern value="[xyz]"/> XML Grammars [ Dr. M’hamed MATAOUI ]
55
Validation Method 2: W3C Schema
Examples of Patterns The only acceptable value is FIVE digits in a sequence, and each digit must be in a range from 0 to 9: <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> The acceptable value is zero or more occurrences of lowercase letters from a to z <xs:pattern value="([a-z])*"/> XML Grammars [ Dr. M’hamed MATAOUI ]
56
Validation Method 2: W3C Schema
Examples of Patterns The acceptable value is one or more pairs of letters, each pair consisting of a lower case letter followed by an upper case letter. For example, "sToP" will be validated by this pattern, but not "Stop" or "STOP" or "stop": <xs:pattern value="([a-z][A-Z])+"/> The only acceptable value is en OR fr OR ar: <xs:pattern value="en|fr|ar"/> XML Grammars [ Dr. M’hamed MATAOUI ]
57
Validation Method 2: W3C Schema
Additional Example The next example defines an element called "password" with a restriction. There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a to z, or a number from 0 to 9: <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9]{8}"/> </xs:restriction> </xs:simpleType> </xs:element> XML Grammars [ Dr. M’hamed MATAOUI ]
58
Validation Method 2: W3C Schema
Restrictions/Facets: Restrictions on Whitespace Characters To specify how whitespace characters should be handled, we would use the whiteSpace constraint. The whiteSpace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters: <xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType> </xs:element> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
59
Validation Method 2: W3C Schema
Restrictions/Facets: Restrictions on Length To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints. This example defines an element called "password" with a restriction. The value must be exactly eight characters: <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType> </xs:element> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
60
Validation Method 2: W3C Schema
Restrictions/Facets: Restrictions on Length To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints. This example defines an element called "password" with a restriction. The value must be exactly eight characters: <xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> <xs:maxLength value="8"/> </xs:restriction> </xs:simpleType> </xs:element> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
61
Validation Method 2: W3C Schema
Complex Elements: A complex element is an XML element that contains other elements and/or attributes. There are four kinds of complex elements: empty elements elements that contain only other elements elements that contain only text elements that contain both other elements and text Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
62
Validation Method 2: W3C Schema
<person> <firstname>Mohamed</firstname> <lastname>Abdelhadi</lastname> </person> Define a Complex Element: <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
63
Validation Method 2: W3C Schema
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> Define a Complex Element: <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
64
Validation Method 2: W3C Schema
If you use the method described above, several elements can refer to the same complex type, like this: <xs:element name="employee" type="personinfo"/> <xs:element name="student" type="personinfo"/> <xs:element name="member" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
65
Validation Method 2: W3C Schema
<xs:element name="employee" type="fullpersoninfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="fullpersoninfo"> <xs:complexContent> <xs:extension base="personinfo"> <xs:sequence> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> You can also base a complex element on an existing complex element and add some elements, like this: Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
66
Validation Method 2: W3C Schema
Complex Empty Elements: <product prodid="1345" /> To define a type with no content, we must define a type that allows elements in its content, but we do not actually declare any elements, like this: <xs:element name="product"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:integer"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
67
Validation Method 2: W3C Schema
<xs:element name="product"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:integer"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element> Equivalent <xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> </xs:element> Examples from : XML Grammars [ Dr. M’hamed MATAOUI ]
68
Validation Method 2: W3C Schema
Complex Text-Only Elements: <person pers_id="p001">Mohamed Abdelhadi</person> <xs:element name="person"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="pers_id" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> XML Grammars [ Dr. M’hamed MATAOUI ]
69
Validation Method 2: W3C Schema
<xs:element name="person"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="pers_id" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> Equivalent <xs:element name="person" type="pers_type" /> <xs:complexType name="pers_type" > <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="pers_id" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> XML Grammars [ Dr. M’hamed MATAOUI ]
70
Validation Method 2: W3C Schema
Complex Types with Mixed Content: <paragraph> this is a new paragraph with <b>bold text here</b> and <i>italic formatting here</i> end of paragraph. </paragraph> <xs:element name="paragraph"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="b" type="xs:string"/> <xs:element name="i" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> XML Grammars [ Dr. M’hamed MATAOUI ]
71
Validation Method 2: W3C Schema
Complex Types with Mixed Content: <xs:element name="paragraph"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="b" type="xs:string"/> <xs:element name="i" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> Equivalent <xs:element name="paragraph" type="free_text"/> <xs:complexType mixed="true" name="free_text"> <xs:sequence> <xs:element name="b" type="xs:string"/> <xs:element name="i" type="xs:string"/> </xs:sequence> </xs:complexType> XML Grammars [ Dr. M’hamed MATAOUI ]
72
Validation Method 2: W3C Schema
Indicators: There are seven indicators: Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Group name attributeGroup name control HOW elements are to be used in documents XML Grammars [ Dr. M’hamed MATAOUI ]
73
Validation Method 2: W3C Schema
Indicators: <xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType> </xs:element> Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Group name attributeGroup name The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once XML Grammars [ Dr. M’hamed MATAOUI ]
74
Validation Method 2: W3C Schema
Indicators: <xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType> </xs:element> Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Group name attributeGroup name The <choice> indicator specifies that either one child element or another can occur XML Grammars [ Dr. M’hamed MATAOUI ]
75
Validation Method 2: W3C Schema
Indicators: <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Group name attributeGroup name The <sequence> indicator specifies that the child elements must appear in a specific order XML Grammars [ Dr. M’hamed MATAOUI ]
76
Validation Method 2: W3C Schema
Indicators: <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"/> </xs:sequence> </xs:complexType> </xs:element> Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Element Groups Attribute Groups The <maxOccurs> (resp. <minOccurs>) indicator specifies the maximum number (resp. minimum number) of times an element can occur, XML Grammars [ Dr. M’hamed MATAOUI ]
77
Validation Method 2: W3C Schema
<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> Indicators: Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Element Groups Attribute Groups XML Grammars [ Dr. M’hamed MATAOUI ]
78
Validation Method 2: W3C Schema
Indicators: <xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributeGroup> <xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> </xs:complexType> </xs:element> Order indicators: All Choice Sequence Occurrence indicators: maxOccurs minOccurs Group indicators: Element Groups Attribute Groups XML Grammars [ Dr. M’hamed MATAOUI ]
79
Validation Method 2: W3C Schema
The <any> Element: <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:any minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> The <any> element enables us to extend the XML document with elements not specified by the schema XML Grammars [ Dr. M’hamed MATAOUI ]
80
Validation Method 2: W3C Schema
The <anyAttribute> Element: <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> <xs:anyAttribute/> </xs:complexType> </xs:element> The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema XML Grammars [ Dr. M’hamed MATAOUI ]
81
XML Grammars Any questions? End of this Chapter : XML Grammars
Chapter III – XML Grammars [ Dr. M’hamed MATAOUI ]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.