Download presentation
Presentation is loading. Please wait.
Published byLanny Widjaja Modified over 6 years ago
1
XML Technologies Instructors: Geoffrey Fox and Bryan Carpenter
Dept. of Computer Science School of Computational Science and Information Technology 400 Dirac Science Library Florida State University Tallahassee Florida Nancy McCracken, Ozgur Balsoy 9/22/2018 it2xmltech01
2
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Outline XML Schema based on We cover all the topics in this primer Basic Schema Groups Namespaces Include Derived types import 9/22/2018 it2xmltech01
3
XML Schema PO Example po.xml I
<?xml version="1.0"?> <purchaseOrder orderDate=" "> <shipTo country="US"> <name>Alice Smith</name> <street>123 Maple Street</street> <city>Mill Valley</city> <state>CA</state> <zip>90952</zip> </shipTo> <billTo country="US"> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo> Need to add mechanism to associate schema with this XML instance 9/22/2018 it2xmltech01
4
XML Schema PO Example po.xml II
<comment>Hurry, my lawn is going wild!</comment> <items> <item partNum="872-AA"> <productName>Lawnmower</productName> <quantity>1</quantity> <USPrice>148.95</USPrice> <comment>Confirm this is electric</comment> </item> <item partNum="926-AA"> <productName>Baby Monitor</productName> <USPrice>39.98</USPrice> <shipDate> </shipDate> </items> </purchaseOrder> 9/22/2018 it2xmltech01
5
The Purchase Order Schema, po.xsd I
<xsd:schema xmlns:xsd=" <xsd:annotation> <xsd:documentation> Purchase order schema for Example.com. Copyright 2000 Example.com. All rights reserved. </xsd:documentation> </xsd:annotation> <xsd:element name="purchaseOrder" type="PurchaseOrderType"/> <xsd:element name="comment" type="xsd:string"/> <xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:element name="shipTo" type="USAddress"/> <xsd:element name="billTo" type="USAddress"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="items" type="Items"/> </xsd:sequence> <xsd:attribute name="orderDate" type="xsd:date"/> </xsd:complexType> 9/22/2018 it2xmltech01
6
The Purchase Order Schema, po.xsd II
<xsd:complexType name="USAddress"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> <xsd:element name="zip" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="country" type="xsd:NMTOKEN" use="fixed" value="US"/> </xsd:complexType> 9/22/2018 it2xmltech01
7
The Purchase Order Schema, po.xsd III
<xsd:complexType name="Items"> <xsd:sequence> <xsd:element name="item" minOccurs="0" maxOccurs="unbounded"> <xsd:complexType> <xsd:element name="productName" type="xsd:string"/> <xsd:element name="quantity"> <xsd:simpleType> <xsd:restriction base="xsd:positiveInteger"> <xsd:maxExclusive value="100"/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="USPrice" type="xsd:decimal"/> <xsd:element ref="comment" minOccurs="0"/> <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/> </xsd:sequence> <xsd:attribute name="partNum" type="SKU"/> </xsd:complexType> Anonymous type quantity Specify item 9/22/2018 it2xmltech01
8
The Purchase Order Schema, po.xsd IV
</xsd:element> <!– End item specification </xsd:sequence> <!– End sequence for items specification </xsd:complexType> <!– End items specification <!-- Stock Keeping Unit, a code for identifying products --> <xsd:simpleType name="SKU"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{3}-[A-Z]{2}"/> </xsd:restriction> </xsd:simpleType> </xsd:schema> 9/22/2018 it2xmltech01
9
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Comments on po.xsd The prefix xsd: is Namespace specified by xmlns:xsd= The label xsd is conventional; you could use a different one Schema does two types of things: Defines new types of elements or attributes e.g. PurchaseOrderType with xsd:complexType or xsd:simpleType Defines internal elements and attributes for tags e.g. PurchaseOrder using xsd:element and xsd:attribute So an element like shipTo in po.xml has at most one attribute country which if it appears must have value US. The elements name, street, city, state, zip must appear in this order and the first four are strings; zip is a xsd:decimal 9/22/2018 it2xmltech01
10
xsd:complexType Example
Anything of PurchaseOrderType is allowed one attribute (orderDate) of type xsd:date. Further it must consists of 4 elements shipTo, billTo, comment and items in that order comment can be absent as minOccurs = 0 Note that comment is a global element defined in the schema. It is accessed by ref= rather than type= One can define global elements or attributes which cannot themselves use ref (global elements are particularly important if one imports this schema into other schema as only global components can be re-used) minOccurs and maxOccurs have default values of 1 and so shipTo, billTo and items must appear once and once only 9/22/2018 it2xmltech01
11
Specifying xsd:element and xsd:attribute
Any xsd:element tag can have attribute minOccurs, maxOccurs, fixed and default Any xsd:attribute can have attribute use and value Note attributes can NEVER appear more than once - means not present 9/22/2018 it2xmltech01
12
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Simple Types Attributes must be Simple Types; Elements can be complex or simple types Simple types cannot themselves have attributes or contain other elements The xsd:restriction tag allows you to build new simple types by adding constraints to existing simple types. These constraints are specified by a set of “constraining facets” xsd:minInclusive and xsd:maxInclusive restrict <= myInteger <= 99999 xsd:pattern restricts using a regular expression to 3 single digits followed by two capital letters 9/22/2018 it2xmltech01
13
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Constraining Facets These are defined in and are length, minLength, maxLength, pattern, enumeration, whiteSpace, maxInclusive, maxExclusive, minExclusive, minInclusive, precision, scale, encoding, duration, period Tables tell you which constraining facets can be used with which simple type enumeration can be used with all simple types except boolean. In example below, you could define a USState simple type which could take any of conventional abbreviations of the US States 9/22/2018 it2xmltech01
14
Simple Types Built In to XML Schema I
Simple Type Examples (delimited by commas) string Confirm this is electric CDATA Confirm this is electric token Confirm this is electric byte , 126 unsignedByte , 126 binary E617279 integer , -1, 0, 1, positiveInteger , negativeInteger , -1 nonNegativeInteger 0, 1, nonPositiveInteger , -1, 0 int , unsignedInt , long , unsignedLong , short , 12678 unsignedShort , 12678 9/22/2018 it2xmltech01
15
Simple Types Built In to XML Schema II
decimal , 0, 123.4, float INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN double INF, -1E4, -0, 0, 12.78E-2, 12, INF, NaN boolean true, false time :20:00.000, 13:20: :00 timeInstant T13:20: :00 [May 31st 1999 at 1.20pm Eastern Standard Time which is 5 hours behind Coordinated Universal Time] timePeriod T13:20 timeDuration P1Y2M3DT10H30M12.3S [1 year, 2 months, 3 days, 10 hours, 30 minutes, 12.3 seconds] date month [May 1999] year [1999] century [the 1900's] recurringDay [every 31st day] recurringDate [every May 31st] recurringDuration T13:20: [May 31st every year at 1.20pm Coordinated Universal Time, format similar to timeInstant] Red is Note 9/22/2018 it2xmltech01
16
Simple Types Built In to XML Schema III
Simple Type Examples (delimited by commas) Notes in [] Name shipTo [XML 1.0 Name type] QName po:USAddress [XML Namespace QName] NCName USAddress [XML Namespace NCName, i.e. a QName without the prefix and colon] uriReference language en-GB, en-US, fr [valid values for xml:lang as defined in XML 1.0] ID [XML 1.0 ID attribute type] IDREF [XML 1.0 IDREF attribute type] IDREFS [XML 1.0 IDREFS attribute type] ENTITY [XML 1.0 ENTITY attribute type] ENTITIES [XML 1.0 ENTITIES attribute type] NOTATION [XML 1.0 NOTATION attribute type] NMTOKEN US, Brésil [XML 1.0 NMTOKEN attribute type] NMTOKENS US UK, Brésil Canada Mexique [XML 1.0 NMTOKENS attribute type, i.e. a whitespace separated list of NMTOKEN's] 9/22/2018 it2xmltech01
17
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
List Types List types are gotten from Simple types as white space separated atomic types NMTOKENS IDREFS and ENTITIES are built-in list types <listofMyInt> </listofMyInt> Length, minLength, maxLength and enumeration are good facets of list types <sixStates>PA NY CA FL LA AK</sixStates> is an example 9/22/2018 it2xmltech01
18
Anonymous and Union types
Union types define elements or attributes whose value can fall in union of one or more atomic types Anonymous types are declared as for item and quantity in our earlier example without a type attribute (page 7) Examples: <zips>CA</zips> <zips> </zips> Use <xsd:complexType> Or <xsd:simpleType> after an xsd:element tag without a type= attribute 9/22/2018 it2xmltech01
19
Adding Attributes to Simple Types
We used <xsd:element name="USPrice" type="xsd:decimal"/> in po.xsd. Suppose we wish to give this an attribute to specify currency. This is done by deriving a new complex type from the simple type decimal internationalPrice is defined as an anonymous type by With example <internationalPrice currency=“EUR” >123.4 </internationalPrice> 9/22/2018 it2xmltech01
20
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Mixed Content The mixed=“true” attribute in xsd:complexType allows one to specify an XML elelement that meaningfully mixes character data with elements as in example This schema defines two mixed contents elements salutation and letterbody. Note order must be correct and is checked on validation 9/22/2018 it2xmltech01
21
Just Attributes. Empty Content
<internationalPrice currency=“EUR” value=“123.4” /> can be defined in two ways Note use of xsd:anyType in full form as we are defining an element with no content – therefore type irrelevant. By definition anyType puts no constraints on the content model This is equivalent to top form 9/22/2018 it2xmltech01
22
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Annotations Here <xsd:annotation> with sub element <xsd:documentation> is used to explain schema of internationalPrice Can also use xsd:appInfo element as a child of xsd:annotation to provide information to tools and stylesheets <xsd:annotation> can appear in most Schema xsd: elements 9/22/2018 it2xmltech01
23
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Generalized Groups <xsd:sequence> is rather strict as it requires order of elements declared within sequence. Some flexibility is allowed from minOccurs and maxOccurs <xsd:choice> with <xsd:group> and <xsd:all> allow more general structures This allows either a single address <singleUSAddress> or separate shipTo and billTo addresses This is the shipAndBill group referenced with ref= above 9/22/2018 it2xmltech01
24
The <xsd:all> Group
<xsd:all> MUST appear at top-level of any content model and the the elements in the all group must have minOccurs and maxOccurs as 0 or 1 Elements in all group can appear in any order Top of Content Model for PurchaseOrderType Here <xsd:all> is NOT at top 9/22/2018 it2xmltech01
25
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Attribute Groups Often one wishes to use the same attributes in several elements This is done using parameter entities in DTD syntax. Rather than “crude” macro substitution, Schema allows groups to be defined and referenced New Attributes 9/22/2018 it2xmltech01
26
Use of Attribute Groups
Note Attribute declarations and attributeGroup references must appear at end of a complexType definition Reference attributeGroup Define attributeGroup 9/22/2018 it2xmltech01
27
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Null Values As well as setting explicit values for an element, one may wish to indicate that element has NO set value Taking example of shipDate (which was absent for the Lawnmower in po.xml), one indicates this that is allowed in Schema definition by <xsd:element name="shipDate" type="xsd:date" nullable="true"/> And to explicitly represent that shipDate has a null value in the instance document, we set the null attribute (from the XML Schema namespace for instances) to true: <shipDate xsi:null="true"></shipDate> This assumes one has set xmlns:xsi = Such null values MUST not values set in <shipDate ..></shipDate> but can have any allowed attributes 9/22/2018 it2xmltech01
28
Schema and Namespaces I
Schema do a better job than DTD in making it clear who how Namespaces can be used effectively Here we define as the targetNamespace for this schema We also define po: as the same URL and the default Namespace to be W3C Schema central Then we do NOT need xsd: as in original po.xml as we have default Schema central to be default In name=‘..’, we do NOT need to specify a Namespace but in type=“..” or ref=“..” we do not as these could reference another Namespace No Namespace as string type define in Schema Central 9/22/2018 it2xmltech01
29
Schema and Namespaces II
We must distinguish global and local elements The attribute definitions in po1.xml elementFormDefault="unqualified" attributeFormDefault="unqualified“ Imply that all local elements are assumed to come from targetNamespace and MUST not be qualified purchaseOrder and comment are global; remainder local apo: must be used with global elements but not with other ones 9/22/2018 it2xmltech01
30
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
elementFormDefault I We can require local elements to be qualified by setting elementFormDefault as “qualified” Attributes need not be qualified as we left attributeFormDefault as “unqualified” Schema Example of this Schema 9/22/2018 it2xmltech01
31
elementFormDefault II
If we make po1.xml the default Namespace, then whatever setting of elementFormDefault we need no prefixes at all. 9/22/2018 it2xmltech01
32
Selective Qualification
One can set defaults and then override defaults for selected attributes or elements This is illustrated for attribute publicKey here Qualification and its default is to be designed to ensure no ambiguities and mistakes when there are multiple Namespaces 9/22/2018 it2xmltech01
33
Global Elements in Schema
We can make schema where all elements and attributes are global as in this po.xsd This is just the DTD version and all element names must be unique and all must be qualified unless po.xml is declared the default Namespace 9/22/2018 it2xmltech01
34
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
xsd:include Tag Here we include another schema file which itself is a valid schema definition. The net result is a single Namespace referenced through original file One can any number of include statements and arbitrary nesting but there will always be a single Namespace <include schemaLocation= " 9/22/2018 it2xmltech01
35
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Xsd:Include Here is the File to be included The parser will find all the files and construct the total Schema 9/22/2018 it2xmltech01
36
Deriving Types by Extension
Derived Derived Equivalent to Standalone Here the types USAddress and UKAddress are gotten by adding elements and attribute (for UKAddress) to complex type Address ipo is defined as Namespace where UKPostcode and USAddress are defined There is an equivalent standalone way of doing this which is shorter perhaps but as we see on next foil is less powerful in the long run 9/22/2018 it2xmltech01
37
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Using Extended types One can use derived types in an instance of a Schema where Schema specified only the parent To do this you must set the xsi:type attribute for the derived element to be ipo:derived type Here xsi is W3C central XML Schema Instance namespace. ipo is Namespace for this particular schema 9/22/2018 it2xmltech01
38
Restriction of Complex Types
Here we are taking a given complex or simple type and restricting it to a subset of its previous capabilities. In example, we take Items and derive a new type ConfirmedItems which only has one change minOccurs=“1” not “0” for the item subelement Note you must repeat all the unchanged parts of items and item 9/22/2018 it2xmltech01
39
Examples of Restrictions
Valid restrictions must decrease allowed range so that if in original (minOccurs,maxOccurs) = (1,1) one cannot further restrict it The first three examples are in common between attributes ( where one needs to specify use and value) and elements 9/22/2018 it2xmltech01
40
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
<xsd:redefine Tag Here you do not create new elements but rather change an existing element preserving its name Here we change Address This change holds for all derived types based on this changed element So UKAddress also gets a country element 9/22/2018 it2xmltech01
41
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Substitution Groups Consider any global element, such as comment in po.xml. We can define alternative labels for this tag with the substitutionGroup attribute in <xsd:element One can use the new labels to improve readability of an XML instance We define alternatives customerComment and shipComment 9/22/2018 it2xmltech01
42
Abstract Element and Type
If we declare comment as abstract by <element name="comment" type="string" abstract="true"/> Then we cannot use comment but we can use any element defined in a substitutionGroup such as customerComment and shipComment Vehicle is described as an abstract type. One can use conventionally any types like Car or Plane derived from it. Element transport is defined with a type Vehicle that is abstract. Using an element with an abstract type requires setting xsi:type=“XXX” where XXX is a non abstract type derived from original one 9/22/2018 it2xmltech01
43
Restricting what can be changed I
One can control how much freedom there is in extending or restricting simple and complex types by use of the final attribute in the <xsd:element <xsd:complexType or <xsd:simpleType Schema elements This stops derivation of this type later in Schema final=“restriction” prevents new types gotten by restricting original type final=“extension” prevents new types gotten by extending original type final=“#all” prevents restriction and extension In xsd:schema the attribute finalDefault can be used with these same three values to preset values of final in element and type definitions In example, the derivation of USAddress and UKAddress would not be allowed 9/22/2018 it2xmltech01
44
Restricting what can be changed II
We also described how one can use in instance documents, derivations such as USAddress and UKAddress as in shipTo and billTo elements which are defined in the schema to have type Address This capability can be controlled using the block attribute block can take values restriction, extension or substitution (to forbid use of substitution groups) or #all to reflect all of these three capabilities As with final, one can specify blockDefault in the <xsd:schema element to apply a default to block in every type and element definition 9/22/2018 it2xmltech01
45
Changing Facets in Simple Types
One can use the fixed attribute to control restriction of simple types as in this Postcode example [A-Z]{2}\d\s\d[A-Z]{2} is an allowed derived type as length still 7 However changing length or specifying a pattern that is not 7 in length is illegal 9/22/2018 it2xmltech01
46
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Constrained Data This example shows a report where there are some implicit constraints Each Zip code occurs only once Each part can occur in different zip codes but any part occurring in any zip code must occur once and only once in <parts> section These are constraints between different element and attribute values With facets, we constrained individual entries. 9/22/2018 it2xmltech01
47
Report Schema report.xsd I
The <xsd:unique tag with name dummy1 specifies a set of elements – those in regions/zip defined on next page. The <xsd:field tag tells you what has to be unique in this set of elements Namely the code attribute We will do xpath later 9/22/2018 it2xmltech01
48
Report Schema report.xsd II
Here we illustrate a more powerful concept which generalizes ID and IDREF in basic XML to any attribute or element The <xsd:key tag identifies the number part element in parts as a key (generalized ID) to be referenced by pNumKey ( a generalized IDREF) As it is a key, the number attribute in parts/part must be unique as is ID in XML specification The <xsd:keyref tag specifies the number attribute in zip/part has properties of IDREF It is not necessarily unique but must reference a “pNumKey” with its value 9/22/2018 it2xmltech01
49
Report Schema report.xsd III
This is rest of report.xsd We can specify two fields in dummy1 to ensure that pair (zip,part) is unique i.e. for each zip, each part occurs 0 or 1 times 9/22/2018 it2xmltech01
50
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Import directive I In report.xsd we started schema specification <schema targetNamespace=" xmlns=" xmlns:r=" xmlns:xipo=" elementFormDefault="qualified“ > <import namespace=" include (as used in ipo.xsd to include address.xsd) incorporates external schema components into same target namespace as base schema. import allows us to set up “libraries” and use predefined components from different target namespaces 9/22/2018 it2xmltech01
51
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Import directive II Here we see <xsd:include used earlier in ipo.xsd Given we have imported all of ipo.xsd, we can reference any globally defined component Thus we can write in report.xsd <element ref="xipo:comment"/> Note however, that we cannot reuse the shipTo element from po.xsd as only global components can be used. Thus one canNOT write <element ref="xipo:shipTo"/> 9/22/2018 it2xmltech01
52
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Import directive III One can import complex types as long as they are globally defined USAddress can be used from ipo.xsd and in example we extend it to Analyst type which is used to define in obvious way (not shown) an analyst element used as shown When schema components are imported from multiple namespaces, each namespace must be identified with a separate import element, which must appear as the first children of the schema element. One must also associate each namespace with a prefix, using a standard xmlns:prefix namespace declaration. This prefix is used in any references to components in the imported namespace 9/22/2018 it2xmltech01
53
it2xmltech01 http://aspen.csit.fsu.edu/it2spring01
Type Library Here we define a type library and access it to form a new element as shown in this Schema fragment <schema targetNamespace= " xmlns=" xmlns:m=“ > <import namespace=“ <element name="convertFrom" type="m:Currency" > … </schema> In an instance we would use: <convertFrom name="AFA"/>199.37</convertFrom> 9/22/2018 it2xmltech01
54
Use of any Element in Schema
Here we introduce a new element <xsd:any with two special attributes namespace and processContents In this example a single element <htmlexample is allowed but it is allowed to have any tags from the XHTML Namespace processContents is set to skip so that the XHTML must be well formed but is not validated processContents can also be strict to require validation of XHTML fragment or processContents can also be lax when XML parser does its best 9/22/2018 it2xmltech01
55
any Schema Element - Example
This is an example with inserted XHTML. Note use of default Namespace in <table tag so we can avoid any prefixes 9/22/2018 it2xmltech01
56
any Schema Attribute or Element
The namespace attribute in <xsd:any can take several values with ##any ##local ##other and ##targetNamespace having special meaning One can also define <xsd:anyAttribute with the example allowing href to appear in <htmlExample tag 9/22/2018 it2xmltech01
57
schemaLocation and xsi:schemaLocation
One can use in <xsd:schema the attribute xsi:schemaLocation to specify helpful information to parser. The syntax is xsi:schemaLocation=“Namespace1 Hint1 Namespace2 Hint2” The parser is allowed to ignore the hints. For a document without a target namespace use xsi:noNamespaceSchemaLocation In import and include, the schemaLocation attribute (no xsi: prefix) specifies URI for imported/included Schema Example of use of xsi:schemaLocation 9/22/2018 it2xmltech01
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.