XML Namespaces Theidea behing XML namespaces is to avoid element name conflicts . Example of name conflict (w3schools.com) <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> <table> <name>African Coffee Table</name> <width>80</width> <length>120</length> </table> Same tag-name, different content and meaning!
4.
Solving Name Conflict<h:table> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <f:table> <f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length> </f:table> Prefix h has xhtml-related elements and prefix f has furniture-related elements
5.
xmlns - attributesWhen using prefixes in XML, a so-called namespace for the prefix must be defined. The namespace is defined by the xmlns attribute in the start tag of an element.
Namespace name Thename of namespace should be unique: <h:table xmlns:h= "http://www.w3.org/TR/html4/ "> It is just a string, but it should be declared as URI. Using URI reduces the possibility of different namespaces using duplicate identifiers .
9.
Example: AnXHTML + MathML + SVG Profile An XHTML+MathML+SVG profile is a profile that combines XHTML 1.1, MathML 2.0 and SVG 1.1 together. This profile enables mixing XHTML, MathML and SVG in the same document using XML namespaces mechanism.
XML Schema (W3C)Language for defining set of rules for XML – documents. W3C Recommendation (2001) More specific than DTD Datatypes! Is XML-language and it uses xml namespaces
13.
Schema vs. DTD(W3Schools.com) XML Schemas are extensible to future additions XML Schemas are richer and more powerful than DTDs XML Schemas are written in XML XML Schemas support data types XML Schemas support namespaces
14.
DTD Linking Definesthe structure, tag names and order for all xhtml - documents W3C has created XML-language "XHTML" by defining it's rules in DTD.
15.
DTD Linking Definesthe structure, tag names and order for all "book"- documents TAMK has created XML-language "Book" by defining it's rules in DTD.
16.
Schema Linking Definesthe structure, tag names and order for all "book"- documents TAMK has created XML-language "Book" by defining it's rules in DTD.
17.
Linking? The basicidea with linking to Schema: <?xml version="1.0"?> <root schemaLocation="note.xsd" > <foo>...</foo> </root> The problem with this is that now it is set that attribute "schemaLocation" is part of your XML-language
18.
Linking and NamespaceUsage Linking with namespace <?xml version="1.0"?> <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="note.xsd" > <foo>...</foo> </root> Now the "schemaLocation" – attribute is in it's own namespaces (xsi) and does not belong to the "main" language.
Let's remove namespaces...<?xml version="1.0"?> <schema> <element name="koululainen" type="koululaiset_tyyppi"/> <complexType name="koululaiset_tyyppi"> <sequence> <element name="etunimi" type="string"/> <element name="sukunimi" type="string"/> </sequence> </complexType> </schema> It doesn't look so confusing after all?
21.
The Basics: ElementYou define the name for the elements by using element -element. <element name="foo" type="bar" /> Type? 44 Built-in schema datatypes string, double, time, date, etc. See all the datatypes
minOccurs and maxOccursThe amount of elements In DTD: *, ?, + In Schema: minOccurs, maxOccurs Example <xsd:element name="date" type="xsd:date" minOccurs="1" maxOccurs="2" /> Default and special values default minOccurs : 1 default maxOccurs : same as minOccurs maxOccurs="unbounded" : unlimited
24.
Defining new DatatypesIf the the built-in datatypes are not enough, you can build your own datatypes. This does not necessarily work: <xsd:element name="grade" type="xsd:integer" /> There are two ways of specifying your own datatype Named Data Type Anonymous Data Type
Structure of theXML-file It's possible to define the structure of the XML-file using complexType If element A has child-elements, then element A's type is complexType
32.
SimpleType vs. ComplexTypeSimpleType <grade> 7 </grade> Since grade does not hold other child – elements, grade's type is simpleType ComplexType <students> <student>Jack</student> </students> Since student does hold child – element(s), student's type is complexType
33.
Example: XML -File <?xml version="1.0"?> <students> <firstname>Pekka</firstname> <lastname>Virtanen</lastname> </students>
Order of theelements Sequence : Elements appear in same order than in Schema All : Elements can appear in any order Choice : One element can appear from the choice-list <xsd:element name="person"> <xsd:complexType> <xsd: choice > <xsd:element name="employee" type="employee"/> <xsd:element name="member" type="member"/> </xsd: choice > </xsd:complexType> </xsd:element>
Empty Element withAttribute XML <student id="A1" /> Schema <xsd:element name="student" type="student_type" /> <xsd:complexType name="student_type"> <xsd:attribute name="id" type="xsd:ID"/> </xsd:complexType>
44.
PHP5 and SchemaWith PHP5 you do not have to link xml to schema – files. The linking is done in PHP5 – code, not in XML. Example of schema-validation: $doc = new domDocument; if ( $doc->load("books.xml") and $doc->schemaValidate("books.xsd' ) ) { print "Is WellFormed and Schema-valid!"; }