** XSD : XML Schema Definition

W3C 표준으로 발표된 XML 스키마 언어 중 하나로 XML 스키마 전용 언어이다.

XML 문서가 그 스키마에 대해 유효한 것으로 여겨지기 위해 반드시 지켜야 하는 규칙들의 집합을 표현하는 데 쓰인다. But 다른 것들과 달리 XSD는 문서의 유효성 검증을 통해 특정 자료형들에 종속적인 정보들의 묶음을 만들어 내자는 의도로 설계되었다.

 

The purpose of an XML Schema is to define the legal building blocks of an XML document

 

Why Learn XML Schema?

In the XML world, hundreds of standardized XML formats are in daily use.

Many of these XML standards are defined by xml schemas

XML Schema is an XML-based (and more powerful ) alternative to DTD

 

XML Schemas Support Data Types

It is easier to describe allowable document content

It is easier to validate the correctness of data

It is easier to define data facets

It is easier to define data patterns

It is easier to convert data between different data types

 

XML Schemas use XML Syntax

XML Schemas is that they are written in XML

XML Schemas are extensible

- Reuse your Schema in other Schemas

- Create your own data types derived from the standard types

- Reference multiple schemas in the same document

 

XSD How to?

** DTD (Document Type Definition) : XML문서의 구조를 명시적으로 정의하기 위한 방법, 업계별로 표준안을 정의하는 용도로 사용. 하나의 XML 문서는 하나의 DOCTYPE 선언을 통해 오직 하나의 DTD와 결합된다.

 

XML 스키마 : XML 문법 준수, 네임스페이스 지원, 다양한 빌트인 데이터 형식과 사용자 지정 데이터 형식을 지원, 반복 연산자를 다양하게 지정, W3C(2001) 표준

A simple XML Document

<?xml version=”1.0”?>

<note>

           <to>Tove</to>

           <from>Jani</from>

           <heading>Reminder</heading>

           <body>sentence</body>

</note>

 

The note element is a complex type cuz it contains other elements. The other elements (to, from, heading, body) are simple types cuz they do not contain other elements.

 

The <schema> element

<xs:schema></xs:schema>

 

XSD Simple Elements

A simple element is an XML element that contains only text. It cannot contain any other elements or attributes

<xs:element name=”xxx” type=”yyy”/>

e.g à xs:string, decimal, integer, boolean, date, time

e.g2

<xs:element name=”lastname” type=”xs:string”/>

<xs:element name=”age” type=”xs:integer”/>

<xs:element name=”dateborn” type=”xs:date”/>

 


 

Default and Fixed Values for Simple Elements

e.g) <xs:element name=”color” type=”xs:string” default=”red”/>

<xs:element name=”color” type=”xs:string” fixed=”red”/>

 

XSD Attributes

All attributes are declared as simple types

<xs:attribute name=”xxx” type=”yyy”/>

e.g) <lastname lang=”EN”>Smith</lastname>

           <xs:attribute name=”lang” type=”xs:string”/>

 

Default and Fixed Values for Attributes

e.g) <xs:attribute name=”lang” type=”xs:string” default=”EN”/>

 

XSD Restrictions / Facets

e.g 나이 제한을 lower than 0, greater than 120으로 잡고 싶을 때

<xs:element name=”age”>

           <xs:simpleType>

             <xs:restriction base=”xs:integer”>

               <xs:minInclusive value=”0”/>

               <xs:maxInclusive value=”120”/>

             </xs:restriction>

           </xs:simpleType>

</xs:element>

 

 

<xs:element name = “car”>

<xs:simpleType>

  <xs:restriction base = “xs:string”>

    <xs:enumeration value = “Audi”/>

    <xs:enumeration value = “Golf”/>

    <xs:enumeration value = “BMW”/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

------------------------------------------------------------- different way to use

<xs:element name = “car” type=”carType”/>

 

<xs:simpleType name = “carType”>

 <xs:restriction base = “xs:string”>

  <xs:enumeration value = “Audi”/>

  <xs:enumeration value = “Golf”/>

  <xs:enumeration value = “BMW”/>

 </xs:restriction>

</xs:simpleType>

-----------------------------------------------------------------------------------------------

<xs:element name = “letter”>

<xs:simpleType>

  <xs:restriction base = “xs:string”>

        // only 소문자 1글자 or 대문자 3글자

        <xs:pattern value = “[a-z]”/>

  </xs:restriction>

</xs:simpleType>

</xs:element>

 

** <xs:element name = “initials”> : [A-Z] [A-Z] [A-Z]  ß 대문자 3글자만 조건

** <xs:element name = “initials”> : [a-zA-Z] [a-zA-Z] [a-zA-Z]  ß 대소문자 3글자만 조건

** <xs:element name = “choice”> : [xyz]  ß 대소문자 3글자만 조건

 

** <xs:element name = “prodid”> : xs:integer,  “[0-9] [0-9] [0-9]”  ß 3 숫자만 조건

** <xs:element name = “letter”> : “([a-z]*)” ß The acceptable value is zero or more occurrences of lowercase letters from a to z

** <xs:element name = “gender”> : “male||female”

** <xs:element name = “password”> : “[a-zA-Z0-9]{8}” ß 대소문자 or 숫자 구성 8자리

** <xs:element name = “address”> : “preserve or replace or collapse” ß whitespace 보존, 대체, 제거  

 

Restrictions on Length

<xs:element name = “password”>

<xs:simpleType>

  <xs:restriction base = “xs:string”>

    <xs:length value = “8”/>

        Or

    <xs:minLength value = “5”/>

<xs:maxLength value = “8”/>

  </xs:restriction>

</xs:simpleType>

</xs element>

 

XSD Complex Elements

A complex element contains other elements and / or attributes

There are four kinds of complex elements

- Empty elements : <product pid = “1235”/> ß empty

- only other elements : contains only other elements

<employee>

  <firstname>John</firstname>

  <lastname>Smith</lastname>

</employee>

- only text : <food type = “dessert”>candy</food>

- both other elements and text : description

<description>

It happened on <date lang = “norwegian”>03.03.99</date> …

</description>

 

How to Define a Complex Element

<xs:element name = “employee”>

<xs:complexType>

  <xs:sequence>

    <xs:element name = “firstname” type = “xs:string”/>

<xs:element name = “lastname” type = “xs:string”/>

  </ xs:sequence>

</xs:complexType>

</xs:element>

 

Or

 

<xs:element name = “employee” type = “personinfo”>

 

<xs:complexType name = “persioninfo”>

<xs:sequence>

  <xs:element name = “firstname” type = “xs:string”/>

<xs:element name = “lastname” type = “xs:string”/>

</xs:sequence>

</xs:complexType>

'문돌이의 IT > JSP&Servlet' 카테고리의 다른 글

XSD Schema(XML Schema Definition)2  (0) 2017.01.15
XSD Schema(XML Schema Definition)  (0) 2017.01.14
SOAP이란?  (0) 2017.01.09
서블릿(servlet)과 JSP(JavaServer Pages)  (0) 2016.11.13
웹에서 PrintWriter의 역할  (0) 2016.05.11

+ Recent posts