XSD를 어떻게 사용하나요?

XML 문서는 DTD 또는 XML Schema를 참조할 수 있습니다.

간단한 XML 문서:

이 이름이 "note.xml"인 XML 문서를 보세요:

<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>기억하시오</heading>
<body>회의를 잊지 마세요!</body>
</note>

DTD 파일

이 예제는 "note.dtd"라는 이름의 DTD 파일로, 위의 XML 문서의 요소를 정의합니다:

<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>

1 행에서 note 요소는 "to, from, heading, body"라는 네 개의 서브 요소를 정의합니다.

2-5 행에서 to, from, heading, body 요소의 타입은 "#PCDATA"로 정의되었습니다.

XML Schema

아래의 예제는 "note.xsd"라는 이름의 XML Schema 파일로, 위의 XML 문서의 요소를 정의합니다:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.codew3c.com"
xmlns="http://www.codew3c.com"
elementFormDefault="qualified">
<xs:element name="note">
    <xs:complexType>
      <xs:sequence>
	<xs:element name="to" type="xs:string"/>
	<xs:element name="from" type="xs:string"/>
	<xs:element name="heading" type="xs:string"/>
	<xs:element name="body" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

note 요소는 다른 서브 요소를 포함하기 때문에 복합 유형입니다. 다른 요소 (to, from, heading, body)는 다른 요소를 포함하지 않기 때문에 간단 유형입니다. 복합 유형과 간단 유형에 대한 더 많은 내용을 아래 장에서 배울 수 있습니다.

DTD에 대한 참조

이 파일은 DTD에 대한 참조를 포함하고 있습니다:

<?xml version="1.0"?>
<!DOCTYPE note SYSTEM "http://www.codew3c.com/dtd/note.dtd">
<note>
<to>George</to>
<from>John</from>
<heading>기억하시오</heading>
<body>회의를 잊지 마세요!</body>
</note>

XML Schema에 대한 참조

이 파일은 XML Schema에 대한 참조를 포함하고 있습니다:

<?xml version="1.0"?>
<note>
xmlns="http://www.codew3c.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.codew3c.com note.xsd">
<to>George</to>
<from>John</from>
<heading>기억하시오</heading>
<body>회의를 잊지 마세요!</body>
</note>