<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Galex's Semi-Structured Blog : Xml Schema</title><link>http://blogs.msdn.com/galexy/archive/tags/Xml+Schema/default.aspx</link><description>Tags: Xml Schema</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Union and Lists</title><link>http://blogs.msdn.com/galexy/archive/2005/08/10/430902.aspx</link><pubDate>Wed, 10 Aug 2005 21:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:430902</guid><dc:creator>galexy</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/galexy/comments/430902.aspx</comments><wfw:commentRss>http://blogs.msdn.com/galexy/commentrss.aspx?PostID=430902</wfw:commentRss><description>&lt;P&gt;Last time we talked about built-in simple types and how additional types can be defined as restrictions of those types. Now, we will talk about the remaining kinds of simple types: unions and lists. &lt;/P&gt;
&lt;P&gt;According to the W3C spec, list types are data types where the legal values are&amp;nbsp;finite-length sequence of values. Each list type can be a list of a specific itemType. Within SQL Server 2005, we do not permit the creation of list of lists, list of union types and list of xs:ID. List types can be derived from the built-in atomic types, user defined restricted atomic types or anonymous types. The lexical representation of instances of list types are space delimited sequences of instances of the itemType. That means itemTypes that require spaces will not validate correctly when used within a list. For example, if I had a simple type that was a enumeration of three possible values "foo bar", "foo baz" and "bar baz", I would not be able to define a list of this type. When the validator runs across the instance "foo bar foo baz bar baz", it will try to validate "foo", "bar", "foo", "baz", "bar" and "baz" as instances of the itemType; all of which will fail. List types&amp;nbsp;are XSD's intrinsic collection type. Although other mechanisms in XSD can be used to form collections of items, the list type may be more suitable for your needs. In additional to being able to create list types from atomic types, new types can derived from list types that restrict&amp;nbsp;the list further by&amp;nbsp;length, enumeration and pattern facets.&lt;/P&gt;
&lt;P&gt;Union types are defined as types where the value and lexical space is the union of the value and lexical spaces of one or more simple types. You could create a union type that was the union of xs:duration and xs:time. During validation of an instance of this type, the lexical value will be validated against xs:duration first and if it doesn't pass validation, it will be validated against xs:time. However, if you want to have an instance validate against a specific memberType, you can specify that within the instance element via the xsi:type attribute. XSD unions&amp;nbsp;are similar to C unions but have subtle differences. For one thing, the actual type of the value is not defined by a type descriptor. During validation, the PSVI is annotated with the actual type of the value.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;CREATE XML SCHEMA COLLECTION foo AS N'&lt;BR&gt;&amp;lt;xs:schema xmlns:xs="&lt;A href="http://www.w3.org/2001/XMLSchema"&gt;http://www.w3.org/2001/XMLSchema&lt;/A&gt;" xmlns:tn="urn:test" targetNamespace="urn:test"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;lt;xs:simpleType name="myAtomicType"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;lt;xs:restriction base="xs:string"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;xs:pattern value="(ab)*"/&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;lt;/xs:restriction&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;lt;/xs:simpleType&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;lt;xs:simpleType name="myList"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;lt;xs:list itemType="tn:myAtomicType"/&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;lt;/xs:simpleType&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;lt;xs:simpleType name="myAnonymousList"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;lt;xs:list&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;xs:simpleType&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;xs:restriction base="xs:int"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;xs:totalDigits value="8"/&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/xs:restriction&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;lt;/xs:simpleType&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;lt;/xs:list&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;lt;/xs:simpleType&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;lt;xs:simpleType name="myUnion"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;lt;xs:union memberTypes="tn:myAnonymousList tn:myAtomicType xs:float"/&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;lt;/xs:simpleType&amp;gt;&lt;BR&gt;&amp;lt;/xs:schema&amp;gt;'&lt;/P&gt;
&lt;P&gt;I've created two list types, one that uses a type I've created, another using an anonymous type defined only within its scope. I've also created a union type that is a union of a list type, a restricted type and a builtin type.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=430902" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/galexy/archive/tags/Xml+Schema/default.aspx">Xml Schema</category></item><item><title>Simple Types</title><link>http://blogs.msdn.com/galexy/archive/2005/06/16/429874.aspx</link><pubDate>Fri, 17 Jun 2005 01:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:429874</guid><dc:creator>galexy</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/galexy/comments/429874.aspx</comments><wfw:commentRss>http://blogs.msdn.com/galexy/commentrss.aspx?PostID=429874</wfw:commentRss><description>&lt;P&gt;My fellow team members have all blogged on &lt;EM&gt;static typing&lt;/EM&gt; and anyone reading these blogs would be correct to conjecture that it is pretty important. There is so much to say about it and I'd rather not duplicate what my team members have written. For an introduction, read Denis' entry &lt;a href="http://blogs.msdn.com/denisruc/archive/2005/06/13/428726.aspx"&gt;here&lt;/A&gt;&amp;nbsp;and Mike's entry &lt;a href="http://blogs.msdn.com/mrorke/archive/2005/04/18/409295.aspx"&gt;here&lt;/A&gt;. After reading these posts, it should be apparent that the point of static typing is to aid the developer in writing correct queries. Static typing is often used in other languages such as C# which are strongly typed. &lt;A href="http://www.w3.org/XML/Query"&gt;XQuery&lt;/A&gt;&amp;nbsp;is a strongly typed language and implementations have the option of providing static type checking at query compile time. The XQuery implementation in SQL Server 2005 does in fact perform static type checking.&lt;/P&gt;
&lt;P&gt;Before going into static typing, it is important to understand types within XML: where they come from, how they are defined and what can they "look" like. I will spend the next few posts talking just about types.&lt;/P&gt;
&lt;P&gt;The type system in XQuery is based on &lt;A href="http://www.w3.org/XML/Schema"&gt;XML Schema (XSD)&lt;/A&gt;. If you don't plan on using XSD validation in your XML, you might think there is no point in learning more about types. However, static typing is still in affect when quering untyped XML within XQuery. The difference between typed and untyped instances is that the former is validated against an XSD collection.&amp;nbsp;I won't go into the details of XSD, but I suggest those who are not familiar with it read &lt;A href="http://www.w3.org/TR/xmlschema-0/"&gt;Part 0&lt;/A&gt; of the XSD specification.&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.w3.org/TR/xmlschema-2/"&gt;Part 2&lt;/A&gt; of the XSD specification talks about primitive and builtin types. XQuery inherits these builtin types into its own type system. Along with simple and complex types from XSD, XQuery defines node types. XQuery also has the notion of a sequence of items. Items are either atomic values or nodes. (Don't worry, these will become clear later)&lt;/P&gt;
&lt;P&gt;Atomic types within XQuery are the builtin primitive types (simple types) and the types derived from these primitives by restriction &lt;STRIKE&gt;or union&lt;/STRIKE&gt;. XQuery defines a few extra simple types: xdt:untypedAtomic, xdt:anyAtomicType, xdt:yearMonthDuration and xdt:dayTimeDuration (SQL Server 2005 does not support the last two types). We will discuss these types in the future. Like XSD, the builtin types just exist and the user has the ability to introduce new types. In SQL Server 2005 we do this by creating an XSD schema collection. To create new atomic simple types, we have to restrict an existing atomic simple type. For example, if I wanted to create a type named foo that is an integer greater than 10, I would execute this DDL:&lt;/P&gt;
&lt;P&gt;CREATE XML SCHEMA COLLECTION new_type_collection AS N'&lt;BR&gt;&amp;lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tn="urn:new_type" targetNamespace="urn:new_type"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;xs:simpleType name="foo"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;xs:restriction base="xs:integer"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;xs:minExclusive value="10"/&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/xs:restriction&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/xs:simpleType&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;xs:element name="fooElement" type="tn:foo"/&amp;gt;&lt;BR&gt;&amp;lt;/xs:schema&amp;gt;'&lt;/P&gt;
&lt;P&gt;I've also defined an element name fooElement. Assuming I have an XML instance, I can now use this type within a query.&lt;/P&gt;
&lt;P&gt;declare @x xml(new_type_collection)&lt;BR&gt;set @x = '&amp;lt;tn:fooElement xmlns:tn="urn:new_type"&amp;gt;11&amp;lt;/tn:fooElement&amp;gt;'&lt;BR&gt;select @x.query('declare namespace tn="urn:new_type"; tn:foo("12") &amp;gt; /tn:fooElement')&lt;/P&gt;
&lt;P&gt;Notice that I &lt;EM&gt;created &lt;/EM&gt;an instance of the type foo by using a constructor syntax. What I have done is created an instance with the numeric value of 12 and then compare it to the value stored within the instance I am querying. In this case, the result is true. I can also create instances of builtin types with the constructor syntax:&lt;/P&gt;
&lt;P&gt;declare @x xml(new_type_collection)&lt;BR&gt;set @x = '&amp;lt;tn:fooElement xmlns:tn="urn:new_type"&amp;gt;11&amp;lt;/tn:fooElement&amp;gt;'&lt;BR&gt;select @x.query('declare namespace tn="urn:new_type"; xs:integer("0") &amp;gt; /tn:fooElement')&lt;/P&gt;
&lt;P&gt;Here, I've created an instance of xs:integer with the value 0 and compared it to the stored instance. &lt;/P&gt;
&lt;P&gt;Okay, so now you know what builtin types are, how to create new restricted types from them via an XML Schema and how to create instances of these simple types within a query. The builtin simple types consist of string, date/time, floating point, decimal, integer, binary and various other types. To learn more about them, their semantics, restrictions and valid value and lexical spaces, read Part 2 of the XSD Specification. If you prefer to read a book try &lt;EM&gt;Definitive XML Schema&lt;/EM&gt; by Priscilla Walmsley. Here is a &lt;A href="http://www.w3.org/TR/xpath-functions/#datatypes"&gt;diagram&lt;/A&gt; of the builtin type hierarchy.&lt;/P&gt;
&lt;P&gt;Next time, I will talk about union and list types and their properties within XQuery.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=429874" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/galexy/archive/tags/Xml+Schema/default.aspx">Xml Schema</category><category domain="http://blogs.msdn.com/galexy/archive/tags/XQuery/default.aspx">XQuery</category></item></channel></rss>