Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic XML, DTD & Schema.

Similar presentations


Presentation on theme: "Basic XML, DTD & Schema."— Presentation transcript:

1 Basic XML, DTD & Schema

2 XML의 필요성 오늘날 문서 데이터의 요구사항 XML은 위의 요구사항을 지원한다!! 사람과 시스템이 모두 읽을 수 있는 형태
데이터의 내용과 구조를 정의 관계설정 자료의 구조와 보여주는 부분의 분리 개방성 및 확장성 XML은 위의 요구사항을 지원한다!!

3 XML태그를 이용한 데이터의 표현 문서는 세가지 요소로 구성됨 XML 문서는 문서의 내용에 대한 구조적인 정보를 가지고 있음
자료 구조 출력

4 XML의 역사와 W3C XML은 SGML의 Subset형태이다.
Standard Generalized Markup Language 1986년에 ISO 표준으로 정의 XML은 World Wide Web Consortium (W3C)에 의해 표준화

5 XML(eXtensible Markup Language)
웹 상에서 구조화된 문서를 전송하도록 설계된 마크업 언어 1996년 W3C(World Wide Web Consortium)에서 제안 SGML에 기반을 둔 단순하고, 매우 유연성 있는 text형식 논리/내용 구조와 표현을 분리 사용자 임의의 구조정의-자유로운 element와 attribute정의 HTML(HyperText Markup Language)의 단점을 해결하고 SGML(Standard Generalized Markup Language)의 복잡함을 해결하기 위한 방안으로 제시 1998년 2월 W3C에 의해 XML1.0에 대한 표준규약 제정

6 XML의 취지 최근에 들어와서는 정보의 양이 급격히 증가하고 인터넷이 대중화되면서 다음의 두 가지 성과를 통합할 필요성이 점차 강조 되고 있음 정보를 표준화된 형태로 저장해 그 활용도를 증대 시키는 것 이를 웹 환경을 통해 제공함으로써 대중성을 확보하는 것

7 XML 표준 제정 동기 90년 이후 기업간 합병에 따른 저비용 시스템의 통합의 필요
인터넷 페이지 증가에 따른 HTML의 구조 검색의 한계 SGML의 개발 및 사용의 한계 OS, H/W 시스템, 특정 Band의 문서 포멧에 대한 국제적인 독립적인 문서 포멧에 대한 요구 소프트웨어 개발 용이, 문서작성 용이, 저장 용의, 관리 용이, 전송 용이, 인터넷 서비스에 용이한 문서 필요 다양한 형식으로 변환이 가능한 문서 필요

8 “XML is a breakthrough technology” - Bill Gates -
Logical View Layout 내용(Content) 형식(Style) + Data로서 의미를 지님 다양한 사용자 뷰(View)제공 “XML is a breakthrough technology” Bill Gates -

9 XML의 특징 eXtensible Markup Language DTD / XML Schema XSL 구조화된 문서
태그언어 구조화된 문서 문서의 내용과 형식의 분리 DTD / XML Schema XSL

10 XML in Community-Specific

11 XML Everywhere

12 XML문서 생성 Element & Attribute XML 문법 XML 문서의 구성 XML 문서의 구조
Well-Formed XML 문서 생성 Element와 Attribute의 사용

13 Element & Attribute ▣ Element(요소)
하나의 Element는 Start-Tags/Contents/End-Tags로 구성. <greeting> Start-Tags Hello, XML! Contents(데이터) </greeting> End-Tags ※ EMPTY Element (빈요소) <tag/>로 표시. html에 경우 <br><hr>등 ▣ Attribute(속성) <tag attribute=”value”>XML 테스트</tag> 속성이름 = “value”로 html과 달리 “ ” 나 ‘ ’로 하여야 한다.

14 XML 문법 XML 은 데이터를 설명하기 의해 태그를 사용 정보를 나타내는 기본 요소의 표현 방식 하위요소 포함 가능
<name> Lars Peterson </name> Start tag Element content End tag <employee> <name>Lars Peterson</name> <salary>25000</salary> </employee>

15 XML 문서의 구성 Processing instruction Comment Root element Child element
<?xml version="1.0"?> Comment <!-- This is a comment --> Root element <employee> </employee> Child element <name>Lars Peterson</name> <permanent_staff /> Empty element Attribute <salary currency="US$"> 25000 </salary> <salary> 25000 </salary>

16 XML 문서의 구조 Prolog 상위 element 는 다른 element를 포함 XML 선언
DTD 또는 XML schema (문법의 정의) 상위 element 는 다른 element를 포함 <?xml version="1.0"?> <!DOCTYPE employees SYSTEM "employees.dtd"> <employee> <name>Lars Peterson</name> ... </employee>

17 Well-Formed XML문서 생성 Well-formed 문서의 요구사항 Valid문서의 요구사항
단일, 유일한 상위 element 시작/끝 tags의 일치 대소문자 구분 Element들간의 올바른 nesting Attribute는 인용부호를 사용 하나의 element에서 속성을 반복하여 사용하지 않음 Valid문서의 요구사항

18 Document Object Model (DOM)
XML 용어 Validation Style sheets XML XML Document Object Model (DOM) Data binding

19 XML 처리 시스템 구성도

20 XML 문서의 유효성 Validation의 필요성 Well-Formed 과 Valid Documents
Validating 기술: DTD와XML Schemas

21 Validation의 필요성 Validation은 XML 문서를 원하는 형식에 맞게 작성/교환 할 수 있도록 한다.
올바른 element와 attributes element와 attribute의 관계정의 하부 Element의 숫자와 순서 정의 데이터 형식(XML schema에서 언급)

22 Well-Formed 와 Valid 문서 Well-Formed XML 문서 Valid XML 문서 XML의 기본문법 준수
elements/attributes의 조합으로 구성 “문법”을 가지고 있음 Valid XML 문서 well formed 문서 Document Type Definition (DTD) 또는 XML schema로 정의 DTD/XML schema로 정의 된 문법사용

23 Validating 기술:DTD와XML Schema
다른 구현 방식 DTD XML schema 표준으로 정착 Parser가 지원 제한점 표준으로 정착 기능이 풍부

24 DTD (Document Type Definition)

25 DTD 예

26 마크업 선언(markup declaration)
<! keyword name content > 1 2 3 4 1.모든 DTD의 시작은 markup declaration open delimiter(MDO)인 '<!'로 한다. 2. 마크업 선언 유형에 따라서 ELEMENT, ATTLIST, ENTITY, NOTATION 와 같은 키워드를 선언한다. 대문자(uppercase)로 꼭 표기해야 한다. 3. 선언하고자 하는 이름과 내용을 기입한다. 이름의 정의하는 방법은 XML문서에서의 요소(element), 속성(attribute) 생성 규칙과 동일하다. 4. 모든 DTD의 끝은 markup declaration close delimiter(MDC)인 '>'로 닫아준다.

27 요소(Element) 선언 <!ELEMENT 요소이름 요소유형 > 1 2 3 4
2. 요소이름은 XML 요소이름 명명과 같다. 3. 요소 유형을 지정한다. 4. > 로 요소 선언을 종료한다. 요소유형 : Children | EMPTY | MIXED | ANY Children : 자식 요소(child element)를 포함한다. EMPTY : 어떤 자식 요소(child element)도 포함할 수 없다. MIXED : 일반 텍스트형태(#PCDATA)와 자식 요소(child element)를 포함. ANY : 어떤 유형의 요소(element)도 포함할 수 있다. 보통 ROOT에서 사용.

28 요소(Element) 생성 규칙 Connector | => 선택적인 자식요소 선언
 | => 선택적인 자식요소 선언 <!ELEMENT choice (one|two)> <choice><one>Test</one></choice> (O) <choice><two>Test</two></choice> (O) , => 순차적인 자식요소 선언 <!ELEMENT seq (one, two)> <seq><one>Test-one</one><two>Test-two</two></seq> (O) <seq><two>Test-two</two><one>Test-one</one></seq> (X)

29 Indicator  ? => 한번 나타나거나 한번도 나타나지 않는 요소 선언 <!ELEMENT choice (option?)> <choice><option>선택</option></choice> (O) <choice> 선택 </choice> (O) * => 0회 이상 반복되는 요소 선언 <!ELEMENT mult (no*)> <!ELEMENT no (#PCDATA)> <mult><no>1</no><no>2</no></mult> (O) <mult /> (O)

30  + => 1회 이상 반복되는 요소 선언 <!ELEMENT mult (no+)> <!ELEMENT no (#PCDATA)> <mult><no>1</no><no>2</no></mult> (O) <mult /> (X) ( ) => 자식 요소들의 그룹 지정 <!ELEMENT node ( (A | B), C)> <!ELEMENT A (#PCDATA)> <!ELEMENT B (#PCDATA)> <!ELEMENT C (#PCDATA)> <node> <A>1</A><C>2</C> </node>

31 속성(Attribute) 1. 속성 선언은 <!ATTLIST 로 한다.
> 5 <!ATTLIST 요소이름 속성 유형 1 2 4 속성이름 3 1. 속성 선언은 <!ATTLIST 로 한다. 2. 요소이름은 속성이 부여될 XML 요소이름을 지정. 3. 속성이름을 지정. 4. 속성의 유형을 지정. 5. > 로 속성 선언을 종료. ▣ 속성 유형 CDATA Enumeration(나열형식) ID IDREF IDREFS ENTITY ENTITIES NMTOKEN NMTOKENS NOTATION

32 DTD의 한계 제대로 된 DTD를 만들기가 어렵다. DTD는 확장할 수 없다.
DTD는 데이터로서의 XML을 제대로 기술하지 못한다. DTD는 이름공간(Namespace)를 제대로 지원하지 못한다. DTD는 기술(description)능력에 한계가 있다. DTD에는 기본 요소 내용에 대한 기능이 없다.

33 Schema

34 What is XML Schemas? Answer: An XML vocabulary for expressing your data's business rules

35 Validating your data XML Schema validator Data is ok! XML Schema
<location> <latitude> </latitude> <longitude> </longitude> <uncertainty units="meters">2</uncertainty> </location> XML Schema validator Data is ok! -check that the latitude is between -90 and +90 -check that the longitude is between -180 and +180 - check that the fraction digits is 6 for lat and lon ... XML Schema

36 Purpose of XML Schemas (and DTDs)
Specify: the structure of instance documents "this element contains these elements, which contains these other elements, etc" the datatype of each element/attribute "this element shall hold an integer with the range 0 to 12,000" (DTDs don't do too well with specifying datatypes like this)

37 Motivation for XML Schemas
People are dissatisfied with DTDs It's a different syntax You write your XML (instance) document using one syntax and the DTD using another syntax --> bad, inconsistent Limited datatype capability DTDs support a very limited capability for specifying datatypes. You can't, for example, express "I want the <elevation> element to hold an integer with a range of 0 to 12,000" Desire a set of datatypes compatible with those found in databases DTD supports 10 datatypes; XML Schemas supports 44+ datatypes

38 Highlights of XML Schemas
XML Schemas are a tremendous advancement over DTDs: Enhanced datatypes 44+ versus 10 Can create your own datatypes Written in the same syntax as instance documents less syntax to remember Object-oriented'ish Can extend or restrict a type (derive new type definitions on the basis of old ones) Can express sets, i.e., can define the child elements to occur in any order Can specify element content as being unique (keys on content) and uniqueness within a region Can define multiple elements with the same name but different content Can define elements with nil content Can define substitutable elements - e.g., the "Book" element is substitutable for the "Publication" element.

39 Classic use of XML Schemas (Trading Partners - B2B)
Software to Process P.O. P.O. "P.O. is okay" Supplier Consumer P.O. Schema Validator P.O. Schema (Schema at third-party, neutral web site)

40 Let's Get Started! Convert the BookStore.dtd (next page) to the XML Schema syntax for this first example we will make a straight, one-to-one conversion, i.e., Title, Author, Date, ISBN, and Publisher will hold strings, just like is done in the DTD We will gradually modify the XML Schema to use stronger types

41 BookStore.dtd <!ELEMENT BookStore (Book)+>
<!ELEMENT Book (Title, Author, Date, ISBN, Publisher)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)>

42 ELEMENT ATTLIST BookStore #PCDATA Author Book ID Title NMTOKEN CDATA Publisher ISBN ENTITY Date This is the vocabulary that DTDs provide to define your new vocabulary

43 (targetNamespace) complexType element BookStore sequence Author Book schema boolean Title string Publisher ISBN integer Date This is the vocabulary that XML Schemas provide to define your new vocabulary One difference between XML Schemas and DTDs is that the XML Schema vocabulary is associated with a name (namespace). Likewise, the new vocabulary that you define must be associated with a name (namespace). With DTDs neither set of vocabulary is associated with a name (namespace) [because DTDs pre-dated namespaces].

44 (explanations on succeeding pages) <?xml version="1.0"?>
<xsd:schema xmlns:xsd=" <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:schema> (explanations on succeeding pages) BookStore.xsd (see example01) xsd = Xml-Schema Definition

45 <?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element ref="Book" minOccurs="1" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Book"> <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Author" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Date" minOccurs="1" maxOccurs="1"/> <xsd:element ref="ISBN" minOccurs="1" maxOccurs="1"/> <xsd:element ref="Publisher" minOccurs="1" maxOccurs="1"/> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:schema> <!ELEMENT BookStore (Book)+> <!ELEMENT Book (Title, Author, Date, ISBN, Publisher)> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)>

46 Default Value for minOccurs and maxOccurs
The default value for minOccurs is "1" The default value for maxOccurs is "1" <xsd:element ref="Title" minOccurs="1" maxOccurs="1"/> Equivalent! <xsd:element ref="Title"/> Do Lab1

47 Anonymous types <?xml version="1.0"?>
<xsd:schema xmlns:xsd=" <xsd:element name="BookStore"> <xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> Anonymous types

48 Using <sequence> and <choice>
DTD: <!ELEMENT life ((work, eat)*, (work | play), sleep)* > <?xml version="1.0"?> <xsd:schema xmlns:xsd=" <xsd:element name="life"> <xsd:complexType> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:element name="work" type="xsd:string"/> <xsd:element name="eat" type="xsd:string"/> </xsd: sequence> <xsd:choice> <xsd:element name="play" type="xsd:string"/> </xsd:choice> <xsd:element name="sleep" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> XML Schema:

49 Summary of Declaring Elements (two ways to do it)
1 <xsd:element name="name" type="type" minOccurs="int" maxOccurs="int"/> A simple type (e.g., xsd:string) or the name of a complexType (e.g., BookPublication) A nonnegative integer A nonnegative integer or "unbounded" Note: minOccurs and maxOccurs can only be used in nested (local) element declarations. 2 <xsd:element name="name" minOccurs="int" maxOccurs="int"> <xsd:complexType> </xsd:complexType> </xsd:element>

50 Built-in Datatypes Primitive Datatypes string boolean decimal float
double duration dateTime time date gYearMonth gYear gMonthDay Atomic, built-in "Hello World" {true, false} 7.08 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN P1Y2M3DT10H30M12.3S format: CCYY-MM-DDThh-mm-ss format: hh:mm:ss.sss format: CCYY-MM-DD format: CCYY-MM format: CCYY format: --MM-DD Note: 'T' is the date/time separator INF = infinity NAN = not-a-number

51 Built-in Datatypes (cont.)
Primitive Datatypes gDay gMonth hexBinary base64Binary anyURI QName NOTATION Atomic, built-in format: ---DD (note the 3 dashes) format: --MM-- a hex string a base64 string a namespace qualified name a NOTATION from the XML spec

52 Built-in Datatypes (cont.)
Derived types normalizedString token language IDREFS ENTITIES NMTOKEN NMTOKENS Name NCName ID IDREF ENTITY integer nonPositiveInteger Subtype of primitive datatype A string without tabs, line feeds, or carriage returns String w/o tabs, l/f, leading/trailing spaces, consecutive spaces any valid xml:lang value, e.g., EN, FR, ... must be used only with attributes part (no namespace qualifier) 456 negative infinity to 0

53 Built-in Datatypes (cont.)
Derived types negativeInteger long int short byte nonNegativeInteger unsignedLong unsignedInt unsignedShort unsignedByte positiveInteger Subtype of primitive datatype negative infinity to -1 to to to 32767 -127 to 128 0 to infinity 0 to 0 to 0 to 65535 0 to 255 1 to infinity Note: the following types can only be used with attributes (which we will discuss later): ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.

54 Attributes On the next slide I show a version of the BookStore DTD that uses attributes. Then, on the following slide I show how this is implemented using XML Schemas.

55 <!ELEMENT BookStore (Book)+>
<!ELEMENT Book (Title, Author+, Date, ISBN, Publisher)> <!ATTLIST Book Category (autobiography | non-fiction | fiction) #REQUIRED InStock (true | false) "false" Reviewer CDATA " "> <!ELEMENT Title (#PCDATA)> <!ELEMENT Author (#PCDATA)> <!ELEMENT Date (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT Publisher (#PCDATA)> <!ELEMENT Month (#PCDATA)> <!ELEMENT Year (#PCDATA)> BookStore.dtd

56 <xsd:element name="BookStore">
<xsd:complexType> <xsd:sequence> <xsd:element name="Book" maxOccurs="unbounded"> <xsd:element name="Title" type="xsd:string"/> <xsd:element name="Author" type="xsd:string" maxOccurs="unbounded"/> <xsd:element name="Date" type="xsd:string"/> <xsd:element name="ISBN" type="xsd:string"/> <xsd:element name="Publisher" type="xsd:string"/> </xsd:sequence> <xsd:attributeGroup ref="BookAttributes"/> </xsd:complexType> </xsd:element> <xsd:attributeGroup name="BookAttributes"> <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> <xsd:attribute name="InStock" type="xsd:boolean" default="false"/> <xsd:attribute name="Reviewer" type="xsd:string" default=" "/> </xsd:attributeGroup> Category (autobiography | non-fiction | fiction) #REQUIRED InStock (true | false) "false" Reviewer CDATA " "

57 "Instance documents are required to have the Category attribute
<xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute> "Instance documents are required to have the Category attribute (as indicated by use="required"). The value of Category must be either autobiography, non-fiction, or fiction (as specified by the enumeration facets)." Note: attributes can only have simpleTypes (i.e., attributes cannot have child elements).

58 Summary of Declaring Attributes (two ways to do it)
1 <xsd:attribute name="name" type="simple-type" use="how-its-used" default/fixed="value"/> required optional prohibited Do not use the "use" attribute if you use either default or fixed. xsd:string xsd:integer xsd:boolean ... 2 <xsd:attribute name="name" use="how-its-used" default/fixed="value"> <xsd:simpleType> <xsd:restriction base="simple-type"> <xsd:facet value="value"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute>

59 MSXML Parser를 사용할 경우는 4.0으로 업그레이드 해야 한다.
그리고 MSXML2.DOMDocument.4.0 객체 생성 후 테스트 해야 함.


Download ppt "Basic XML, DTD & Schema."

Similar presentations


Ads by Google