xml simple types cspp51038 shortcourse. simple types recall that simple types are composed of...
Post on 20-Dec-2015
228 views
TRANSCRIPT
XML Simple Types
CSPP51038 shortcourse
Simple Types• Recall that simple types are composed of text-only
values.
• All attributes are of simple type
• Elements with text and no attributes are of simple type
• Careful: Elements with text and attributes are said to have simple content
Types• 44 built-in simple types that are defined in
the XML schema namespace
• For your homework, you must use reasonably restrictive types in your schema
• Following slides borrowed from– http://www.xfront.com/#tutorials– Copyright © [2002]. Roger L. Costello. All Rights Reserved.
Built-in Datatypes• Primitive Datatypes
– string
– boolean
– decimal
– float
– double
– duration
– dateTime
– time
– date
– gYearMonth
– gYear
– gMonthDay
• Atomic, built-in
– "Hello World"
– {true, false, 1, 0}
– 7.08
– 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN
– 12.56E3, 12, 12560, 0, -0, INF, -INF, NAN
– P1Y2M3DT10H30M12.3S– format: CCYY-MM-DDThh:mm:ss
– format: hh:mm:ss.sss
– format: CCYY-MM-DD
– format: CCYY-MM
– format: CCYY
– format: --MM-DD
Note: 'T' is the date/time separator INF = infinity NAN = not-a-number
Copyright © [2002]. Roger L. Costello. All Rights Reserved.
Built-in Datatypes (cont.)• Primitive Datatypes
– gDay
– gMonth
– hexBinary
– base64Binary
– anyURI
– QName
– NOTATION
• Atomic, built-in– format: ---DD (note the 3 dashes)
– format: --MM--
– a hex string
– a base64 string
– http://www.xfront.com
– a namespace qualified name
– a NOTATION from the XML spec
Copyright © [2002]. Roger L. Costello. All Rights Reserved.
Built-in Datatypes (cont.)• Derived types
– normalizedString
– token
– language
– IDREFS
– ENTITIES
– NMTOKEN
– NMTOKENS
– Name
– NCName
– ID
– IDREF
– ENTITY
– integer
– nonPositiveInteger
• Subtype of primitive datatype– A string without tabs, line feeds, or carriage returns
– String w/o tabs, l/f, leading/trailing spaces, consecutive spaces
– any valid xml:lang value, e.g., EN, FR, ...
– must be used only with attributes
– must be used only with attributes
– must be used only with attributes
– must be used only with attributes
– part (no namespace qualifier)
– must be used only with attributes
– must be used only with attributes
– must be used only with attributes
– 456
– negative infinity to 0
Copyright © [2002]. Roger L. Costello. All Rights Reserved.
Built-in Datatypes (cont.)
• Derived types
– negativeInteger
– long
– int
– short
– byte
– nonNegativeInteger
– unsignedLong
– unsignedInt
– unsignedShort
– unsignedByte
– positiveInteger
• Subtype of primitive datatype
– negative infinity to -1
– -9223372036854775808 to 9223372036854775807
– -2147483648 to 2147483647
– -32768 to 32767
– -127 to 128
– 0 to infinity
– 0 to 18446744073709551615
– 0 to 4294967295
– 0 to 65535– 0 to 255
– 1 to infinity
Note: the following types can only be used with attributes (which we will discuss later): ID, IDREF, IDREFS, NMTOKEN, NMTOKENS, ENTITY, and ENTITIES.
Copyright © [2002]. Roger L. Costello. All Rights Reserved.
Defining New Types• Can define new simple types built off of
existing types.
• Types are derived from other, more general types.
• Type hierarchy:http://www.w3.org/TR/xmlschema-2/#built-in-datatypes
Simple Type
• Defines new simple types.– Character data– No children (i.e., no subelements)– No attributes.
• Can create new simple types by:– Restriction– List– Union
Derivation by Restriction• 12 facets that describe possible restrictions on a
built-in data type– length, minLength, maxLength– pattern
• Uses regular expression language.
– enumeration– whiteSpace– minInclusive, maxInclusive– minExclusive, maxExclusive– totalDigits fractionDigits
• Not all facets apply to all built-in data types
Restriction syntax
<xsd:simpleType name=“typeName”><xsd:restriction base=“builtIn_or_userDef”>
<facet1 value=“Value”/><facet1 value=“Value”/><facet1 value=“Value”/><!-- etc. -->
</xsd:restriction></xsd:simpleType>
List and Union syntax
<xsd:simpleType name=“typeName”><xsd:list itemType=“simpleType”>
</xsd:simpleType>
<xsd:simpleType name=“typeName”><xsd:union memberTypes=“simpleType1
simpleType2 ...”></xsd:simpleType>
Restriction examples
<!– restricting upper and lower value of int --><xs:simpleType name=“myInteger”> <xs:restriction base=“xs:integer”> <xs:minInclusive value=“-2”/> <xs:maxInclusive value=“5/> </xs:restriction></xs:simpleType>
<!– enumerations of car names--><xs:simpleType name = “cars”> <xs:restriction base=“xs:string”> <xs:enumeration value=“pinto”/> <xs:enumeration value=“gremlin”/> <xs:enumeration value=“pacer”/> </xs:restriction></xs:simpleType>
Examples<xs:simpleType name=“CapitalizedNameWS”> <xs:restriction base=“xs:token”> <xs:pattern value=([A-Z]([a-z]*) ?)+”/> </xs:restriction></xs:simpleType>
<!– disable scientific notation--> <xs:simpleType name=“nonScientific”/> <xs:restriction base=“xs:float”> <xs:pattern value=“[^eE]*”/> </xs:restriction></xs:simpleType>
Examples
<!– a list of exactly three strings-->
<xs:simpleType name="testList"> <xs:list itemType="xs:int"/></xs:simpleType>
<xs:simpleType name="constrList" final="list"> <xs:restriction base="testList"> <xs:maxLength value="3"/> <xs:minLength value="3"/> </xs:restriction></xs:simpleType>
Union
<!– a list of variable elements of fixed length--><xs:simpleType name="testUnion“> <xs:union memberTypes="xs:string xs:int xs:boolean"/></xs:simpleType>
<xs:simpleType name="unionList"> <xs:list itemType="testUnion"/></xs:simpleType>
<xs:simpleType name=“fixedUnionList”> <xs:restriction base=“unionList”> <xs:maxLength=3/> </xs:restriction></xs:simpleType>
Examples
<!– example two facets together--><xs:simpleType name="myString"> <xs:restriction base="xs:string"> <xs:length value="3"/> <xs:whiteSpace value="collapse"/> </xs:restriction></xs:simpleType>
“Fixed” attribute
<!– disallows modification of minInclusive facet during derivation/><xs:simpleType name=”minInclusive"> <xs:restriction base="xs:float"> <xs:minInclusive value=“10” fixed=“true”/> <xs:whiteSpace value="collapse"/> </xs:restriction></xs:simpleType>
Exercises1. Create a new simple type that is a list of exactly three xs:token values each of which is
between 9 and 10 characters long.
2. Create a new simple type that is a list of 10-20 elements of each member of which can be either an xs:int greater than zero or an xs:date
• Syntax reminder for restriction, list, and union:
<xsd:simpleType name=“typeName”><xsd:restriction base=“builtIn_or_userDef”>
<facet1 value=“Value”/><facet1 value=“Value”/><facet1 value=“Value”/><!-- etc. -->
</xsd:restriction></xsd:simpleType>
<xsd:simpleType name=“typeName”><xsd:list itemType=“simpleType”>
</xsd:simpleType>
<xsd:simpleType name=“typeName”><xsd:union memberTypes=“simpleType1 simpleType2 ...”>
</xsd:simpleType>
Answer 1 <xs:simpleType name="foo1"> <xs:restriction base="xs:token"> <xs:maxLength value="10"> </xs:maxLength> <xs:minLength value="9"> </xs:minLength> </xs:restriction> </xs:simpleType> <xs:simpleType name="foo2"> <xs:list itemType="foo1"> </xs:list> </xs:simpleType>
<xs:simpleType name="foo3"> <xs:restriction base="foo2"> <xs:length value="3"> </xs:length> </xs:restriction> </xs:simpleType>
Answer 2<xs:simpleType name="posInt"> <xs:restriction base="xs:integer"> <xs:minInclusive value="1"/> </xs:restriction> </xs:simpleType>
<xs:simpleType name="listofUnions"> <xs:list> <xs:simpleType> <xs:union memberTypes="posInt xs:date"/> </xs:simpleType> </xs:list> </xs:simpleType>
<xs:simpleType name="restrictedListofUnions"> <xs:restriction base="listofUnions"> <xs:minLength value="10"/> <xs:maxLength value="20"/> </xs:restriction> </xs:simpleType>