Web servisai III (XML Schemos ir DTD)

Slides:



Advertisements
Similar presentations
What is XML? a meta language that allows you to create and format your own document markups a method for putting structured data into a text file; these.
Advertisements

17 Apr 2002 XML Syntax: DTDs Andy Clark. Validation of XML Documents XML documents must be well-formed XML documents may be valid – Validation verifies.
1 Web Data Management XML Schema. 2 In this lecture XML Schemas Elements v. Types Regular expressions Expressive power Resources W3C Draft:
An Introduction to XML Based on the W3C XML Recommendations.
XML 6.3 DTD 6. XML and DTDs A DTD (Document Type Definition) describes the structure of one or more XML documents. Specifically, a DTD describes:  Elements.
XML Document Type Definitions ( DTD ). 1.Introduction to DTD An XML document may have an optional DTD, which defines the document’s grammar. Since the.
1 XML DTD & XML Schema Monica Farrow G30
Document Type Definitions
XML Schemas Lecture 10, 07/10/02. Acknowledgements A great portion of this presentation has been borrowed from Roger Costello’s excellent presentation.
A Technical Introduction to XML Transparency No. 1 XML quick References.
XML Schemas and Namespaces Lecture 11, 07/10/02. BookStore.dtd.
1 Print your own copy If you bring it along, hand in with your exam script Do not write anything extra or you will be penalized Student Name: Student Number:
XML Verification Well-formed XML document  conforms to basic XML syntax  contains only built-in character entities Validated XML document  conforms.
Document Type Definitions. XML and DTDs A DTD (Document Type Definition) describes the structure of one or more XML documents. Specifically, a DTD describes:
Processing of structured documents Spring 2003, Part 3 Helena Ahonen-Myka.
17 Apr 2002 XML Schema Andy Clark. What is it? A grammar definition language – Like DTDs but better Uses XML syntax – Defined by W3C Primary features.
XML Anisha K J Jerrin Thomas. Outline  Introduction  Structure of an XML Page  Well-formed & Valid XML Documents  DTD – Elements, Attributes, Entities.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 Created by Cheryl M. Hughes, Harvard University Extension School — Cambridge, MA The Web Wizard’s Guide.
Introduction to XML cs3505. References –I got most of this presentation from this site –O’reilly tutorials.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation XML Schema 1 Lecturer.
Chapter 4: Document Type Definitions. Chapter 4 Objectives Learn to create DTDs Validate an XML document against a DTD Use DTDs to create XML documents.
1 XML Schemas. 2 Useful Links Schema tutorial links:
Dr. Azeddine Chikh IS446: Internet Software Development.
McGraw-Hill/Irwin © 2004 by The McGraw-Hill Companies, Inc. All rights reserved. Schemas Ellen Pearlman Eileen Mullin Programming the Web Using XML.
XML CPSC 315 – Programming Studio Fall 2008 Project 3, Lecture 1.
XML Language Family Detailed Examples Most information contained in these slide comes from: These slides are intended.
XML 1 Enterprise Applications CE00465-M XML. 2 Enterprise Applications CE00465-M XML Overview Extensible Mark-up Language (XML) is a meta-language that.
XML Extensible Markup Language. What is XML? ● meta-markup language ● a language for defining a family of languages ● semantic/structured mark-up language.
XML Syntax - Writing XML and Designing DTD's
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation XML Schema 2 Lecturer.
1 Chapter 10: XML What is XML What is XML Basic Components of XML Basic Components of XML XPath XPath XQuery XQuery.
Of 33 lecture 3: xml and xml schema. of 33 XML, RDF, RDF Schema overview XML – simple introduction and XML Schema RDF – basics, language RDF Schema –
E0262 – MIS – Multimedia Storage Techniques XML (Extensible Markup Language  XML is a markup language for creating documents containing structured information.
An OO schema language for XML SOX W3C Note 30 July 1999.
Lecture 16 Introduction to XML Boriana Koleva Room: C54
XML – Part III. The Element … This type of element either has the element content or the mixed content (child element and data) The attributes of the.
An Introduction to XML Sandeep Bhattaram
1/11 ITApplications XML Module Session 3: Document Type Definition (DTD) Part 1.
CSE3201 Information Retrieval Systems DTD Document Type Definition.
Sheet 1XML Technology in E-Commerce 2001Lecture 2 XML Technology in E-Commerce Lecture 2 Logical and Physical Structure, Validity, DTD, XML Schema.
C# and Windows Programming XML Processing. 2 Contents Markup XML DTDs XML Parsers DOM.
Processing of structured documents Spring 2003, Part 3 Helena Ahonen-Myka.
SNU OOPSLA Lab. Logical structure © copyright 2001 SNU OOPSLA Lab.
XML Schema (W3C) Thanks to Jussi Pohjolainen TAMK University of Applied Sciences.
XML Schema Definition (XSD). Definition of a Schema It is a model for describing the structure and content of data The XML Schema was developed as a content.
Introduction to XML Jussi Pohjolainen TAMK University of Applied Sciences.
C Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Introduction to XML Standards.
Digital Multimedia, 2nd edition Nigel Chapman & Jenny Chapman Chapter 14 This presentation © 2004, MacAvon Media Productions XML.
XML Notes taken from w3schools. What is XML? XML stands for EXtensible Markup Language. XML was designed to store and transport data. XML was designed.
CITA 330 Section 4 XML Schema. XML Schema (XSD) An alternative industry standard for defining XML dialects More expressive than DTD Using XML syntax Promoting.
4 Copyright © 2004, Oracle. All rights reserved. Validating XML by Using XML Schema.
CITA 330 Section 2 DTD. Defining XML Dialects “Well-formedness” is the minimal requirement for an XML document; all XML parsers can check it Any useful.
Extensible Markup Language (XML) Pat Morin COMP 2405.
eXtensible Markup Language
Unit 4 Representing Web Data: XML
CIS 228 The Internet 9/20/11 XHTML 1.0.
G52IWS: Extensible Markup Language (XML)
Java XML IS
Session III Chapter 6 – Creating DTDs
Chapter 7 Representing Web Data: XML
Introduction to XML: Part I
Web Programming Maymester 2004
ece 720 intelligent web: ontology and beyond
Chapter 13 XML Yingcai Xiao.
Extensible Markup Language
Working with Namespaces and Schemas
XML Schema Primer Seong Jong Choi Multimedia Lab.
CH 20 XML Schema.
Document Type Definition (DTD)
XML IST 421.
Presentation transcript:

Web servisai III (XML Schemos ir DTD) Tautvydas Dagys 2010

Turinys DTD XML Schemos XML in .NET

Prisiminimui ...

Well formed vs Valid

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

 ? 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

Ką aprašo Schemos ir DTD?

DOCUMENT TYPE DEFINITION Jussi Pohjolainen TAMK University of Applied Sciences

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

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

General Syntax: External Subset <?xml version="1.0"?> <!DOCTYPE root-element [SYSTEM OR PUBLIC FPI] "uri"> <root> <foo>...</foo> </root>

Root Element <?xml version="1.0"?> <!DOCTYPE root [SYSTEM OR PUBLIC FPI] "uri"> <root> <foo>...</foo> </root>

Examples <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- strict.dtd"> <!DOCTYPE books PUBLIC "-//TAMK//DTD MY BOOKS//EN" "books.dtd"> <!DOCTYPE books SYSTEM

Internal DTD <?xml version="1.0"?> <!DOCTYPE root [ <!ELEMENT root (foo)> <!ELEMENT foo (#PCDATA)> ]> <root> <foo>...</foo> </root>

External DTD: foo.xml (system) <?xml version="1.0"?> <!DOCTYPE root SYSTEM "foo.dtd"> <root> <foo>...</foo> </root>

External DTD: foo.dtd <!ELEMENT root (foo)> <!ELEMENT foo (#PCDATA)>

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

Example of ELEMENT - usage XML: <student> <name>Tiina</name> </student> DTD: <!ELEMENT student (name)> <!ELEMENT name (#PCDATA)>

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)>

The Amount of Elements <!ELEMENT koululaiset (koululainen+)> <!ELEMENT koululainen (etunimi+, sukunimi, henktunnus?)> <!ELEMENT etunimi (#PCDATA)> <!ELEMENT sukunimi (#PCDATA)> <!ELEMENT henktunnus (#PCDATA)>

Alternative Elements Alternative elements: The use of brackets: <!ELEMENT materiaali (kirja | video)> The use of brackets: <!ELEMENT henkilo ((etunimi+,(sukunimi, tyttönimi?))|tunnus)>

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>

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>

Attribute Requirements #REQUIRED, mandatory #FIXED, value is fixed #IMPLIED, optional Usage: <!ATTLIST koululainen tunnari ID #REQUIRED>

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>

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)>

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)>

W3C SCHEMA Jussi Pohjolainen TAMK University of Applied Sciences

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

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

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

Linking and Namespace Usage 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.

Simple Schema <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>

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?

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

Usage of Datatypes <xsd:element name="firstname" type="xsd:string" /> <xsd:element name="ableToSwim" type="xsd:boolean" /> <xsd:element name="date" type="xsd:date" />

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

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

1) Named Data Type <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>

2) Anonymous Data Type <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>

Benefits of Named Data Type If you want re-use your datatype: <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>

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>

SimpleType: pattern Using REGEX: <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:pattern value="[a-z]"/> </xsd:restriction> </xsd:simpleType>

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}"/>

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

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

Example: XML - File <?xml version="1.0"?> <students> <firstname>Pekka</firstname> <lastname>Virtanen</lastname> </students>

Example: XSD – file Named ComplexType <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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)

Example: XSD – file Anonymous ComplexType <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>

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>

Deep Structure in XML - File <?xml version="1.0"?> <students> <student> <name> <firstname>Pekka</firstname> </nam> </student> </students>

Using Anonymous Data Type: The Horror! <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>

"There is an error in my schema, could you find it for me?" <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>

Use Named Datatypes! It's easier to find errors.. <?xml version="1.0"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <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>

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>

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>

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>

XML in .NET

System.Xml namespace System.Xml namespace supports: XML 1.0 - 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

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

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>

DOM medžio pavyzdys Elipsė = XmlNode

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: http://msdn.microsoft.com/en- us/library/system.xml.xmlnode_members.aspx

XmlDocument Atlieka visas XmlNode funkcijas + Kuria naujus Elementus: CreateNode, CreateAttribute, CreateElement, CreateTextNode Atlieka paiešką: GetElementById, GetElementByName Informacija apie XmlDocument klasę http://msdn.microsoft.com/en- us/library/system.xml.xmldocument_members.aspx

Pabaigai...

Resursai XML ir DTD XML Schemos DOM: XML Developer center: http://www.slideshare.net/pohjus/xml-and-dtd-presentation XML Schemos http://www.slideshare.net/pohjus/xml-schema-w3c- presentation DOM: http://msdn.microsoft.com/en-us/library/hf9hbf87.aspx XML Developer center: http://msdn.microsoft.com/en-us/xml/default.aspx