1 / 17

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

ash
Download Presentation

XML Schema Definition Language (XSD)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Schema Definition Language (XSD) Eugenia Fernandez IUPUI

  2. 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

  3. Status of XSD • Became W3C Recommendation on May 2, 2001 • Specification available at http://www.w3c.org/XML/Schema

  4. 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>

  5. 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

  6. XSD Schema Element <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> ... </xsd:schema>

  7. Tag Types • Simple • contain only data content • Complex • contain other elements and/or have attributes

  8. Defining Simple Elements <xsd:element name="element-name" type="xsd:datatype" /> <xsd:element name="firstname" type="xsd:string"/>

  9. 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"/>

  10. 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

  11. Defining Complex Elements <xsd:element name="parent-element"> <xsd:complexType> <xsd:element name="child-element" type="simple-type-ref"/> </xsd:complexType> </xsd:element>

  12. 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>

  13. Occurrence Constraints • Via attributes of the element tag • minOccurs="number" • maxOccurs="{number | unbounded}" • Default values on both minOccurs & maxOccurs = 1

  14. 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"/>

  15. 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

  16. 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>

  17. 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

More Related