XML IN THE DATABASE: THE XML DATA TYPE
XML IN THE DATABASE: THE XML DATA TYPE
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
XML IN THE DATABASE: THE XML DATA TYPE
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
TYPED AND UNTYPED XML Listing 8-5: A Simple XML Schema < ! - defines types for the namespace http://example.org/People This is known as the targetNamespace but does not indicate The location of the schema document >
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
XML IN THE DATABASE: THE XML DATA TYPE Built-in Numeric Types double float decimal integer longnonPositiveInteger intnegativeInteger unsignedLong short unsignedInt unsignedShort unsignedByte nonNegativeInteger positiveInteger byte Numeric Cluster Built-in XML Types anyURI QName NOTATION string normalizedString NMTOKEN NMTOKENS token language Name NCName ENTITY ENTITIES XML Intrinsics Cluster IDREF IDREFS Figure 8-1: XSD Base Data Types (continued ) ID
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services
TYPED AND UNTYPED XML XML schemas define a series of data types that exist in a particular namespace. Schemas are themselves well-formed schema-compliant XML documents, just as relational table definitions and other DDL are valid Transact-SQL. Although there can be more than one schema definition document for a particular namespace, a schema definition document defines types in only one namespace. The XML Schema Definition language (XSD) defines a standard set of base types that are supported as types in XML documents, just as the SQL:1999 standard defines a set of base types that relational databases must support. The XSD base data types are shown in Figure 8-1. Schema documents may also import types from other namespaces, using the importelement. There are also some special namespaces that are imported by default in SQL Server s XML type. Two of the most important ones are: The http://www.w3.org/2001/XMLSchema namespace. This namespace defines the constructs (elements and attributes) used in XML schema documents. The http://www.w3.org/2001/XMLSchema-instance namespace. This namespace defines the constructs to be used in XML documents that are not schemas. These are usually assigned the namespace prefixes xs and xsi, respectively, although a lot of other schema-manipulating products will use the xsd prefix rather than xs. A complete explanation of the XML Schema specification is beyond the scope of this book, but Listing 8-5 illustrates the main concept. Built-in Misc. Types date time dateTime duration gDay gMonth gYear gYearMonth gMonthDay Time Cluster boolean base64Binary hexBinary Misc. Cluster Figure 8-1: XSD Base Data Types
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
XML IN THE DATABASE: THE XML DATA TYPE procedures or user-defined function parameters and user-defined function return codes may be XML types. This allows you to return dynamic XML to the user based on logical operations, such as in the following example. create the user-defined function CREATE FUNCTION my_business_logic( in_dept INTEGER ) RETURNS XML AS DECLARE @x XML do some business logic that produces an XML document RETURN @x GO now call it, using dept 10 SELECT * FROM dbo.my_business_logic(10) Note that the XMLreturn code is a scalar type, rather than a TABLEtype. As with XML data type columns and variables, procedure parameters and return codes can be declared with a schema collection name and used to ensure schema validity. Although being able to schema-validate input parameters may obviate the requirement for a lot of domain-checking of input, we can still use the fact that we can do processing inside procedures to make the XML a lot more dynamic. We ll see how using and producing XML based on dynamic rules and being able to pass in XML as just another data type can be used in conjunction with new extended features of the composition and decomposition functions, SELECT…FORXMLand OpenXML, in a few sections. Typed and Untyped XML Cataloging and Using XML Schema Collections In addition to storing untyped XML documents or fragments in an XML data type column, you can use SQL Server to validate your XML data type column, variable, or parameter by associating it with an XML schema collection. Therefore, you can think of XML data types as being either schema-validated (containing data types defined by a specific set of XML schemas) or untyped (containing any well-formed XML). Whether your XML type is typed or untyped, it can still contain documents or fragments, since fragments can also be schema-valid. In addition, when you define an XML type to be schema-validated, you can also specify that it can contain only XML documents, or XML documents or fragments (known as XML content).
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
USING XML DATA VARIABLES AND PARAMETERS lexical fidelity. Listing 8-4 illustrates what you can and cannot do with the XMLdata type in SQL statements. Listing 8-4: Using the XML Data Type in SQL Statements assume the same xml_tab as in previous examples comparison to NULL works SELECT the_id FROM xml_tab WHERE xml_col IS NULL illegal SELECT xml_col FROM xml_tab GROUP BY xml_col SELECT xml_col FROM xml_tab ORDER BY xml_col SELECT xml_col FROM xml_tab WHERE xml_col =
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
XML IN THE DATABASE: THE XML DATA TYPE the character or National character data types, as well as BINARY and VARBINARY. When you cast from BINARY and VARBINARY, you can either specify the encoding in the XML itself or include the beginning byte-order mark (0xFFFE) if the format is Unicode. When you cast to BINARY or VARBINARY, the XML will be cast to UTF-16 with the byte-order mark present. You can cast a TEXT or NTEXT data type instance to an instance of the XML type, but you cannot cast an instance of the XML type to TEXT or NTEXT. Using one of the special methods of the XML data type (the value method, discussed later in this chapter) can produce different SQL data types. Listing 8-3 shows retrieving data from an XMLdata type column. Listing 8-3: Returning Data from an XML Data Type Column CREATE TABLE xml_tab( the_id INTEGER PRIMARY_KEY IDENTITY, xml_col XML) GO INSERT INTO xml_tab VALUES(
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services
USING THE XML DATA TYPE IN TABLES these work fine INSERT INTO xml_tab VALUES(
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost PHP Web Hosting services
XML IN THE DATABASE: THE XML DATA TYPE < ! This fails with an unknown encoding error > < ?xml version= 1.0 encoding= i-bogus ?>
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Cheap Web Hosting services
USING THE XML DATA TYPE IN TABLES used to improve the performance of the functions associated with an XML data type. In addition to the previous example that created a single XMLtype column in a table, you can have tables that contain more than one XMLdata type column. You can create a table with an XML column in local or global temporary tables as well as ordinary tables. An XML data type column can be used in a VIEWas well. XMLdata type columns have certain limitations when used in tables. They may not be declared as a PRIMARYKEYin a table. They may not be declared as a FOREIGNKEYin a table. They may not be declared with a UNIQUEconstraint. They may not be declared with the COLLATEkeyword. These first three limitations exist because individual instances of the XML data type may not be compared with each other. Although it would not be difficult to perform a string comparison with XML data, it would be an order of magnitude more difficult to perform a comparison at the Infoset level. For example, the two XML documents in the following example are Infoset-equivalent, but not lexically equivalent. < ! These two documents are equivalent >
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services
Powered by Cheap Web Hosting