XSDを使用する方法
- 前のページ XSD がなぜ使用されるべきか
- 次のページ XSD <schema>
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」の4つの子要素を持つと定義されています。
第 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>
- 前のページ XSD がなぜ使用されるべきか
- 次のページ XSD <schema>