Download presentation
Presentation is loading. Please wait.
1
Web servisai III (XML Schemos ir DTD)
Tautvydas Dagys 2010
2
Turinys DTD XML Schemos XML in .NET
3
Prisiminimui ...
4
Well formed vs Valid
5
Rules for Well-formed XML document
it must begin with the XML declaration it must have one unique root element start-tags must have matching end-tags elements are case sensitive all elements must be closed all elements must be properly nested all attribute values must be quoted entities must be used for special characters
6
? XML schemų rūšys DTD XSD XML Schema Definition RELAX NG, DSD
Nevaliduotas XML dokumentas Validuotas XML dokumentas (gali būti pasikeitęs) DTD Document Type Definition XSD XML Schema Definition RELAX NG, DSD ir kitos schemos
7
Ką aprašo Schemos ir DTD?
8
DOCUMENT TYPE DEFINITION
Jussi Pohjolainen TAMK University of Applied Sciences
9
In General DTD is used both in XML and SGML
Specifying the structure and tag-names in XML-language: Tag names, order, amount Attribute names and datatypes The order of the elements
10
Associating DTDs with Documents
A DTD is associated with an XML document via a Document Type Declaration Internal vs. external subset Internal: DTD is part of the declaration and is embedded into the xml-document External: DTD is in external file and declaration links to the file
11
General Syntax: External Subset
<?xml version="1.0"?> <!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri"> <root> <foo>...</foo> </root>
12
Root Element <?xml version="1.0"?> <!DOCTYPE root [SYSTEM OR PUBLIC FPI] "uri"> <root> <foo>...</foo> </root>
13
Examples <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " strict.dtd"> <!DOCTYPE books PUBLIC "-//TAMK//DTD MY BOOKS//EN" "books.dtd"> <!DOCTYPE books SYSTEM
14
Internal DTD <?xml version="1.0"?> <!DOCTYPE root [
<!ELEMENT root (foo)> <!ELEMENT foo (#PCDATA)> ]> <root> <foo>...</foo> </root>
15
External DTD: foo.xml (system)
<?xml version="1.0"?> <!DOCTYPE root SYSTEM "foo.dtd"> <root> <foo>...</foo> </root>
16
External DTD: foo.dtd <!ELEMENT root (foo)>
<!ELEMENT foo (#PCDATA)>
17
DTD's ELEMENT Declaration
ELEMENT defines tag-name: <!ELEMENT tagname (values)> Values defines child-elements <!ELEMENT tagname (child1, child2)> Or the type of the element <!ELEMENT tagname (type)> Types? #PCDATA, ANY or EMPTY Amount + = 1 - n * = 0 - n ? = 0 - 1
18
Example of ELEMENT - usage
XML: <student> <name>Tiina</name> </student> DTD: <!ELEMENT student (name)> <!ELEMENT name (#PCDATA)>
19
Example of ELEMENT - usage
XML <koululaiset> <koululainen> <etunimi>Tiina</etunimi> <sukunimi>Virtanen</sukunimi> </koululainen> </koululaiset> DTD <!ELEMENT koululaiset (koululainen)> <!ELEMENT koululainen (etunimi, sukunimi)> <!ELEMENT etunimi (#PCDATA)> <!ELEMENT sukunimi (#PCDATA)>
20
The Amount of Elements <!ELEMENT koululaiset (koululainen+)>
<!ELEMENT koululainen (etunimi+, sukunimi, henktunnus?)> <!ELEMENT etunimi (#PCDATA)> <!ELEMENT sukunimi (#PCDATA)> <!ELEMENT henktunnus (#PCDATA)>
21
Alternative Elements Alternative elements: The use of brackets:
<!ELEMENT materiaali (kirja | video)> The use of brackets: <!ELEMENT henkilo ((etunimi+,(sukunimi, tyttönimi?))|tunnus)>
22
Attribute Declaration
Attribute is defined with ATTLIST. Attribute in XML: <koululainen tunnari="aaa-222"> SYNTAX: <!ATTLIST ELEMENTNAME ATTRIBUTENAME TYPE REQUIRED> ELEMENTNAME: The element which the attribute is given ATTRIBUTENAME: attribute name TYPE: attribute contents REQUIRED: Is the attribute mandatory or not Example: <!ATTLIST koululainen tunnari ID #REQUIRED>
23
Attribute Types Attribute Datatypes CDATA: character data
ENTITY or ENTITIES: entity defined somewhere else ID: Unique value. Must begin with letter, underscore or colon. IDREF or IDREFS: Reference to ID NMTOKEN or NMTOKENS: CDATA without spaces NOTATION: Link to external resource Example <!ATTLIST koululainen tunnari ID #REQUIRED>
24
Attribute Requirements
#REQUIRED, mandatory #FIXED, value is fixed #IMPLIED, optional Usage: <!ATTLIST koululainen tunnari ID #REQUIRED>
25
Example <!ELEMENT kalenteri (tapaaminen*)>
<!ELEMENT tapaaminen (aika, paikka)> <!ELEMENT aika (pvm , klo)> <!ELEMENT paikka EMPTY> <!ELEMENT klo (#PCDATA)> <!ELEMENT pvm (#PCDATA)> <!ATTLIST tapaaminen id ID #REQUIRED> <!ATTLIST paikka yritys CDATA #REQUIRED kaupunki (Helsinki|Tampere) "Helsinki" tila NMTOKEN #IMPLIED>
26
Is the XML Valid? XML DTD <students> <student>
<name>Tiina Virtanen</name> <gender>Female</gender> </student> </students> DTD <!ELEMENT students (student)> <!ELEMENT student (name, gender)> <!ELEMENT name (#PCDATA)> <!ELEMENT gender (#PCDATA)>
27
What About Now? XML DTD <students> <student>
<name>Tiina Virtanen</name> <gender>Cow</gender> </student> </students> DTD <!ELEMENT students (student)> <!ELEMENT student (name, gender)> <!ELEMENT name (#PCDATA)> <!ELEMENT gender (#PCDATA)>
28
W3C SCHEMA Jussi Pohjolainen TAMK University of Applied Sciences
29
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
30
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
31
Linking? The basic idea 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
32
Linking and Namespace Usage
Linking with namespace <?xml version="1.0"?> <root xmlns:xsi=" 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.
33
Simple Schema <?xml version="1.0"?>
<xsd:schema xmlns:xsd=" <xsd:element name="koululainen" type="koululaiset_tyyppi"/> <xsd:complexType name="koululaiset_tyyppi"> <xsd:sequence> <xsd:element name="etunimi" type="xsd:string"/> <xsd:element name="sukunimi" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
34
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?
35
The Basics: Element You 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
37
Usage of Datatypes <xsd:element name="firstname"
type="xsd:string" /> <xsd:element name="ableToSwim" type="xsd:boolean" /> <xsd:element name="date" type="xsd:date" />
38
minOccurs and maxOccurs
The 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
39
Defining new Datatypes
If 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
40
1) Named Data Type <?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="grade" type="grade_type" /> <xsd:simpleType name="grade_type"> <xsd:restriction base="xsd:positiveInteger"> <xsd:minInclusive value="4"/> <xsd:maxInclusive value="10"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
41
2) Anonymous Data Type <?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="grade"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:minInclusive value="4"/> <xsd:maxInclusive value="10"/> </xsd:restriction> </xsd:simpleType> </xsd:element> </xsd:schema>
42
Benefits of Named Data Type
If you want re-use your datatype: <?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="grade" type="grade_type" /> <xsd:element name="teachers_IQ" type="grade_type" /> <xsd:simpleType name="grade_type"> <xsd:restriction base="xsd:positiveInteger"> <xsd:minInclusive value="4"/> <xsd:maxInclusive value="10"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
43
SimpleType: enumeration
Alternative content <xsd:simpleType name="car"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="Audi"/> <xsd:enumeration value="Golf"/> <xsd:enumeration value="BMW"/> </xsd:restriction> </xsd:simpleType>
44
SimpleType: pattern Using REGEX: <xsd:simpleType>
<xsd:restriction base="xsd:string"> <xsd:pattern value="[a-z]"/> </xsd:restriction> </xsd:simpleType>
45
REGEX Examples <xs:pattern value="[A-Z][A-Z][A-Z]"/>
<xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> <xs:pattern value="[xyz]"/> <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> <xs:pattern value="([a-z])*"/> <xs:pattern value="male|female"/> <xs:pattern value="[a-zA-Z0-9]{8}"/>
46
Structure of the XML-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
47
SimpleType vs. ComplexType
<grade>7</grade> Since grade does not hold other child – elements, grade's type is simpleType ComplexType <students><student>Jack</student></stud ents> Since student does hold child – element(s), student's type is complexType
48
Example: XML - File <?xml version="1.0"?> <students>
<firstname>Pekka</firstname> <lastname>Virtanen</lastname> </students>
49
Example: XSD – file Named ComplexType
<?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="students" type="students_type"> <xsd:complexType name="students_type"> <xsd:sequence> <xsd:element name="firstname" type="xsd:string"/> <xsd:element name="lastname" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema> Use now complexType (vs. simpleType)
50
Example: XSD – file Anonymous ComplexType
<?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="students"> <xsd:complexType> <xsd:sequence> <xsd:element name="firstname" type="xsd:string"/> <xsd:element name="lastname" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
51
Example: ComplexType <xsd:element name="employee" type="personinfo"/> <xsd:element name="student" type="personinfo"/> <xsd:element name="member" type="personinfo"/> <xsd:complexType name="personinfo"> <xsd:sequence> <xsd:element name="firstname" type="xsd:string"/> <xsd:element name="lastname" type="xsd:string"/> </xsd:sequence> </xsd:complexType>
52
Deep Structure in XML - File
<?xml version="1.0"?> <students> <student> <name> <firstname>Pekka</firstname> </nam> </student> </students>
53
Using Anonymous Data Type: The Horror!
<?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="students"> <xsd:complexType> <xsd:sequence> <xsd:element name="student"> <xsd:element name="name"> <xsd:element name="firstname" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
54
"There is an error in my schema, could you find it for me?"
<?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="students"> <xsd:complexType> <xsd:sequence> <xsd:element name="student"> <xsd:element name="name"> <xsd:element name="firstname" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema>
55
Use Named Datatypes! It's easier to find errors..
<?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="students" type="students_type" /> <xsd:complexType name="students_type"> <xsd:sequence> <xsd:element name="student" name="student_type" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="student_type"> <xsd:element name="name" name="name_type" /> <xsd:complexType name="name_type"> <xsd:element name="firstname" name="xsd:string" /> </xsd:schema>
56
Order of the elements 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>
57
Attribute XML Schema <student id="A1">...</student>
<xsd:element name="student" type="student_type" /> <xsd:complexType name="student_type"> <xsd:sequence> ... </xsd:sequence> <xsd:attribute name="id" type="xsd:ID"/> </xsd:complexType>
58
Empty Element with Attribute
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>
59
XML in .NET
60
System.Xml namespace System.Xml namespace supports:
XML including DTD support XML Namespaces - both stream level and DOM. XSD Schemas XPath expressions XSLT transformations DOM Level 1 Core DOM Level 2 Core XmlReader XmlWriter
61
XML DOM Standartizuoja W3C
Hierarchinis XML dokumentas laikomas atmintyje Leidžia skaityti, rašyti, manipuliuoti XML duomenis XmlDocument paveldi iš XmlNode XML DOM – pirminė užduotis redagavimas Jeigu reikia tik skaityti – naudoti XmlReader
62
XML DOM pavyzdys <?xml version="1.0"?> <books> <book> <author>Carson</author> <price format="dollar">31.95</price> <pubdate>05/01/2001</pubdate> </book> <pubinfo> <publisher>MSPress</publisher> <state>WA</state> </pubinfo> </books>
63
DOM medžio pavyzdys Elipsė = XmlNode
64
Pagrindinės operacijos
Pagrindiniai veiksmai per XmlNode klasę Pirmiausia reikia susirasti Root element’ą Darbas su vaikais: ChildNodes, FirstChild, LastChild,... Darbas su tėvais ir kaimynais: ParentNode, NextSibling, PreviousSibling,... Modifikavimas: AppendChild, InsertAfter, RemoveAll, RemoveChild,.. XmlNode aprašas: us/library/system.xml.xmlnode_members.aspx
65
XmlDocument Atlieka visas XmlNode funkcijas +
Kuria naujus Elementus: CreateNode, CreateAttribute, CreateElement, CreateTextNode Atlieka paiešką: GetElementById, GetElementByName Informacija apie XmlDocument klasę us/library/system.xml.xmldocument_members.aspx
66
Pabaigai...
67
Resursai XML ir DTD XML Schemos DOM: XML Developer center:
XML Schemos presentation DOM: XML Developer center:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.