ZVON > Tutorials > XML Schema and Relax NG Tutorial |
Intro / Search / ZVON |
Index | >> Example 3 / 8 << | Prev | Next | |
The element "A" represents your salary - it must be positive number and it must be rounded to two decimal places. We will define our custom type "Salary" using "minExclusive" and "fractionDigits" elements. The element "A" must be of that type.
Valid document <A xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >1000.00</A> Invalid document <A xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >-1000.00</A> Invalid document <A xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >999.9999</A> |
Correct XML Schema (correct_0.xsd) <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" > <xsd:element name="A" type="Salary"/> <xsd:simpleType name="Salary"> <xsd:restriction base="xsd:decimal"> <xsd:minExclusive value="0"/> <xsd:fractionDigits value="2"/> </xsd:restriction> </xsd:simpleType> </xsd:schema> |
The element "A" must be positive number and it must be rounded to two decimal places. We will use the "decimal" datatype from XML Schemas and restrict it using the "fractionDigits" and "minExclusive" parameters (facets).
Valid document <A xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >1000.25</A> Valid document <A xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >1000.0</A> Invalid document <A xmlns="">-1000.00</A> Invalid document <A xmlns="">999.9999</A> |
Correct Relax NG schema (correctRelax_0.rng) <grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes" xmlns="http://relaxng.org/ns/structure/1.0" > <start> <element> <name ns="">A</name> <ref name="Salary"/> </element> </start> <define name="Salary"> <data type="decimal"> <param name="minExclusive">0</param> <param name="fractionDigits">2</param> </data> </define> </grammar> |