⑤XMLスキーマで値の有効範囲を定義
XMLスキーマで、値の有効範囲をチェックしてみます。
簡単なサンプルを作ってみます。
■ValueRange.xml
<?xml version="1.0" encoding="UTF-8"?> <greeting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ValueRange.xsd" attribute="10">100</greeting>
■ValueRange.xsd
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="greeting" type="greetingType" /> <xsd:complexType name="greetingType"> <xsd:simpleContent> <xsd:extension base="greetingString"> <xsd:attribute name="attribute" type="greetingString" use="required" /> </xsd:extension> </xsd:simpleContent> </xsd:complexType> <xsd:simpleType name="greetingString"> <xsd:restriction base="xsd:int"> <xsd:minInclusive value="10"/> <xsd:maxInclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:schema>
「greetingString」型を数値にしました。
数値の最小値は「
最小値、最大値の範囲内であればチェックOKになります。
… <xsd:restriction base="xsd:int"> <xsd:minInclusive value="10"/> <xsd:maxInclusive value="100"/> </xsd:restriction> …
Eclipseで、XMLスキーマ検証してみます。
XMLファイルを開いて、右クリックメニューの「検証」で、XMLスキーマ検証します。
エラーは出ませんね。
ValueRange.xmlの
■ValueRange.xml
<?xml version="1.0" encoding="UTF-8"?> <greeting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="SelectValue.xsd" attribute="9">101</greeting>
「ValueRange.xsd」では、