Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMP 051 XML Introduction Session IV Chapter 10 – Defining Simple Types

Similar presentations


Presentation on theme: "CMP 051 XML Introduction Session IV Chapter 10 – Defining Simple Types"— Presentation transcript:

1 CMP 051 XML Introduction Session IV Chapter 10 – Defining Simple Types

2 Outline XML Simple Element Types Defining a Simple Element Type
Using Date and Time Types Using Number Types Predefining an Element Content Deriving Custom Simple Types Deriving Named Custom Types Specifying a Range of Acceptable Values Specifying a Set of Acceptable Values Limiting the Length of an Element Specifying the Pattern for an Element Limiting a Number’s Digits Deriving a List Type Deriving a Union Type 8/1/2014 Copyright © Carl M. Burnett

3 XML Simple Element Types
XML simple element contains only text. Cannot contain any other elements or attributes. Most common built-in data types: xs:string xs:decimal xs:integer xs:boolean xs:date xs:time 8/1/2014 Copyright © Carl M. Burnett

4 Defining a Simple Element Type
Type <xs:element to begin the definition. Type name-”label” where label is the name of the XML element that you are defining. Type types=“ Type the following to identify the simple data types: xs:string for string of characters. xs:decimal for decimal number. xs:boolean for true / false. xs:date for a date. xs:time for time. xs:anyURI for a reference to a file. Type > to complete the definition. <xs:element name="height" type="xs:string"/> <xs:element name="year_built" type="xs:integer"/> 8/1/2014 Copyright © Carl M. Burnett

5 Using Date and Time Types
Syntax Format xs:date YYYY-MM-DD xs:time hh:mm:ss xs:dateTime YYYY:MM:DDThh:mm:ss xs:duration PnYnMnDTnHnMnS Where: P stands for Period T only required for time units Y – Years M – Months D – Days H – Hours M – Minutes S – Seconds Syntax Format xs:gYear YYYY xs:gYearMonth YYYY-MM xs:gMonth --MM xs:gMonthDay --MM—DD xs:gDay --DD 8/1/2014 Copyright © Carl M. Burnett

6 Using Number Types Syntax Format Syntax Format A signed 8-bit integer
xs:byte A signed 8-bit integer xs:decimal 4.26, -100, or 0 xs:int A signed 32-bit integer xs:integer 542 or -7 xs:positiveInteger 1 , 2 xs:negativeInteger 0, -1,-2 xs:nonPositiveInteger 0,-1,-2 xs:nonNegativeInteger 0,1,2 Syntax Format xs:float 32-bit floating point numbers xs:short 32-bit integer xs:unsignedLong An unsigned 64-bit integer xs:unsignedInt An unsigned 32-bit integer xs:unsignedShort xs:unsignedByte 8/1/2014 Copyright © Carl M. Burnett

7 Predefining an Element Content
To Set an Element’s Value <xs:element name="how_destroyed" type="xs:string" fixed="fire“ /> Within the element tag type fixed=. Type ”value” where value is what the element must be equal to in order to be considered valid. To Set an Element’s Default Value Within the element tag type default=. Type ”value” where value is what the element will equal to if empty or omitted. <xs:element name="how_destroyed" type="xs:string" default="fire“ /> 8/1/2014 Copyright © Carl M. Burnett

8 Deriving Custom Simple Types
Identify the name of the XML element that you are using an XML Schema to define. To do so type <xs:element name=“label”> where label is the name of the XML element. Type <xs:simpleType> to start deriving your custom simple type. Type <xs:restriction base=“foundation”> where foundation is any of the built-in simple types you want to base your custom type. Specify as many restrictions (or facets) as necessary to define your new custom type. Type </xs:restriction> Type </xs:simpleType> Type </xs:element> to complete the definition of the element. <?xml version="1.0"?> <xs:schema xmlns:xs=" <xs:element name="simple_types"> <xs:complexType> <xs:sequence> <xs:element name="story"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="1024"/> </xs:restriction> </xs:simpleType> </xs:element> </xs:sequence> </xs:complexType> </xs:schema> 8/1/2014 Copyright © Carl M. Burnett

9 Deriving Named Custom Types
Type <xs:simpleType to start the simple custom type. Type <name=“custom_type_name”> where custom_type_name identifies your new custom simple type. Type <xs:restriction base=“foundation”> where foundation is the simple type upon which you are building your custom type. Specify as many restrictions (or facets) as necessary to define your new custom type. Type </xs:restriction> Type </xs:simpleType> To use your new custom type <xs:element name=label type=“custom_type_name”> using custom_type_name you gave your new custom type. <xs:simpleType name="story_type"> <xs:restriction base="xs:string"> <xs:length value="1024"/> </xs:restriction> </xs:simpleType> <xs:element name="story" type="story_type"/> <xs:element name="summary" type="story_type"/> <xs:element name="another_story" type="story_type"/> 8/1/2014 Copyright © Carl M. Burnett

10 Specifying a Range of Acceptable Values
To specify the highest possible value <xs:element name="total_bases"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:maxInclusive value="6856"/> </xs:restriction> </xs:simpleType> </xs:element> Within a custom type definition type <xs:maxInclusive Then, type value="n“ , where the element’s content must be less than or equal to n in order to be valid. Finally, type /> to complete the xs:maxInclusive facet. Another way to specify the highest possible value 1. Within a custom type definition type <xs:maxExclusive 2. Then, type value="n", where the element’s content must be less than (but not equal to) n in order to be valid. 3. Finally, type /> to complete the xs:maxExclusive facet. <xs:element name="total_bases"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:maxEnclusive value="6856"/> </xs:restriction> </xs:simpleType> </xs:element> 8/1/2014 Copyright © Carl M. Burnett

11 Specifying a Range of Acceptable Values
To specify the lowest possible value <xs:element name=“game_day"> <xs:simpleType> <xs:restriction base="xs:date"> <xs:minInclusive value=“ "/> </xs:restriction> </xs:simpleType> </xs:element> Within a custom type definition type <xs:mimInclusive Then, type value="n“ , where the element’s content must be less than or equal to n in order to be valid. Finally, type /> to complete the xs:minInclusive facet. Another way to specify the lowest possible value <xs:element name=“game_day"> <xs:simpleType> <xs:restriction base="xs:date"> <xs:minInclusive value=“ "/> <xs:maxInclusive value=“ "/> </xs:restriction> </xs:simpleType> </xs:element> 1. Within a custom type definition type <xs:minExclusive 2. Then, type value="n", where the element’s content must be less than (but not equal to) n in order to be valid. 3. Finally, type /> to complete the xs:minExclusive facet. 8/1/2014 Copyright © Carl M. Burnett

12 Specifying a Set of Acceptable Values
<xs:element name="wonder_name"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value= "Colossus of Rhodes"/> "Great Pyramid of Giza"/> "Hanging Gardens of Babylon"/> "Statue of Zeus at Olympia"/> "Temple of Artemis at Ephesus"/> "Mausoleum at Halicarnassus"/> "Lighthouse of Alexandria"/> </xs:restriction> </xs:simpleType> </xs:element> 1. Within a custom type definition , type <xs:enumeration. 2. Then, type value="choice", to identify one acceptable choice for the content of the element or attribute. 3. Finally, type /> to complete the xs:enumeration element. 4. Repeat Steps 1–3 for each addition 8/1/2014 Copyright © Carl M. Burnett

13 Limiting the Length of an Element
To specify the exact length of an element <xs:element name="wonder_code"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="5"/> </xs:restriction> </xs:simpleType> </xs:element> Within a custom type definition type <xs:length value="g"/>, where g is the number of characters that the element must have. To specify the Minimum length of an element Within a custom type definition type <xs:minLength value=“n"/>, where n is the minimum length of characters that the element must have. To specify the Maximum length of an element <xs:element name="brief_description"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:maxLength value="256"/> </xs:restriction> </xs:simpleType> </xs:element> Within a custom type definition type <xs:maxLength value=“x"/>, where x is the maximum length of characters that the element must have. 8/1/2014 Copyright © Carl M. Burnett

14 Specifying the Pattern for an Element
Within a custom type definition type <xs:pattern. Then, type value="regex", where regex is the regular expression that the XML element’s content must match. Regular expressions are made up of letters, numbers, and special symbols; in the order which those letters, numbers, and symbols should appear in the content. Symbols include: . (a period) for any character at all. \d for any digit; \D for any non-digit. \s for any white space (including space, tab, newline, and return); \S for any character that is not white space. x* to have zero or more x’s; (xy)* to have zero or more xy’s. x? to have zero or one x; (xy)? to have zero or one xy. x+ to have one or more x’s; (xy)+ to have one or more xy’s. [abc] to include one of a group of values (a, b, or c). [0–9] to include the range of values from 0 to 9. <xs:element name="wonder_code"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="w_\d{3}"/> </xs:restriction> </xs:simpleType> </xs:element> 8/1/2014 Copyright © Carl M. Burnett

15 Specifying the Pattern for an Element
Within a custom type definition type <xs:pattern. Then, type value="regex", where regex is the regular expression that the XML element’s content must match. Regular expressions are made up of letters, numbers, and special symbols; in the order which those letters, numbers, and symbols should appear in the content. 3. Finally, type /> to complete the xs:pattern element. Symbols include: . (a period) for any character at all. \d for any digit; \D for any non-digit. \s for any white space (including space, tab, newline, and return); \S for any character that is not white space. x* to have zero or more x’s; (xy)* to have zero or more xy’s. x? to have zero or one x; (xy)? to have zero or one xy. x+ to have one or more x’s; (xy)+ to have one or more xy’s. [abc] to include one of a group of values (a, b, or c). [0–9] to include the range of values from 0 to 9. this | that to have this or that in the content. Separate additional choices with additional vertical bars. x{5} to have exactly 5 x’s (in a row). x{5,} to have at least 5 x’s (in a row). x{5,8} to have at least 5 and at most 8 x’s (in a row). (xyz){2} to have exactly two xyz’s (in a row). Note: Parentheses control what the curly brackets and other modifiers, such as ?, +, and *, affect. 8/1/2014 Copyright © Carl M. Burnett

16 Specifying the Pattern for an Element
<xs:element name="wonder_code"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="w_\d{3}"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="race_time"> <xs:simpleType> <xs:restriction base="xs:duration"> <xs:pattern value="PT\d+H\d+M\d+S"/> </xs:restriction> </xs:simpleType> </xs:element> 8/1/2014 Copyright © Carl M. Burnett

17 Limiting a Number’s Digits
To specify the total number of digits in a number 1. Within a custom type definition type <xs:totalDigits. 2. Then, type value="n", where n is the maximum number of digits that can appear in the number. 3. Finally, type /> to complete the xs:totalDigits facet. <xs:element name="atomic_weight"> <xs:simpleType> <xs:restriction base="xs:decimal"> <xs:totalDigits value="6"/> <xs:fractionDigits value="4"/> </xs:restriction> </xs:simpleType> <xs:element> To specify the number of digits after the decimal point 1. Within a custom type definition type <xs:fractionDigits. 2. Then, type value="n", where n is the maximum number of digits that can appear after the decimal in the number. 3. Finally, type /> to complete the xs:fractionDigits facet. 8/1/2014 Copyright © Carl M. Burnett

18 Deriving a List Type First, identify the name of the XML element that you are using XML Schema to define. Type <xs:element name="label">, where label is the name of the XML element. Type <xs:simpleType> to start deriving your custom simple type. Type <xs:list itemType="list_element"/>, where list_element is the simple type (built in or custom) that defines each individual unit in your list. Type </xs:simpleType> to complete your new custom simple type. Type </xs:element> to complete the definition of the element. <xs:element name="recent_eclipses"> <xs:simpleType> <xs:list itemType="xs:dateTime"/> </xs:simpleType> </xs:element> 8/1/2014 Copyright © Carl M. Burnett

19 Deriving a Union Type First, identify the name of the XML element that you are using XML Schema to define. Type <xs:element name="label">, where label is the name of the XML element. Type <xs:simpleType> to start your custom simple type. Type <xs:union memberTypes="union_elements"/>, where union_elements is a white-space-separated group of simple types (built-in or custom) that define the valid simple types for this element. Type </xs:simpleType> to complete your new custom simple type. Type </xs:element> to complete the definition of the element. <xs:simpleType name="isbn10"> <xs:restriction base="xs:string"> <xs:pattern value="\d{9}[\d|X]"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="isbn13"> <xs:pattern value="\d{3}-\d{10}"/> <xs:element name="book"> <xs:simpleType> <xs:union memberTypes="isbn10 isbn13"/> </xs:simpleType> </xs:element> 8/1/2014 Copyright © Carl M. Burnett

20 Review XML Simple Element Types Defining a Simple Element Type
Using Date and Time Types Using Number Types Predefining an Element Content Deriving Custom Simple Types Deriving Named Custom Types Specifying a Range of Acceptable Values Specifying a Set of Acceptable Values Limiting the Length of an Element Specifying the Pattern for an Element Limiting a Number’s Digits Deriving a List Type Deriving a Union Type Next: Chapter 11 – Complex Types 8/1/2014 Copyright © Carl M. Burnett


Download ppt "CMP 051 XML Introduction Session IV Chapter 10 – Defining Simple Types"

Similar presentations


Ads by Google