XSDハウツー?
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)>
最初の行は、note要素を4つの子要素(「to、from、heading、body」)を持つように定義しています。
2-5行目では、to、from、heading、body要素を「#PCDATA」タイプに定義しています。
XMLスキーマへ
次の例は、上記のXMLドキュメント( "note.xml")の要素を定義する "note.xsd"というXMLスキーマファイルです。
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://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要素は他の要素を含んでいるため、複合型です。他の要素(to、from、heading、body)は、他の要素を含まないため、単純なタイプです。次の章では、単純型と複雑型について詳しく学習します。
DTDへの参照
このXMLドキュメントにはDTDへの参照があります。
<?xml version="1.0"?>
<!DOCTYPE note SYSTEM
"https://www.w3schools.com/xml/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="https://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.w3schools.com/xml note.xsd">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>