Search
Database

SQL Server 2008 : Explaining XML - XQuery and XPath

Published 7 min readBy Programming4us Editorial Team
XQuery is a language used to query XML data. You can 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:
  • Query( )

  • Value( )

  • Exist( )

  • Nodes( )

The query() method is used to return XML data that matches a query.

In Figure 1, we will perform a query that returns everyone that has a favorite color.

Figure 1. Sample query() Method

In Figure 1 we created a temporary table called #tbl_xml and inserted the XML data into that temporary table. The query shown in Figure 12.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.

Figure 2. Query() Method In-depth

The first part of our query, SELECT people.query, uses a standard SQL command, SELECT, followed by the column name in our #tbl_xml document, people. We then use the method, query(), to tell our SELECT statement that we will be querying against this XML data column. After that, we simple write out an XPath statement and close it with the FROM clause. XPath will be discussed in our next section. Table 1 includes a description of the XQuery argument.

Table 1. Query() Method Argument

Query() ArgumentDescription
XQueryIs a string, an XQuery expression that queries for XML nodes such as elements and attributes, in an XML instance.

Warning

You need to observe some basic syntax rules when you are writing your code. First, XQuery is case-sensitive. Pay close attention to this. Second, XQuery elements and attributes MUST BE valid XML names. Lastly, XQuery variables are always defined with a $ followed by the variable name (example: $name).


The value() method allows you to extract a value from the XML document. This will enable you to compare XML data with data from non-XML columns. For example, in Figure 2, you can use the following query to return the age of ‘Bill Bergen’ to an integer.

Figure 2. 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 2 as a guide for the value() parameter.

Table 2. Value() Method Argument

Value() ArgumentDescription
XQUERYIs 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.
SQLTypeIs 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.

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 12.16 we will query the sample XML document, shown in Figure 12.13, to see if the name ‘Jennifer’ exists. Table 3 includes a description of the Exist() argument.

Figure 3. Sample Exist() Method

Table 3. Exist() Method Argument

Exist() ArgumentsDescription
XQueryIs an XQuery expression, a string literal.

The last method for querying XML documents in an XML data type is the nodes() method. The nodes() method will return a rowset for each row in the query. This is helpful when you want to make your XML data type into a relational format. Every XML document has a node, and in the XML data type this is the document node. The document node is the node at the top of every XML data type. In Figure 12.17, we query the sample XML document, shown in Figure 4, to return rows for each person in our employee node. Table 4 includes a description of the Nodes() argument.

Figure 4. Sample Node() Method

Table 4. Nodes() Method Argument

Nodes() ArgumentDescription
XQueryIs a string literal, an XQuery expression. If the query expression constructs nodes, these constructed nodes are exposed in the resulting rowset. If the query expression results in an empty sequence, the rowset will be empty. If the query expression statically results in a sequence that contains atomic values instead of nodes, a static error is raised.
Table(Column)Is the table name and the column name for the resulting rowset.

SQL Server 2008 provides extensions that allow XQuery to modify data using the modify() method. The modify() method includes three DML statements: insert, delete, and replace value of (see Table 5).

Table 5. Modify() Method Argument

DMLDescription
insertInserts one or more nodes as a child. Insert allows you to insert XML before or after and existing XML node. You can also insert attributes.
deleteDelete XML elements or attributes from your XML document
replace value ofReplace a node with a new value that you specify. The node you select must be a single node, not multiple nodes.

In Figure 5 we use our XML document, shown in Figure 1, to perform and insert, delete, and replace value of. Figure 12.18 will insert the tag <HireDate></HireDate> for employee number 3, Jennifer. Notice that we use the UPDATE statement to perform this function and not the INSERT statement. The insert is performed in the XQuery command.

Figure 5. Insert DML for Modify() Method

In Figure 6 we will update employee number three’s age from 9 to 10 using the replace value of DML.

Figure 6. Replace Value of DML for Modify() Method

In Figure 7 we will use the delete DML. Here we will delete employee number three’s HireDate.

Figure 7. Using the Delete DML


XPath

XPath (XML Path Language) is a query language used to identify a set of nodes within an XML document. XPath can be used to compute values such as strings, numbers, or Boolean values from an XML document. It supports many different expression types. Table 6 describes each expression type.

Table 6. Expression Types for XPath

CategoryFunctionDescription
NumericCeilingReturns the smallest integer of the values passed
 FloorReturns the largest integer of the values passed
 RoundRounds to the nearest integer
BooleanNotTrue or False value
StringConcatConcatenates the strings passed
 ContainsReturns a true value if the first argument contains the second argument
 SubstringReturns a portion of the first argument starting at the location of the second argument
 String-lengthReturns the length of a string passed
NodeNumberReturns a number for the value of the node passed
ContextPositionReturns an integer that is the current position in the sequence.
 LastReturns an integer that is the count of the last item in the sequence
SequencesEmptyReturns true if the argument passed, which is a sequence, is empty.
 DistinctvaluesRemoves duplicates from your sequence
AggregateAvgReturns the average of a set of numbers
 CountCounts the number of items in the set and returns an integer
 MinReturns the minimum value from a set of numbers
 MaxReturns the maximum value from a set of numbers
 SumReturns the sum of a set of numbers.
ConstructorVariousAllows you to create an XSD type from another type or literal
DataAccessDataReturns the typed value of the node
 StringReturns the value of the argument as a string

Now, let’s explore some examples of how SQL Server 2008 uses XPath to calculate numbers, strings, and Boolean values. In Figure 8, we have created an XML document and stored it in our XML data type in the declared table @XMLTable.

Figure 8. Sample XML Document

In Figure 9 we will use the XPath function, count, in order to count the number of item nodes in our XML document.

Figure 9. XPath Expression count

In Figure 10 we will use the XPath function, contains, to return the string that contains the word “BaseBall Bats.”

Figure 10. XPath Expression Contains

These are just two examples of all the XPath expressions you can use.