XML

6.XML Schema

mangji2 2024. 4. 13. 18:54

■개요

-DTD의 단점

: XML형식 아닌 EBNF형식 따름

:재사용성,확장성 떨어짐

:제한적 DATA TYPE 지원

-XML Schema

:markup 언어 및 xml 문서 양식 정의 언어

:data type 이용해서 element 및 속성 정의

:built-in data type 제공

:사용자 정의 data type 제공

:data type 상속 방법 제공

:xml namespace 지원

-XML Schema 이용 xml 문서 작성

:namespace 정의되지 않은 스키마 -> noNamespaceSchemaLocation

:namespace 정의 스키마  -> schemaLocation

 

스키마 문서의 구성

– Type definitions(정의)

▪ simple types ▪ complex types

– Declarations(선언)

▪ element ▪ attribute ▪ notation

– Group definitions

▪ model group, attribute group

– Constraint definitions

▪ unique, key, keyref

– Annotations

▶스키마 문서 구조

-<schema> element

:xml schema의 루트 엘리먼트

▶속성

:Namespace 선언  √Target namespace  √Default namespace  √Namespace prefixes 선언

√Target namespace : 설정할 언어의 namespace 찾기

√Default namespace: 디폴트의 namespace 정함(접두사 붙지 않을 때도 이 디폴트 namespace) 

▶<schema>의 자식 element

:외부 xml schema 관련 element    ✓<include> ✓<import>  ✓<redefine>

:new element 속성,타입 등 선언 element ✓<element>,attribute,simpleType,complexType

                                                                  ✓group,attributeGroup

                                                                  ✓notation,annotation

 

■<element> 선언

.명시적 정의 -> name || 다른 element 참조 -> ref 속성 이용

.element type 지정

▶ global type || 미리 정의된 타입,type 속성 통해 지정

▶명시적 (local type) ||<simpleType> <complexType> 이용 정의

.빈도수 제한 || minOccurs, maxOccurs (default:1) (maxOccurs:unbounded)//횟수 제한 x

.기본값, 고정값 설정 || default 또는 fixed

.관련 element,attribute 사이 유일성 or 참조 관계 제약 설정 || uniqe,key,keyref

.element 치환 그룹 

 

1.데이터값 갖는 엘리먼트 선언

-element type으로 simple type 사용 || built-in simple type or 사용자 정의 simple type

-빈도수 지정 || minOccurs, maxOccurs 속성 사용

2. 자식 엘리먼트나 속성을 갖는 엘리먼트 선언

element type으로 complex type 사용 ||

2.1 자식 element만 갖는 elemnet

2.2 속성만 갖는 empty element

2.3 자식 element + 속성 갖는 element

※ 속성 선언은 반드시 자식 element 선언 다음

2.4 data값과 속성 갖는 element

▪Complex type ▪ Simple content 및 속성 포함

더보기

ex)  <xsd:element name="가격"> // 일단 complextype->simpletype->단위에대한 type->속성

            <xsd:complexType>

               <xsd:simpleType>//데이터값 type

                  <xsd:extension base="xsd:int">////data 값 type

                        <속성내용>

                 </xsd:extenstion>

■ <Attribute> 선언

.name or ref

.type :simple type만 가능

.use || 속성 생략 여부 나타냄 "required" "optional(default)"

.default || 속성 생략 경우 자동적으로 그 값으로 인식

.fixed

 

▶참 고 ! !

-default 

속성 선언 시 || 속성 생략 시 그 속성 default

엘리먼트 선언 시 || 엘리먼트가 empty element -> contemnt에 default 값 가짐

                               element 자체 생략 -> 그 element 존재 x

-fixed || element,attribute 모두 사용

-빈도수 

elment || minoccurs , maxoccurs

attribute || use(0 or 1)

 

■ global(전역) 선언 vs local(지역) 선언

❑ Global element/attribute declaration

– 엘리먼트의 자식으로 선언

– 전역 선언된 엘리먼트는 XML 문서의 최상위 엘리먼트 (즉, 루트 엘리먼트)로 사용 가능

– 다른 엘리먼트/속성 선언에서 ref 속성을 통해 참조될 수 있음 (재사용 개념)

– minOccurs, maxOccurs, use 속성을 이용한 빈도수 지정 불가

❑ Local element/attribute declaration

– 타입 정의 안에서 선언됨

– 다른 선언에서 참조될 수 없음 (local scope를 가짐)

– minOccurs, maxOccurs, use 속성을 사용하여 빈도수 지정 가능

 

■ Data Types

? data type

: 엘리먼트의 content 또는 속성의 값으로 어떤 종류(구조)의 정보가 어떤 형태로 작성될 수 있는지 정의

-XML Schema는 다양한 데이터 타입을 제공함으로써 XML 문서의 구조를 더 세밀하게 정의할 수 있음

-XML Schema는 사용자가 새로운 데이터 타입을 정의하는 것을 지원함

☆ dtd -> data type 정확히 표현 x

 

▶data type의 분류

1. 사용용도

종류 설명 용도
built-in 정의되어 있는 type  
사용자 정의 simple type 새로 정의 속성 or element content 종류 지정
속성 값 반드시 simple type
엘리먼트 타입 simple type -> data만 갖는 element 
사용자 정의 complex type 새로 정의 자식 엘리먼트 또는 속성을 갖는 엘리먼트 선언 시 사용

2.정의되는 위치

-Global type                                                                         -Local type 

:schema element 자식으로 선언                                           :특정 element나 속성 선언 내에 정의

:name 속성 가짐                                                                    :name 속성 x

:참조 가능                                                                              :참조 불가

 

■Built-in Simple Type

■Simple Type

형태에 따라

- atomic type(restriction) || built-in type 이나 이미 정의된 simple type 을 제한하여 새로운 simple type 정의

: facets? data type 가질 수 있는 범위 제한 <facetName value="값"/>

-list type || 공백 문자열로 분리된 토큰들의 리스트를 값으로 갖는 타입 정의

.

-union type || 여러 개의 simiple type들을 결합하여 여러 종류의 데이터 값을 갖는 타입 정의

. <union memberTypes="type들의 이름"/>

※ 내부에 다른 element 나 속성 포함할 수 없음

 

■ Complex Type Definition

:속성이나 자식 element를 갖는 엘리먼트 선언에 필요한 타입

:속성은 complex type 사용 불가

●Global complex type  ●Local complex type

:정의 방법

1.Restriction of the un-restricted type(ur-type) :새로 정의 , 순서있는 자식 element들을 갖는 타입정의

<sequence/> 자식 element 하나만 있어도 사용

<choice/> 자식 element 선택적으로 가질 수 있는 complex type 정의

<attribute/> 자식 element와 속성 동시에 갖는 complex type 정의 || sequence choice 다음에 와야함

▶ 속성만 갖는 complex type -->즉,empty element 선언 시에도 complex type 사용

▶ data 및 속성을 동시에 갖는 complex type 정의

[참고]▶ <all /> || 명시된 자식 element들이 순서없이 한번씩만 나타날 수 있음

 <xsd:complexType mixed="True"> 혼합형 content 갖는 element type 정의 

-자식 element + 문자열 data가 혼합되어 사용가능

▶ 기존 complex type 확장,제한하여 새로운 complex type을 정의할 수 있음(상속 개념)

 

2.Extension or restriction of a simple content complex type

<simpleContent/> 사용

2-1. Extension

▶ data와 속성을 갖는 simple content complex type의 확장 -> 속성추가 새롭게 정의

▶ <simpleContent/> <extension/> 

2-2. Restriction

▶ 기반 complex type의 데이터 값 범위 제한

▶ <simpleContent/> <restriction/>

 

3.Extenstion or restriction of a complex content complex type

<complexContent/> 사용

3-1. Extension

▶ 새로운 자식 element나 속성을 추가하여 새로운 complex type 정의 || empty element도 확장 가능

3-2. Restriction

▶ 자식 element나 속성에 대해 제한

√ 기반 complex type의 모든 자식 element들을 재선언 해야함

 자식 엘리먼트의 타입 변경(제한), 빈도 수 제한, default 값 선언 등 가능

✓ 새로운 자식 엘리먼트를 추가할 수는 없음

✓ 주의: 자식 엘리먼트가 local type으로 선언된 경우 제한 불가

✓  속성은 제한하고자 하는 속성만 재선언 가능

✓ 속성 값의 제한은 작은 범위로의 제한만 가능

3-3. complex type의 다형성

-element 선언 시:xsi:type속성을 이용하여 다형성을 갖는 complex type을 지정

-xml 문서 작성 시:해당 complex type외에 그것을 확장한 파생 complex type의 엘리먼트 사용가능

 

■Model Group,Attribute Group

■ Constraints

■ Notation 선언

■ Namespace를 갖는 스키마

■ 스키마 문서의 결합

 

■Built-in Simple Type

■Simple Type

 

 

'XML' 카테고리의 다른 글

7.DOM  (0) 2024.04.25
5.XML Namespaces  (0) 2024.04.12
4.DTD를 이용한 문서 구조 설계  (0) 2024.03.31
XML  (0) 2024.03.29