XML Schema

advertisement
XML – Part III
The <complexType> Element …
 This type of element either has the element content or the
mixed content (child element and data)
The attributes of the <complexType> element include:
‘id’, which provides a unique id for the element
‘mixed’, a Boolean that indicates whether the element has a
mixed content or not (the default value is false), and
‘name’, which gives a name to the element
complexType’s children elements
 The children elements of the <complexType> element
can be:
<simpleContent>
<complexContent>
<all>
<group>
<sequence>
simpleContent …
 The simpleContent allows the complexType element to have
leaf nodes of character data, without any child element
 The content of a simpleContent element must either be:
extension or restriction of an existing built in or derived
datatypes
 As an example for using extension, we can make a
complexType element called RockName by extending the
built in string datatype in the simpleContent element
 The RockName element (next slide) can only have character
data as its value
complexType by simpleContent by
extension
<xs:element name="RockName">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string"/>
</xs:simpleContent>
</xs:complexType>
</xs:element>
complextype by simpleContent
by restriction
 As an example for using a restriction in the simpleContent
of a complexType, we can define the complexType called
CelciusTemperature by restricting the xs:decimal
number to have a minimum value of -273.15 oC
<xs:element name="CelciusTemperature">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:decimal"/>
<minInclusive value="-273.15"/>
</xs:simpleContent>
</xs:complexType>
</xs:element>
complexType with complexContent
 The complexType element with complexContent can be
built by adding element content, or a combination of child
elements and character data, i.e., mixed content
 This is done by defining a list of elements and attributes
 The following is an example of a complexType element
called MineralInfo with an element content, using the
<xs:sequence> which defines the order that the child
element should appear
 Note that in this example, there is no need to have the
<xs:complexContent> element at all. We can just put the
<xs:sequence> in the <xs:compleType>
complexType w/ element content
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MineralInfo">
<xs:complextype>
< !-- <xs:complexContent> -->
<xs:sequence>
<xs:element name="name"/>
<xs:element name="type"/>
<xs:element name="composition"/>
<xs:element name="hardness"/>
<!-- other physical and chem. properties -->
</xs:sequence>
< !-- </xs:complexContent> -->
</xs:complextype>
</xs:element>
</xs:schema>
Mixed content
 We can now build a mineralogy element whose type is
MineralInfo.
<xs:element name=”Mineralogy” type=”MineralInfo”/>
 To build a mixed content <complexType> element we set
the ‘mixed’ attribute of the <complexType> element to
‘true’
 This allows us to add text nodes before, between, and after
the child nodes
 In the following, we define a Rock element that allows
writing the description of the rock in text, anywhere in the
document
Example of mixed content
<xs:element name="Rock">
<xs:complexType mixed=”true”>
<xs:sequence>
< !-- assume MineralInfo is already declared -->
<xs:element name="Mineralogy" type="MineralInfo"/>
<xs:element name="type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:sequence>
 The <xs:sequence> element provides a mandatory order to the
sequence of elements in a complex type.
 For example, to force the dip to follow the strike of a planar
structure, we can use the sequence element to do just that
<xs:element name="PlanarAttitude">
<xs:complexType>
<xs:sequence>
<xs:element name="Strike" type="xs:string"/>
<xs:element name="Dip" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
 Let’s say that some geologists provide dip direction and dip amount,
instead of strike and dip for a planar feature, and we want to give them
this option to enter their data. We do it as follows:
<xs:element name="PlanarAttitude">
<xs:complexType>
<xs:group>
<xs:sequence>
<! -- defines a group containing a choice of two sequences -- >
<xs:group>
<xs:choice>
<xs:sequence>
<xs:element name="Strike" type="xs:string"/>
<xs:element name="Dip" type="xs:string"/>
</xs:sequence>
<xs:sequence>
<xs:element name="DipDirection" type="xs:string"/>
<xs:element name="DipAmount" type="xs:string"/>
</xs:sequence>
</xs:choice>
</xs:group>
</xs:sequence>
</xs:group>
</xs:complexType>
</xs:element>
XML instance document
 Because of the choice, we may have either one of the
following in an instance document:
<PlanarAttitude>
<Strike> N30E </Strike>
<Dip> 65SE </Dip>
</PlanarAttitude>
or
<PlanarAttitude>
<DipDirection> S60E </DipDirection>
<DipAmount> 65 </DipAmount>
</PlanarAttitude>
<xs:all>
 In contrast to the <xs:sequence> element, the <xs:all>
element does not put any restriction on the ordering of its
sub-elements
 However, the <xs:all> element must be at the top-level, and
cannot contain, or be part of, a sequence or a choice
 For example, we can define the ‘Structure’ element to have
a set of elements in any order
 This means that an instance document can have any of the
Name, Type, and Attitude listed in any order
Example for <xs:all>
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name=“Structure">
<xs:complexType>
<xs:all>
<xs:element name="Name"/>
<xs:element name="Type"/>
<xs:element name="Attitude"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
Attributes
 In W3C XSD schema, the attributes are declared with the
<xs:attribute> element, which has its own attributes
 Local attributes can be declared within a certain element,
as opposed to those that have a global scope, which are
declared in the <schema> element, and that could be
used by any element or attribute group
 For example, we can define the local ‘TemperatureType’
attribute for the Temperature element, to indicate the type
of the temperature system (Celcius, Farenheit, etc.)
 This attribute can only be referenced by instances of the
Temperature element
Local attribute
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Temperature">
<xs:complexType>
<xs:attribute name="TemperatureType"/>
</xs:complexType>
</xs:element>
</xs:schema>
Attributes of the <xs:attribute>
 The attribute element has several attributes of its own, and
include name, type, default, fixed, id, ref, and use:
 The ‘name’ attribute of the <attribute> element specifies
the name for the attribute, which needs to be a valid XML
name, e.g.,
<attribute name=”color” type=”xs:string”/>
 Notice that the type for the attribute is given by the W3C
XSD string datatype
 We can have two attributes with the same name only if
their type is different, and defined in different content (one
for an element, and the other as an attribute)
 The ‘type’ attribute specifies the simple built-in or
derived (i.e., restriction of simple types) datatype for
the attribute in the instance document
 These datatypes include (there are many more):
boolean, integer, decimal, float, date, time, and string.
 Contrary to the elements, attributes cannot have
complex types
 The ‘default’ attribute of the <attribute> element assigns a
preset value to the attribute if no value is given in the instance
document
 For example, we can define an attribute for kink bands to
specify their straight limb and angular hinge as follows:
<xs:attribute name="style" default="straight limb, angular hinge"/>
 Or, we can define the sample attribute with a default value of
‘rock’ as follows:
<xs:attribute name=”sample” default=”rock” type=”xs:string”/>
 In this case, if not specified, the sample type will be assigned to
‘rock’. Note: there might be other sample types: water, soil, etc.
 If we want the value of an attribute not to change, we
use the ‘fixed’ attribute
 For example, if we want the formula for quartz to
always be SiO2, we can fix it as follows:
<xs:attribute name=”quartzFormula” fixed=”SiO2”/>
 The id attribute specifies a unique identifier for the attribute
 The id can then be referenced by other elements
 Notice that an attribute can also be referenced by its name
 The ref attributes allows referencing another attribute
 For example, let’s say we have a complexType aquifer element,
which has two types: confined and unconfined, each with its
own set of attributes that are of type ref, which reference the
porosity and permeability attributes
 Notice that the references to the porosity and permeability
attributes are global because they are declared in the schema
element before the porosity and permeability are declared
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Aquifer">
<xs:complexType>
<xs:sequence>
<xs:element name="ConfinedAquifer">
<xs:complexType mixed="true">
<xs:attribute ref="porosity"/>
<xs:attribute ref="permeability"/>
</xs:complexType>
</xs:element>
<xs:element name="UnConfinedAquifer">
<xs:complexType mixed="true">
<xs:attribute ref="porosity"/>
<xs:attribute ref="permeability"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!- - These attributes are global; defined in the schema - - >
<xs:attribute name=“porosity" type="xs:string"/>
<xs:attribute name=“permeability" type="xs:string"/>
</xs:schema>
 The ‘use’ attribute specifies the usage of the <xs:attribute> with
its own ‘optional’, ‘prohibited’, and ‘required’ attributes
 The default value for the ‘use’ attribute, when none of these is
defined, is ‘optional’
 Contrary to the ‘required’ value, which specifies that the
attribute must be present, the prohibited value indicates that
the attributes should not be used at all
 In addition to the ref attribute which allows referencing and
reuse of attributes, we can structure and reference a group of
attributes by the <attributeGroup>
 For example, we can structure information about folds in a group
called FoldInfo, and then reference it with the name of the
group in an element called Measurement
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attributeGroup name="FoldfInfo">
<xs:attribute name="hingeline" use="optional" type="xs:string"/>
<xs:attribute name="axis" use="optional" type="xs:string"/>
<xs:attribute name="axialPlane" type="xs:string"/>
<xs:attribute name="limb1" use="required" type="xs:string"/>
<xs:attribute name="limb2" use="required" type="xs:string"/>
<xs:attribute name="limb3" use="optional" type="xs:string"/>
<xs:attribute name="limb4" use="optional" type="xs:string"/>
<xs:attribute name="axialTrace" type="xs:string"/>
</xs:attributeGroup>
<xs:element name="Measurement">
<xs:complexType>
<xs:sequence>
<xs:element name="Fold">
<xs:complexType>
<xs:attributeGroup ref="FoldfInfo"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Enumerations
 Enumeration is a list of options that a user can select from
 For example, we can enumerate the exhaustive list of the
types for minerals
 We define an attribute called mineralType that has an
enumeration of the mineral types
 We then reference this attribute in the Mineral element
 Notice that enumeration is a restriction of a simpleType
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="mineralType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="silicate"/>
<xs:enumeration value="carbonte"/>
<xs:enumeration value="oxide"/>
<xs:enumeration value="hydroxide"/>
<!-- other types of minerals -->
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:element name="Mineral">
<xs:complexType>
<xs:attribute ref="mineralType"/>
</xs:complexType>
</xs:element>
</xs:schema>
Linking XSD schemas with instance
documents
 The XML processor that manipulates an instance
documents uses a special namespace that carries
information on identifying the location and type of the
associated schema
 The namespace URI for the instance document:
http://www.w3.org/2001/XMLSchema-instance
 This has the xsi prefix (Note: ‘i’ stands for ‘instance’).
 The xsi namespace has four attributes:
 xsi:type, xsi:nil, xsi:schemaLocation, and
xsi:noNamespaceSchemaLocation
 The xsi:type attribute assigns a type to the instance of
specific elements in the document
 The xsi:nil takes a Boolean value to indicate that an empty
element is valid
 The xsi:schemaLocation specifies the location of the
XSD schema for the document, with two URIs:
 the targetNamespace for the schema
 the URI that points to the location of the schema
 The xsi:noNamespaceSchemaLocation is used to locate
schemas (similar to the xsi:schemaLocation) that have no
namespace
For example, assume that we want to make an instance
document from the following schema which has a
Structure root element, with three elements of string type
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.gsu.edu/xml/struc/structure">
<xs:element name=“Structure">
<xs:complexType>
<xs:all>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Type" type="xs:string"/>
<xs:element name="Attitude" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
 A valid instance document based on the schema on the
previous slide would look like the following:
<?xml version="1.0" encoding="UTF-8"?>
<Structure xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xsi:noNamespaceSchemaLocation="file:/C:/Users/HAB/Desktop
/Structure.xsd">
<Name> Brevard </Name>
<Type> Fault </Type>
<Attitude> 045, 30NW </Attitude>
</Structure>
XSL Transformation
 Well-formed XML documents (i.e., those having a valid
XML syntax) can be viewed with an XML document reader,
such as the Internet Explorer (by double-clicking the file in
the Windows Explorer)
 Although, very useful, rendering the XML document in a
browser shows all the tags, and looks very cluttered.
 Because XML concentrates on document structure and not
presentation, it has the XSL (XML Stylesheet Language)
standard which allows manipulation of the XML
documents, and their controlled display on XML browsers
or editors
 XSL allows rendering the XML content into HTML, or
tabular and other formats.
 XSL is a more sophisticated XML styling language,
using a scripting language XSLT), compared to CSS
(Cascading Style Sheets) which was originally
designed for HTML
 Both have rules for say font size and color that are
applied or associated to an XML document.
Download