XML Schema Definition Language (XSD)
XML Schema Definition Language (XSD). Eugenia Fernandez IUPUI. Purpose of XSD. Define the elements in an XML document Declare and define any attributes Establish the parent-child relationship among elements Define states and content models for elements and attributes Enforce data types
XML Schema Definition Language (XSD)
E N D
Presentation Transcript
XML Schema Definition Language (XSD) Eugenia Fernandez IUPUI
Purpose of XSD • Define the elements in an XML document • Declare and define any attributes • Establish the parent-child relationship among elements • Define states and content models for elements and attributes • Enforce data types • Establish default values
Status of XSD • Became W3C Recommendation on May 2, 2001 • Specification available at http://www.w3c.org/XML/Schema
Linking an XSD Schema to an XML Document • Add xmlns attribute to root element of xml document <root_element xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "filename.xsd"> </root_element>
XSD Schema Root Element • Root element is always schema • Root element always contains • an xmlns attribute that specifies the default namespace for the Schema element and its child elements • this xmlns attribute provides the prefix that will be added to the Schema element type names in the XSD instance
XSD Schema Element <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> ... </xsd:schema>
Tag Types • Simple • contain only data content • Complex • contain other elements and/or have attributes
Defining Simple Elements <xsd:element name="element-name" type="xsd:datatype" /> <xsd:element name="firstname" type="xsd:string"/>
Defining SimpleType Named Constraints • You can create your own simpleType element and use that element as the data type for other elements • Useful when many elements share same constraints <xsd:simpleType name="a_name" base="xsd:string"/> <xsd:element name="firstname" type="a_name"/><xsd:element name="lastname" type="a_name"/>
string normalizedString (stringwithwhitespaceremoved integer positiveInteger negativeInteger int decimal float, double boolean time, dateTime, date duration Datatypes For a complete list, go to http://www.w3.org/TR/xmlschema-0/#CreatDt
Defining Complex Elements <xsd:element name="parent-element"> <xsd:complexType> <xsd:element name="child-element" type="simple-type-ref"/> </xsd:complexType> </xsd:element>
Constraining the Sequence of Child Elements <xsd:element name="parent-element"> <xsd:complexType> <xsd:sequence> <xsd:element name="child-element" type="simple-type-ref"/> <xsd:element name="child-element" type="simple-type-ref"/> </xsd:sequence> </xsd:complexType> </xsd:element>
Occurrence Constraints • Via attributes of the element tag • minOccurs="number" • maxOccurs="{number | unbounded}" • Default values on both minOccurs & maxOccurs = 1
Declaring Attributes • Use <xsd:attribute> tag with these attributes • name = "attribute-name" • type = "xsd:datatype" • use = "{required | optional}" • default/fixed ="value" <xsd:attribute name="id" type="xsd:positiveInteger" use="required"/>
Data Typing • Multi-stage process • Begin by restricting content to a broad category using <xsd:restriction> element • Then narrow down the broad category with a more specific type • Restrictions on XML elements are called facets
Enumerated Attribute Values <xsd:attribute name="Category" use="required"> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:enumeration value="autobiography"/> <xsd:enumeration value="non-fiction"/> <xsd:enumeration value="fiction"/> </xsd:restriction> </xsd:simpleType> </xsd:attribute>
References • Day 6 in Sams Teach Yourself XML in 21 Days, Second Edition (Sams Publishing, 2001) • XML Schemas, a tutorial by Roger L. Costello, available at http://www.xfront.com/xml-schema.html