9 Pink ' -- Here we create the table to hold the XML data CREATE TABLE #tbl_xml id INT IDENTITY PRIMARY KEY, employee XML -- Here, we insert the XML data into the xml column of the t
Trang 1function, via SOAP, and, presto, they have the information So what happens during
the process of requesting the auction information? We will use the diagram shown
in Figure 11.12 to walk through the process
Figure 11.12 Requesting Information via a Web Service
1 The “Application” creates a SOAP request and invokes the web service
2 The SOAP request is sent over a network using the HTTP protocol The
“Web Server” receives the SOAP request, which then carries out the work
it has been asked to do on the “Database Server.”
3 When the work is completed, it is turned into a SOAP response
4 The SOAP response is sent over a network using the HTTP protocol, and
the SOAP request is processed by the “Application.”
Web Services are being used by more and more companies worldwide as a
mechanism to transfer data It can bring together applications that are not written
in the same programming language
XQuery Explained
SQL Server 2008 supports a subset of the XQuery language as defined by the
World Wide Web Consortium (W3C) The XQuery is a fully featured language
that is used to query structured or semistructured XML data Based on the
existing XPath query language, XQuery adds support for better iteration, better
sorting results, and the ability to construct the necessary XML XQuery can be
used to query and manipulate data from XML documents or data sources that
can be viewed by XML There are four simple methods for querying XML data
with XQuery:
Trang 2Query( )
■
■
Value( )
■
■
Exist( )
■
■
Nodes( )
■
■
The query() method is used to return XML data that matches a query
In Figure 11.13, you will perform a query that returns everyone that has
a favorite color
DECLARE @xmlData XML
SET @xmlData = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employee>
<person>
<name>
<FirstName>Addie</FirstName>
<LastName>Banning</LastName>
</name>
<Age>21</Age>
<FavoriteColor>Blue</FavoriteColor>
</person>
<person>
<name>
<FirstName>Bill</FirstName>
<LastName>Bergen</LastName>
</name>
<Age>99</Age>
<FavoriteColor>Green</FavoriteColor>
</person>
<person>
<name>
<FirstName>Jennifer</FirstName>
<LastName>Liddle</LastName>
Figure 11.13 Sample Query( ) Method
Trang 3</name>
<Age>9</Age>
<FavoriteColor>Pink</FavoriteColor>
</person>
</employee>'
Here we create the table to hold the XML data
CREATE TABLE #tbl_xml (id INT IDENTITY PRIMARY KEY, employee XML)
Here, we insert the XML data into the xml column of the table
INSERT INTO #tbl_xml(employee)
VALUES (@xmlData)
Here, we perform our query
SELECT employee.query(
'for $p in //employee
where $p//FavoriteColor
return
<employee>
<name>{$p//FirstName}</name>
</employee>
'
)
FROM #tbl_xml
DROP TABLE #tbl_xml
In Figure 11.13 you created a temporary table called #tbl_xml and inserted the
XML data into that temporary table The query shown in Figure 11.14 uses
XQuery to SELECT the information in the XML data type to list everyone that
has a favorite color Let’s take a look at this query in more detail
SELECT employee.query(
'for $p in //employee
where $p//FavoriteColor
Figure 11.14 Query( ) Method In-depth
Trang 4The first part of your query, SELECT people.query, uses a standard SQL
Command, SELECT, followed by the column name in your #tbl_xml document, people You then use the method, query(), to tell your SELECT statement that you
will be querying against this XML data column After that, you simply write out an
XPath statement and close it with the FROM clause XPath will be discussed in the
next section
You need to follow some basic syntax rules when you write your code First, XQuery is case sensitive Pay close attention to this Second, XQuery elements and attributes MUST BE valid XML names Lastly, XQuery vari-ables are always defined with a $ followed by the variable name
(example: $name).
return
<employee>
<name>{$p//FirstName}</name>
</employee>
'
)
FROM #tbl_xml
Table 11.5 describes the XQuery method argument
Query() Argument Description
XQuery Is a string, an XQuery expression, that queries
for XML nodes such as elements, attributes, in an XML instance.
Table 11.5 Query( ) Method Argument
The value( ) method allows you to extract a value from the XML document This will allow you to compare XML data with data from non-XML columns
Trang 5For example, in Figure 11.15, you can use the following query to return the age of
“Bill Bergen” to an integer
SELECT employee.value('/employee[1]/person[2]/Age[1][text()]', 'int')
AS Age FROM #tbl_xml
Figure 11.15 Sample Value() Method
As you can see, the value( ) method requires arguments The first argument is
the XQuery expression, and the second argument is the SQL data type Use
Table 11.6 as a guide for the value( ) parameter
Value() Argument Description
XQUERY Is the XQuery expression, a string literal, that
retrieves data inside the XML instance The XQuery must return at most one value Otherwise, an error
is returned.
SQLType Is the preferred SQL type, a string literal, to be
returned The return type of this method matches the SQLType parameter SQLType cannot be an XML data type, a common language runtime (CLR) user-defined type, image, text, ntext, or sql_variant data type SQLType can be an SQL, user-defined data type.
Table 11.6 Value() Method Argument
The exist( ) method is used to check the existence of a value in a XML
document This method will return an integer value of 1 if the value returned is a
non-NULL value and a 0 integer for a NULL value In Figure 11.16, you will
query the sample XML document, shown in Figure 11.13, to determine whether
the specified values exist Table 11.7 describes the exist() method argument
This example checks to see if an employee with the first name
of Jennifer exists
SELECT pk, employee FROM #tbl_xml
WHERE employee.exist('/employee/person/name/FirstName[.="Jennifer"]') = 1
Figure 11.16 Samples of the Exist( ) Method