Welcome to MSDN Blogs Sign in | Join | Help

Galex's Semi-Structured Blog

SQL and XML in SQL Server 2005
Simple Types

My fellow team members have all blogged on static typing 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 here and Mike's entry here. 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. XQuery 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.

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.

The type system in XQuery is based on XML Schema (XSD). 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. I won't go into the details of XSD, but I suggest those who are not familiar with it read Part 0 of the XSD specification.

Part 2 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)

Atomic types within XQuery are the builtin primitive types (simple types) and the types derived from these primitives by restriction or union. 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:

CREATE XML SCHEMA COLLECTION new_type_collection AS N'
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tn="urn:new_type" targetNamespace="urn:new_type">
    <xs:simpleType name="foo">
        <xs:restriction base="xs:integer">
            <xs:minExclusive value="10"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="fooElement" type="tn:foo"/>
</xs:schema>'

I've also defined an element name fooElement. Assuming I have an XML instance, I can now use this type within a query.

declare @x xml(new_type_collection)
set @x = '<tn:fooElement xmlns:tn="urn:new_type">11</tn:fooElement>'
select @x.query('declare namespace tn="urn:new_type"; tn:foo("12") > /tn:fooElement')

Notice that I created 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:

declare @x xml(new_type_collection)
set @x = '<tn:fooElement xmlns:tn="urn:new_type">11</tn:fooElement>'
select @x.query('declare namespace tn="urn:new_type"; xs:integer("0") > /tn:fooElement')

Here, I've created an instance of xs:integer with the value 0 and compared it to the stored instance.

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 Definitive XML Schema by Priscilla Walmsley. Here is a diagram of the builtin type hierarchy.

Next time, I will talk about union and list types and their properties within XQuery.

Posted: Thursday, June 16, 2005 3:05 PM by galexy
Filed under: ,

Comments

No Comments

New Comments to this post are disabled
Page view tracker