An overview of xml schemas, explaining why they are used instead of dtds, and how to define simple elements, attributes, and complex types using xsd. It covers restrictions, enumerations, global and local definitions, and referencing. The document also introduces various predefined string types, date and time types, and numeric types.
Download XML Schemas: Understanding XSD for Structuring XML Data and more Slides Computer Science in PDF only on Docsity!
XML Schemas
Docsity.com
XML Schemas
“Schemas” is a general term--DTDs are a form of XML schemas - According to the dictionary, a schema is “a structured framework or plan”
When we say “XML Schemas,” we usually mean the W3C XML Schema Language - This is also known as “XML Schema Definition” language, or XSD - I’ll use “XSD” frequently, because it’s short
DTDs, XML Schemas, and RELAX NG are all XML schema languages
Docsity.com
Why not XML schemas?
DTDs have been around longer than XSD
Therefore they are more widely used
Also, more tools support them
XSD is very verbose, even by XML standards
More advanced XML Schema instructions can be non-intuitive and confusing
Nevertheless, XSD is not likely to go away quickly
Docsity.com
Referring to a schema
To refer to a DTD in an XML document, the reference goes before the root element: - ...
To refer to an XML Schema in an XML document, the reference goes in the root element: - <rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" (The XML Schema Instance reference is required) xsi:noNamespaceSchemaLocation=" url .xsd"> (This is where your XML Schema definition can be found) ...
Docsity.com
The element may have attributes:
xmlns:xs="http://www.w3.org/2001/XMLSche ma" - This is necessary to specify where all our XSD tags are defined
elementFormDefault="qualified"
This means that all XML elements must be qualified (use a namespace)
It is highly desirable to qualify all elements, or problems will arise when another schema is added
Docsity.com
“Simple” and “complex”
elements
A “simple” element is one that contains text and nothing else - A simple element cannot have attributes - A simple element cannot contain other elements - A simple element cannot be empty - However, the text can be of many different types, and may have various restrictions applied to it
If an element isn’t simple, it’s “complex”
A complex element may have attributes
A complex element may be empty, or it may Docsity.com
Defining an attribute
Attributes themselves are always declared as simple types
An attribute is defined as <xs:attribute name=" name " type=" type " /> where: - name and type are the same as for xs:element
Other attributes a simple element may have:
default=" default value " if no other value isspecified Docsity.com
Restrictions, or “facets”
The general form for putting a restriction on a text value is: - <xs:element name=" name "> (or xs:attribute ) <xs:restriction base=" type "> ... the restrictions ... </xs:restriction> </xs:element>
length -- the string must contain exactly value characters
minLength -- the string must contain at least value characters
maxLength -- the string must contain no more than value characters
pattern -- the value is a regular expression that the string must match
whiteSpace -- not really a “restriction”--tells what to do with whitespace - value="preserve" Keep all whitespace - value="replace" Change all whitespace characters to spaces - value="collapse" Remove leading and trailing whitespace, and Docsity.com
Enumeration
An enumeration restricts the value to be one of a fixed set of values
Elements declared at the “top level” of a are available for use throughout the schema
Elements declared within a xs:complexType are local to that type
Thus, in <xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstName" type="xs:string" /> <xs:element name="lastName" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> the elements firstName and lastName are only locally declared
The order of declarations at the “top level” of a do not specify the order in the XML data document Docsity.com
Declaration and use
So far we’ve been talking about how to declare types, not how to use them
To use a type we have declared, use it as the value of type="..." - Examples: - <xs:element name="student" type="person"/> - <xs:element name="professor" type="person"/> - Scope is important: you cannot use a type if is local to some other type