Download presentation
Presentation is loading. Please wait.
1
X-Informatics: I-400 and I-590 XML Schema
Spring Semester MW 6:00 pm – 7:15 pm Indiana Time Geoffrey Fox and Bryan Carpenter PTLIU Laboratory for Community Grids Informatics, (Computer Science , Physics) Indiana University Bloomington IN 47404 12/9/2018 xmlschemafall01
2
Outline XML Schema discussion based on Note that this is reasonably complete – parts 1 and 2 ( etc.) are more formal discussions not extensions in coverage See (elementary) and note very complete tutorial and examples at We summarize all the topics in this primer Basic Schema Groups ** REMOVED Namespaces Include ** REMOVED Derived types ** REMOVED Import ** REMOVED Remember: Schema and DTD are like classes in Java XML files are instances of objects defined in Schema or DTD’s 12/9/2018 xmlschemafall01
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 (namelist) to associate schema with this XML instance 12/9/2018 xmlschemafall01
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> 12/9/2018 xmlschemafall01
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> 12/9/2018 xmlschemafall01
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> 12/9/2018 xmlschemafall01
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 12/9/2018 xmlschemafall01
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> 12/9/2018 xmlschemafall01
9
Comments on po.xsd I 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 12/9/2018 xmlschemafall01
10
Comments on po.xsd II The prefix xsd: is Namespace specified by xmlns:xsd= The label xsd is conventional; you could use a different one 12/9/2018 xmlschemafall01
11
Comments on po.xsd III Schema does two types of things:
Defines new types of elements or attributes e.g. PurchaseOrderType with xsd:complexType Defines internal elements and attributes for tags e.g. PurchaseOrder using xsd:element and xsd:attribute 12/9/2018 xmlschemafall01
12
Comments on po.xsd IV 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 12/9/2018 xmlschemafall01
13
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 12/9/2018 xmlschemafall01
14
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 12/9/2018 xmlschemafall01
15
Simple Types Attributes must be Simple Types; Elements can be complex types 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 12/9/2018 xmlschemafall01
16
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 12/9/2018 xmlschemafall01
17
Simple Types Built In to XML Schema I
Simple Type Examples (delimited by commas) Red is Note string Confirm this is electric CDATA Confirm this is electric (white space(tabs) to blanks etc.) token Confirm this is electric (trailing/leading white space removed) 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 12/9/2018 xmlschemafall01
18
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 12/9/2018 xmlschemafall01
19
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] 12/9/2018 xmlschemafall01
20
Schema and Namespaces Schema do a better job than DTD in making it clear 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 set 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 12/9/2018 xmlschemafall01
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.