ホーム HTML CSS XML JAVASCRIPT   PHP SQL MORE...   リファレンス 事例集    

XSD How To?

« 前章へ 次章へ »

XML文書は、DTDまたはXMLスキーマを参照することができます。


簡単なXML文書

"note.xml"と言う簡単なXML文書を見てください:

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>


DTDファイル

次の例は、上記XML文書("note.xml")の要素を定義する "note.dtd"というDTDファイルです:

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

最初の行は、4つの子要素:"to, from, heading, body" を持つ note 要素を定義しています。

2-5行は、"#PCDATA"型の to, from, heading, body 要素を定義しています。


XMLスキーマ

次の例は、上記XML文書("note.xml")の要素を定義する"note.xsd" というXMLスキーマファイルです:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.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 要素は、他の要素を含んでいるので、複合型といいます。 note 以外の要素 (to, from, heading, body) は、 他の要素を含んでいないので、単純型といいます。 次の章では、単純型と複合型に関して更に学習します。


DTDの参照

次は、DTD を参照するXML文書です:

<?xml version="1.0"?>

<!DOCTYPE note SYSTEM
"http://www.w3schools.com/dtd/note.dtd">

<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>


XMLスキーマの参照

次は、XMLスキーマを参照するXML文書です:

<?xml version="1.0"?>

<note
xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com note.xsd">
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

« 前章へ 次章へ »