1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Module 3: Validating pdf

30 520 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Validating Pdf
Trường học University of Information Technology
Chuyên ngành Computer Science
Thể loại Tài liệu
Thành phố Ho Chi Minh City
Định dạng
Số trang 30
Dung lượng 336,91 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Consider the following query: SELECT EmployeeID, LastName, FirstName FROM Employees ORDER BY LastName, FirstName FOR XML RAW This produces the following output:... Consider the followi

Trang 1

Appendix C: Appendix to Chapter 9: SQL XML

You can use this appendix to extend your knowledge of Microsoft®SQL Server™ 2000 support for Extensible Markup Language (XML) This continues where Module 9, “SQL XML,” leaves off In addition, there are two lab exercises at the end of this appendix To begin them, you must first complete the first two exercises in Lab 9

Lesson: FOR XML and OpenXML

In this lesson, you will learn about the FOR XML and OpenXML query types

FOR XML

To query a SQL Server database, the XML query must specify the type of output required This is indicated by the mode in the FOR XML clause There are three modes to choose from: RAW, AUTO, and EXPLICIT You can specify three additional parameters to return particular XML output You determine the form of the output by using these various modes and parameters The form of the output depends on your application requirements and what other uses you plan to put the data to

Introduction

Trang 2

The syntax of the FOR XML clause is:

FOR XML mode [, XMLDATA] [, ELEMENTS][, BINARY BASE64]

The arguments that you can use in the FOR XML clause are listed in the following table

Argument Purpose

It can be RAW, AUTO, or EXPLICIT You must specify one mode

returned The schema is prepended to the document as an inline schema

returned as subelements Otherwise, they are mapped to XML attributes This option is supported in AUTO mode only

data returned by the query is represented in base64-encoded format To retrieve binary data in RAW and EXPLICIT mode, this option must be specified In AUTO mode, binary data is returned as a reference by default

The use of the RAW mode means that the results of the query are represented

by a row element with attributes mapped to the column names for values that are not NULL Consider the following query:

SELECT EmployeeID, LastName, FirstName FROM Employees

ORDER BY LastName, FirstName FOR XML RAW

This produces the following output:

<row EmployeeID="5" LastName="Buchanan" FirstName="Steven"/>

<row EmployeeID="8" LastName="Callahan" FirstName="Laura"/>

<row EmployeeID="1" LastName="Davolio" FirstName="Nancy"/>

<row EmployeeID="9" LastName="Dodsworth" FirstName="Anne"/>

<row EmployeeID="2"…

Syntax

FOR XML RAW

Trang 3

In contrast, when you use the AUTO mode, the table is represented as an element and the columns are represented as attributes Consider the following query:

SELECT EmployeeID, LastName, FirstName FROM Employees

ORDER BY LastName, FirstName FOR XML AUTO

This produces the following output:

<Employees EmployeeID="5" LastName="Buchanan"

Trang 4

The EXPLICIT mode is beyond the scope of this course, because it requires considerable explanation and has many special conditions associated with it Here you will only be introduced to the basic concept

The EXPLICIT option enables you to control the output of your XML document in considerable detail through a combination of techniques When the query is in EXPLICIT mode, it generates output referred to as a universal table You can also shape the output by using additional controls called directives

To do an EXPLICIT query, you must specify special metadata information, through use of the keywords Tag and Parent, which gives structure to the universal table When you use this EXPLICIT mode with the previous query, the query becomes the following:

SELECT 1 as Tag, NULL as Parent, EmployeeID as [Employees!1!EmployeeID], Lastname as [Employees!1!Lastname], Firstname as [Employees!1!Firstname]

FROM Employees

This produces the following output

Tag Parent Employees!1!

EmployeeID

Employees!1!

Lastname

Employees!1! Firstname

In normal practice, you use the EXPLICIT mode in complex queries For example, you can use the FOR XML EXPLICIT clause at the end of a join, as

in the following sample code:

SELECT 1 as Tag, NULL as Parent, Customers.CustomerID as [Customer!1!CustomerID], NULL as [Order!2!OrderID]

FROM Customers UNION ALL SELECT 2,

1, Customers.CustomerID, Orders.OrderID

FROM Customers, Orders WHERE Customers.CustomerID = Orders.CustomerID ORDER BY [Customer!1!CustomerID], [Order!2!OrderID]

FOR XML EXPLICIT

FOR XML EXPLICIT

Trang 5

When this query is executed, it produces the following output The results show the customer ID and order IDs associated with each customer

CustomerID="ANATR"><Order OrderID="10308"/>…

For more information about FOR XML EXPLICIT, see “Using

EXPLICIT mode” in SQL Server Books Online or Course 2091A, Building

XML-Enabled Applications Using Microsoft SQL Server 2000

Note

Trang 6

OPENXML

If you want to work with an XML document in a relational database environment, it is often useful to represent it as a normal relational database object like a view or a table The OPENXML keyword enables you to render an XML document as a rowset, through the use of Transact-SQL and XML document manipulation techniques like the Document Object Model (DOM) You can render an XML document as a rowset by preparing the XML document in memory and converting it to a rowset You can then use the rowset with the SELECT, UPDATE, INSERT, and SELECT INTO commands, either directly or, more likely, through stored procedures

This topic provides a brief introduction to OPENXML concepts This is a complex topic and assumes familiarity with XML technologies and techniques including DOM, XML Path Language (XPath), and metaproperties

You must first tell SQL Server to create the XML document in memory by

using the sp_xml_preparedocument stored procedure You then access the

XML in memory by using XPath to convert the data in the elements and attributes into a SQL Server style rowset The rowset can be converted into a SQL Server table through a process known as shredding Shredding is a process

in which a schema is used to convert XML nodes to SQL Server table columns When you are finished processing the XML document, you need to specifically remove it from memory by using the special stored procedure

sp_xml_removedocument

The syntax for OPENXML is as follows:

OPENXML(idoc int [in],rowpattern nvarchar[in],

[flags byte[in]]) [WITH (SchemaDeclaration | TableName)]

The main arguments for this syntax are summarized in the following table

Argument Description

was created by sp_xml_preparedocument

to rows

mapped to a relational rowset

to rows

map nodes to rows

Introduction

Syntax

Trang 7

Consider the following code, which creates an XML document in memory and then retrieves the information from the rowset by using OPENXML:

DECLARE @idoc int, @doc varchar(1000) SET @doc ='

FROM OPENXML (@idoc, '/ROOT/Customer',1) WITH (CustomerID varchar(10), ContactName varchar(20)) ORDER BY CustomerID

This code produces relational output like this:

CustomerID ContactName - - LILAS Carlos Gonzlez

ROMEY Alejandra Camino (2 row(s) affected)

For more information about OPENXML, see Course 2091A, Building

XML-Enabled Applications Using SQL Server 2000

For a complete description of all of the arguments of OPENXML, see the topic

“OPENXML” in SQL Server 2000 Books Online

Example

Note

Trang 8

Lesson: Manipulating XML Using Transact-SQL

There are many ways that you can use XML in combination with Transact-SQL commands This lesson examines a few of these to give you a sense of how you can combine the two technologies

After completing this lesson, you will be able to:

! Place data from an XML document into an existing table

! Insert data into a new SQL Server table

! Understand how to use nested documents

! Work with metaproperties

! Explain in more detail the ways that you can use XML with SQL commands and SQL objects

Introduction

Lesson objectives

Trang 9

How to Use a Rowset from an XML Document

You use the OPENXML keyword to retrieve data as a rowset from an XML document in memory In this topic, you will learn how to use the rowset in an INSERT command and a SELECT INTO command

You use the INSERT command with the OPENXML keyword like you use a normal INSERT command Using the OPENXML example from the previous topic, the INSERT command looks like the following This successfully inserts

data from the XML document into an existing table named NewList1

… INSERT NewList1 SELECT * FROM OPENXML (@idoc, '/ROOT/Customer',1) WITH (CustomerID varchar(10), ContactName varchar(20))

Trang 10

How to Process Nested Documents

If you use OPENXML with XPath and OPENXML flags, a document with a complicated hierarchy with nested elements can be interpreted and shredded into multiple relational tables

You must use multiple OPENXML commands against the XML document that contains the nested, or child, document

In the following code, notice the two OPENXML commands They are used to process the XML document Each OPENXML command shreds part of the document into the appropriate table for the main body of the document

DECLARE @myxmldoc varchar(500) DECLARE @idoc integer

Source XML po document SET @myxmldoc =

'<po ponum="021044" podate="09/18/2001" sourceid="2605"> <orderitem productid="04" price="104.33">

SELECT * FROM OpenXML (@idoc, 'po', 1) WITH (ponum integer, podate datetime, sourceid integer) Get order item data SELECT * FROM

OpenXML (@idoc, 'po/orderitem', 1) WITH (ponum integer ' /@ponum', productid integer,

price money, qnty integer './qnty') Tidy up memory

EXEC sp_xml_removedocument @idoc

Introduction

Example

Trang 11

When this is executed in Query Analyzer, the results of the OPENXML command become the following:

ponum podate sourceid

- - -

21044 2001-09-18 00:00:00.000 2605

(1 row(s) affected) ponum productid price qnty

- - - -

21044 4 104.3300 4

21044 34 65.2100 10

21044 45 21.9900 17 (3 row(s) affected)

Trang 12

Lesson: Retrieving XML Data with ADO and ADO.NET

Both Microsoft ActiveX® Data Objects (ADO) and ADO.NET, have special features that enable them to use data in a Web context However, ADO.NET is highly optimized for the Web, while ADO is not as well optimized for the Web This indicates how technologies have to change to fit the evolving data-centric Internet requirements for e-business

You can use both ADO and ADO.NET to develop Web applications and solutions Each is a major topic and beyond the scope of this course To work with XML data when implementing Microsoft NET, you need to know about how you can use them in Active Server Pages (ASP) and ASP.NET Web solutions These topics are addressed in depth in several other Microsoft MSDN® courses

ADO provides extensive support for XML data handling through the use of

persisting XML data in stream objects However, it is not specifically

optimized for Web and Internet work This, combined with recordset and cursor support, works well for many purposes Data retrieved from a SQL Server 2000 database through an ADO recordset can be persisted as XML and then used in

an e-commerce transaction or other Internet-based business transaction

ADO.NET was specifically designed for Internet-based business and data

handling ADO.NET uses the DataSet and DataReader objects for the rapid

manipulation and reading of XML data in memory Both objects are highly optimized for these specific purposes and directly support the various NET classes required for reading, handling, and manipulating data

Courses for Microsoft Visual InterDev®, Microsoft Visual Studio® NET, ASP.NET and ADO.NET are all available For complete descriptions, see course information on the Training and Certification Web page at

Trang 13

Lesson: Using Mapping Schemas

Matching XML elements and attributes to a relational database may be quite challenging In SQL Server 2000, Microsoft introduced the use of XML-Data Reduced (XDR) schemas to help you map SQL objects to XML documents

In Web Release 1 and 2, XDR schemas were replaced by XML Schema Definitions (XSD) schemas, which are compliant with the World Wide Web Consortium (W3C) Recommendation for schemas XDR schemas are a Microsoft solution used while the W3C standard was developed Eventually, the XSD schema language will completely replace the XDR schema language After completing this lesson, you will be able to describe:

! Why schemas are used with SQL Server

! The use of schemas to map multiple tables to XML documents

! The use of annotations to map XML elements and attributes to relational objects

! The benefits of using the SQL Server XML View Mapper tool to match SQL schemas to XDR schemas

Introduction

Lesson objectives

Trang 14

What Are Schemas?

You can use schemas with SQL Server 2000 to provide XML views that are analogous to SQL Server Views Just as you can query a SQL Server View with SELECT commands, you can also query a schema with an XPath query pattern Schemas define the structure of an XML document and the nature of its data You can reference schemas as virtual names in the Internet Information Services (IIS) Virtual Directory, which must be created if you want to work with XML against a SQL Server database Developing schemas is reasonably complicated, but you can get help by using development tools

The following schema is one example of how XML can describe a relational table by using an XDR schema In this case, the schema describes the

The nature of schemas is changing For more information, see the topic

“Creating XML Views Using Annotated XSD Schemas” in SQL Server Books Online

Introduction

Example

Note

Trang 15

How to Annotate a Schema

While XDR schemas are useful for basic XML work, it is not as useful with SQL Server as it can be To achieve the kind of utility required, XDR schemas need to be annotated

Annotating schemas allows you to map XML elements and attributes to SQL Server tables and the columns within the tables You can use annotated schemas in XPath queries to directly query data in several different ways, including through templates and Uniform Resource Locator (URL) queries

There are several types of annotations The sql:relation and sql:field

annotations are the most basic annotations and relate the XML document elements and attributes to a table view and to the columns in a database table view, respectively You can also annotate data types and various references

An XDR schema with annotations looks like this:

<attribute type="EmpID" sql:field="EmployeeID" />

<attribute type="FName" sql:field="FirstName" />

<attribute type="LName" sql:field="LastName" />

</ElementType>

</Schema>

The annotations relate the XML elements and attributes to the Employees table

and its columns The names are case-sensitive

Introduction

Example

Ngày đăng: 24/01/2014, 10:20

TỪ KHÓA LIÊN QUAN