![]() | ![]() |
Home |
|
|
XML Services in Adaptive Server Enterprise |
|
| Chapter 2 XML Query Functions |
Chapter 2
This chapter describes the XML query functions in detail, and describes the general format of the option_string parameter.
This section describes the SQL extensions for accessing and processing XML documents in SQL statements. The functions are as follows:
Function | Description |
xmlextract | A built-in function that applies an XML query expression to an XML document and returns the selected result. |
xmltest | A SQL predicate that applies an XML query expression to an XML document and returns the boolean result. |
xmlparse | A built-in function that parses an XML document for more efficient processing. |
xmlrepresentation | A built-in function that determines whether a given image column contains a parsed XML document. |
The descriptions of these functions include examples that reference Appendix B, "The sample_docs Example Table," which includes a script for creating and populating the table.
A built-in function that applies the XML_query_expression to the xml_data_expression and returns the result. This function resembles a SQL substring operation.
xmlextract_expression ::=
xmlextract (xml_query_expression,xml_data_expression
[optional_parameters])
xml_query_expression ::=basic_string_expression
xml_data_expression ::= general_string_expression
optional_parameters ::=
options_parameter
| return_type
| options_parameterreturn_type
options_parameter ::= [,] option option_string
returns_type ::= [,] returns {varchar[(integer)] | text | image }
option_string ::= [,] basic_string_expressionA basic_string_expression is a sql_query_expression whose datatype is character, varchar, or java.lang.String.
A general_string_expression is a sql_query_expression whose datatype is text, image, character, varchar, or java.lang.String.
An xmlextract expression can be used in SQL language wherever a character expression is allowed.
The default options_parameter is an empty string. A null options parameter is treated as an empty string.
If the xml_query_expression of an xmlextract call is null, then the result of the xmlextract call is null.
The value of the xml_data_expression parameter is the runtime context for execution of the XML query expression.
The datatype of an xmlextract call is specified by the returns_type.
The default returns_type is text.
If a varchar specified as the returns_type without an integer, the default is 30.
If numeric or decimal is specified without a precision (the first integer), the default is 18. If it is specified without a scale (the second integer), the default is 0.
The initial result of an xmlextract call is the result of applying the xml_query_expression to the xml_data_expression. That result is specified by the XPath standard.
If the returns_type specifies varcharimage, text, or image, the initial result value is returned as a character-string document of that datatype.
If the returns_type specifies a numeric, money, or date-time datatype, the initial result is converted to that datatype and returned. The conversion follows the rules specified for the convert built-in function.
The initial result must be a value suitable for the convert built-in function. This requires using the text() reference in the XML query expression. See the examples following.
See Chapter 3, "XML Language and XML Query Language,"for the following topics:
Restrictions on external URL references, XML namespaces, and XML schemas.
Treatment of predefined entities and their corresponding characters: & (&), < (,), > (>), "e; ("), and ' (;). Be careful to include the semicolon as part of the entity.
Treatment of whitspace.
Treatment of empty elements.
The general format of the option_string is described in option_strings: general format.
The option supported for the xmlextract function is:
xmlerror = {exception| null| message}If the value of the xml_data_expression is not not valid XML, or is an all blank or empty string:
If the explicit or default option specifies that xmlerror=exception, an exception is raised:
invalid XML data
If the explicit or default option specifies xmlerror=null a null value is returned.
If the explicit or default options specifies xmlerror=message, a character string containing an XML element, which contains the exception message, is returned. This value is valid XML.
If the return_type of the xmlextract_expression is varchar and the runtime result of evaluating the xml_query_expression parameter is longer than the maximum length of a varchar, then an exception is raised:
result exceeded maximum varchar length
The following examples use the sample_docs table described in Appendix B, "The sample_docs Example Table".
This example selects the title of documents that have a bookstore/book/price of 55 or a bookstore/book/author/degree whose from attribute is "Harvard".
select xmlextract('/bookstore/book[price=55
| author/degree/[@from="Harvard"] ]/title'
text_doc )
from sample_docs
------------------------------------------------------
<title>History of Trenton</title>
<title>Trenton Today, Trenton Tomorrow</title>
NULL
NULL The following example selects the row/pub_id elements of documents whose row elements either have a price element that is less than 10 or a city element equal to "Boston". This query returns three rows:
A null value from the bookstore row
A single "<row>...</row>" element from the publishers row
4 "<row>...</row>" elements from the titles row
select xmlextract('//row[price<10 | city="Boston" ]/pub_id',
text_doc) from sample_docs2>
------------------------------------
NULL
<pub_id>0736</pub_id>
<pub_id>0736</pub_id>
<pub_id>0877</pub_id>
<pub_id>0736</pub_id>
<pub_id>0736</pub_id>
(3 rows affected)The following example selects the price of "Seven Years in Trenton" as an integer. This query has a number of steps.
To select the price of "Seven Years in Trenton" as an XML element:
select xmlextract
('/bookstore/book[title="Seven Years in Trenton"]/price',text_doc)
from sample_docs
where name_doc='bookstore'
--------------------------------------
<price>12</price> The following attempts to select the full price as an integer by adding a returns integer clause:
select xmlextract
('/bookstore/book[title="Seven Years in Trenton"]/price',
text_doc returns integer)
from sample_docs
where name_doc='bookstore'
Msg 249, Level 16, State 1:
Line 1:
Syntax error during explicit conversion of VARCHAR value '<price>12</price>' to an INT field.To specify a returns clause with a numeric, money, or date-time datatype, the XML query must return value suitable for conversion to the specified datatype. The query must therefore use the text() reference to remove the XML tags:
select xmlextract
('/bookstore/book[title="Seven Years in Trenton"]/price/text()',
text_doc returns integer)
from sample_docs
where name_doc='bookstore'
-----------
12To specify a returns clause with a numeric, money, or date-time datatype, the XML query must also return a single value, not a list. For example, the following query returns a list of prices:
select xmlextract
('/bookstore/book/price',
text_doc)
from sample_docs
where name_doc='bookstore'
-----------
<price>12</price>
<price>55</price>
<price intl="canada" exchange="0.7">6.50</price> Adding the text() reference yields the following result:
select xmlextract
('/bookstore/book/price/text()',
text_doc)
from sample_docs
where name_doc='bookstore'
-----------------------------
12 55 6.50 Specifying the returns integer clause produces an exception, indicating that the combined values aren't suitable for conversion to integer:
select xmlextract
('/bookstore/book/price/text()',
text_doc returns integer)
from sample_docs
where name_doc='bookstore'
Msg 249, Level 16, State 1:
Line 1:
Syntax error during explicit conversion of VARCHAR value '12556.50' to an INT field.To illustrate the xmlerror options, the following command inserts an invalid document into the sample_docs table:
insert into sample_docs (name_doc, text_doc)
values ('invalid doc', '<a>unclosed element<a>)
(1 row affected)
In the following example, the xmlerror options determine the treatment of invalid XML documents by the xmlextract function:
If xmlerror=exception (this is the default), an exception is raised:
select xmlextract('//row', text_doc
option 'xmlerror=exception')
from sample_docs
Msg 14702, Level 16, State 0:
Line 2:
XMLPARSE(): XML parser fatal error
<<The input ended before all started tags
were ended. Last tag started was 'a'>>
at line 1, offset 23.If xmlerror=null, a null value is returned:
select xmlextract('//row', test_doc
option 'xmlerror=null')
from sample_docs
(0 rows affected)If xmlerror=message, a parsed XML document with an error message will be returned:
select xmlextract('//row', test_doc
option 'xmlerror=message')
from sample_docs
----------------------------------<xml_parse_error>The input ended before all startedtags were ended. Last tag started was 'a'</xml_parse_error>
The xmlerror option is used by xmlextract only when the xml_data_expression is a varchar or text expression, or an image expression whose value is an unparsed XML document. In other words, the xmlerror option doesn't apply to a document that is a parsed XML document or to a document returned by an explicit nested call by xmlparse.
For example, in the following xmlextract call, the xml_data_expression is an unparsed character-string document, so the xmlerror option applies to it. The document is invalid XML, so an exception is raised, and the xmlerror option indicates that the exception message should be returned as an XML document with the exception message:
select xmlextract('/', '<a>A<a>' option'xmlerror=message')
---------------------------------------------------
<xml_parse_error>The input ended before all started tags were ended.
Last tag started was 'a'</xml_parse_error> In the following xmlextract call, the xml_data_expression is returned by an explicit call by the xmlparse function (see section xmlparse). Therefore, the default xmlerror option of the explicit xmlparse call applies, rather than the xmlerror option of the outer xmlextract call. That default xmlerror option is exception, so the explicit xmlparse call raises an exception:
select xmlextract('/', xmlparse('<a>A<a>')
option 'xmlerror=message'))
---------------------------------------------------
Msg 14702, Level 16, State 0:
Line 2:
XMLPARSE(): XML parser fatal error
<<The input ended before all started tags were ended.
Last tag started was 'a'>> at line 1, offset 8.To apply the xmlerror=message option to the explicit nested call of xmlparse, specify it as an option in that call:
select xmlextract('/',
xmlparse('<a>A<a>' option 'xmlerror=message'))
----------------------------------------------------
<xml_parse_error>The input ended before all started
tags were ended. Last tag started was
'a'</xml_parse_error> To summarize the treatment of the xmlerror option for unparsed XML documents and nested calls of xmlparse:
The xmlerror option is used by xmlextract only when the document operand is an unparsed document.
When the document operand is an explicit xmlparse call, the implicit or explicit xmlerror option of that call overrides the implicit or explicit xmlerror option of the xmlextract.
This command restores the sample_docs table to its original state:
delete from sample_docs where na_doc='invalid doc'
A predicate that evaluates the XML query expression, which can reference the XML document parameter, and returns a Boolean result. Similar to a SQL like predicate.
xmltest_predicate ::=
xml_query_expression [not] xmltest xml_data
[option option_string] xml_data ::=
xml_data_expression | (xml_data_expression)
xml_query_expression::= basic_string_expression
xml_data_expression ::= general_string_expression
option_string ::= basic_string_expressionA basic_string_expression is a sql_query_expression whose datatype is character, varchar, or java.lang.String.
A general_string_expression is a sql_query_expression whose datatype is character, varchar, or java.lang.String.
An xmltest predicate can be used in SQL language wherever a SQL predicate is allowed.
An xmltest call specifying that:
X not xmltest Y options Zis equivalent to:
not X xmltest Y options Z
If the xml_query_expression or xml_data_expression of an xmltest call is null, then the result of the xmltest call is unknown.
The value of the xml_data_expression parameter is the runtime context for execution of the XPath expression.
An xmltest call evaluates to boolean true or false, as follows:
The xml_query_expression of an xmltest call is an XPath expression whose result is empty (not empty), then the xmltest call returns false (true).
If the xml_query_expression of an xmltest call is an XPath expression whose result is a Boolean false (true), then the xmltest call returns false (true).
Otherwise, an exception is raised:
invalid xml expression for xmltest
See Chapter 3, "XML Language and XML Query Language,"for the following topics:
Restrictions on external URL references, XML namespaces, and XML schemas.
Treatment of predefined entities and their corresponding characters: & (&), < (,), > (>), "e; ("), and ' (;). Be careful to include the semicolon as part of the entity.
Treatment of whitspace.
Treatment of empty elements.
The general format of the option_string is described in option_strings: general format.
The option supported for the xmltest predicate is xmlerror = {exception | null}.
The message alternative, which is supported for xmlextract and xmlparse, is not valid for xmltest. See the Exceptions section.
If the value of the xml_data_expression is not valid XML, or is an all blank or empty string:
If the explicit or default option specifies xmlerror=exception, an exception is raised:
invalid XML data
If the explicit or default options specifies xmlerror=null a null value is returned.
These examples use the sample_docs table described in Appendix B, "The sample_docs Example Table. ".
This example selects the name_doc of each row whose text_doc contains a row/city element equal to "Boston".
select name_doc from sample_docs where '//row[city="Boston"]' xmltest text_doc name_doc ------------------------ publishers (1 row affected)
In the following example the xmltest predicate returns false/true, for a Boolean false/true result and for an empty/not-empty result.
-- A boolean true is 'true':
select case when '/a="A"' xmltest '<a>A</a>'
then 'true' else 'false' end2>
-----
true
-- A boolean false is 'false'
select case when '/a="B"' xmltest '<a>A</a>'
then 'true' else 'false' end
-----
false
-- A non-empty result is 'true'
select case when '/a' xmltest '<a>A</a>'
then 'true' else 'false' end
----- true
-- An empty result is 'false'
select case when '/b' xmltest '<a>A</a>'
then 'true' else 'false' end -----
false
-- An empty result is 'false' (second example)
select case when '/b="A"' xmltest '<a>A</a>'
then 'true' else 'false' end
-----
falseTo illustrate the xmlerror options, the following command inserts an invalid document into the sample_docs table:
insert into sample_docs (name_doc, text_doc)
values ('invalid doc', '<a>unclosed element<a>)
(1 row affected)In the following examples, the xmlerror options determine the treatment of invalid XML documents by the xmltest predicate.
If xmlerror=exception (the default result), an exception is raised.
select name_doc from sample_docs where '//price<10/*' xmltest text_doc option 'xmlerror=exception' Msg 14702, Level 16, State 0: Line 2: XMLPARSE(): XML parser fatal error <<The input ended before all started tags were ended. Last tag started was 'a'>> at line 1, offset 23.
If xmlerror=null or xmlerror=message, a null (unknown) value is returned.
select name_doc from sample_docs where '//price<10/*' xmltest text_doc option 'xmlerror=null' (0 rows affected)
This command restores the sample_docs table to its original state:
delete from sample_docs where name_doc='invalid doc'
A built-in function that parses the XML document passed as a parameter, and returns an image value that contains a parsed form of the document.
xmlparse_call ::=
xmlparse(general_string_expression [options_parameter])
options_parameter ::= [,] option option_string
option_string ::= basic_string_expressionA basic_string_expression is a sql_query_expression whose datatype is character, varchar, or java.lang.String.
A general_string_expression is a sql_query_expression whose datatype is character, varchar, or java.lang.String.
If any parameter of an xmlparse call is null, the result of the call is null.
If the general_string_expression is an all-blank string, the result of xmlparse is an empty XML document.
An xmlparse call parses the general_string_expression as an XML document and returns an image value containing the parsed document.
See Chapter 3, "XML Language and XML Query Language,"for the following topics:
Restrictions on external URL references, XML namespaces, and XML schemas.
Treatment of predefined entities and their corresponding characters: & (&), < (,), > (>), "e; ("), and ' (;). Be careful to include the semicolon as part of the entity.
Treatment of whitspace.
Treatment of empty elements.
The general format of the option_string is described in option_strings: general format. The options supported for the xmlparse function are:
dtdvalidate = {yes | no}xmlerror = {exception | null | message }If dtdvalidate=yes is specified, the XML document is validated against its embedded DTD (if any). This option is for compatibility with the Java-based XQL processor of Adaptive Server Enterprise 12.5.
If dtdvalidate=no is specified, no DTD validation is performed. This is the default.
xmlerror = {exception | null | message}For the xmlerror option, see "Exceptions" below.
If the value of the xml_data_expression is not valid XML, then:
If the explicit or default options specifies xmlerror=exception, an exception is raised:
invalid XML data
If the explicit or default options specifies xmlerror=null, then a null value will be returned.
If the explicit or default options specifies xmlerror=message, then a character string containing an XML element with the exception message is returned. This value is valid parsed XML.
These examples use the sample_docs table described in
As created and initialized, the text_doc column of the sample_docs table contains documents, and the image_doc column is null. You can update the image_doc columns to contain parsed XML versions of the text_doc columns:
update sample_docs set image_doc = xmlparse(text_doc) (3 rows affected)
You can then apply the xmlextract function to the parsed XML documents in the image column in the same way as you apply it to the unparsed XML documents in the text column. Operations on parsed XML documents generally execute faster than on unparsed XML documents.
select name_doc,
xmlextract('/bookstore/book[title="History of Trenton"]/price', text_doc)
as extract_from_text_doc,
xmlextract('/bookstore/book[title="History of Trenton"]/price', image_doc)
as extract_from_image_doc
from sample_docs
name_doc extract_from_text_doc extract_from_image_doc
---------- --------------------- ------------------------
bookstore <price>55</price> <price>55</price>
publishers NULL NULL
titles NULL NULL
(3 rows affected)To illustrate the xmlerror options, this command inserts an invalid document into the sample_docs table
insert into sample_docs (name_doc, text_doc)
values ('invalid doc', '<a>unclosed element<a>)
(1 row affected)In this example, the xmlerror options determine the treatment of invalid XML documents by the xmlparse function:
If xmlerror=exception (the default), an exception is raised:
update sample_docs set image_doc = xmlparse(text_doc option 'xmlerror=exception') Msg 14702, Level 16, State 0: Line 2: XMLPARSE(): XML parser fatal error <<The input ended before all started tags were ended. Last tag started was 'a'>> at line 1, offset 23.
If xmlerror=null, a null value is returned:
update sample_docs set image_doc = xmlparse(text_doc option 'xmlerror=null') select image_doc from sample_docs where name_doc='invalid doc' ------ NULL
If xmlerror=message, then parsed XML document with the error message is returned:
update sample_docs
set image_doc = xmlparse(text_doc option 'xmlerror=message')
select xmlextract('/', image_doc)
from sample_docs
where name_doc = 'invalid doc'
------------------------
<xml_parse_error>The input ended before all started tags were ended. Last tag started was 'a'</xml_parse_error>This command restores the sample_docs table to its original state:
delete from sample_docs where name_doc='invalid doc'
Examines the image parameter, and returns an integer value indicating whether the parameter contains parsed XML data or other sorts of image data.
xmlrepresentation_call::= xmlrepresentation(image_expression)
An image_expression is an sql_query_expression whose datatype is image.
If the parameter of an xmlrepresentation call is null, the result of the call is null.
xmlrepresentation returns an integer 0 if the operand is parsed XML data, and a positive integer if the operand is either not parsed XML data or an all blank or empty string.
These examples use the sample_docs table described in Appendix B, "The sample_docs Example Table. ".
This example illustrates the basic xmlrepresentation function.
-- Return a non-zero value
-- for a document that is not parsed XML
select xmlrepresentation(
xmlextract('/', '<a>A</a>' returns image)
-----------
1
-- Return a zero for a document that is parsed XML
select xmlrepresentation(
xmlparse(
xmlextract('/', '<a>A</a>' returns image))
-----------
0Columns of datatype image can contain both parsed XML documents (generated by the xmlparse function) and unparsed XML documents. After the update commands in the following example, the image_doc column of the sample_docs table contains a parsed XML document for the titles document, an unparsed (character-string) XML document for the bookstore document, and a null for the publishers document (the original value).
update sample_docs
set image_doc = xmlextract('/', text_doc returns image)
where name_doc = 'bookstore'
update sample_docs
set image_doc = xmlparse(text_doc)
where name_doc = 'titles'You can use the xmlrepresentation function to determine whether the value of an image column is a parsed XML document:
select name_doc, xmlrepresentation(image_doc)from sample_docs name_doc --------- ----------- bookstore 1 publishers NULL titles 0 (3 rows affected)
You can update an image column and set all of its values to parsed XML documents. If the image column contains a mixture of parsed and unparsed XML documents, a simple update raises an exception.
update sample_docs set image_doc = xmlparse(image_doc) Msg 14904, Level 16, State 0: Line 1: XMLPARSE: Attempt to parse an already parsed XML document.
You can avoid such an exception by using the xmlrepresentation function:
update sample_docs set image_doc = xmlparse(image_doc) where xmlrepresentation(image_doc) != 0 (1 row affected)
The following command restores the sample_docs table to its original state.
update sample_docs set image_doc = null
This section specifies the general format, syntax and processing of option string parameters in XML Services. Actions of individual options are described in the functions that reference them.Any function that has an option_string parameter accepts the union of all options, and ignores any options that do not apply to that particular function.
For example, forxmlj does not have an XML document parameter, but it still accepts an option_string containing the xmlerror option (which specifies actions for invalid XML operands).This "union options" approach lets you use a single option_string variable for all XML Services functions.
option_string::= basic_string_expression
The complete syntax of the option_string parameter is:
options_string_value ::= option [[,] option] ... option ::= name = value name ::= option name as listed below value ::= simple_identifier | quoted_string
If an option_string parameter is null, it is ignored.
You can use any amount of white space before the first option, after the last option, between options, and around the equals signs.
You can separate options using commas or by white space.
The value of an option can be either a simple identifier, beginning with a letter and continuing with letters, digits, and underscores, or a quoted string. Quoted strings are formed using the normal SQL conventions for embedded quotes.
The set of options, and the functions to which they are applicable, are shown in . See specific function descriptions for descriptions of options.
Option name | Option value | Function |
binary | hex | base64 | forxmlj and for xml clause |
columnstyle | element | attribute | forxmlj and for xml clause |
dtdvalidate | yes | no | xmlparse |
format | yes | no | forxmlj and for xml clause |
header | yes | no | forxmlj and for xml clause |
incremental | yes | no | for xmlj clause |
nullstyle | attribute | omit | forxmlj and for xml clause |
nullclause | null | empty | forsqlcreatej forsqlscriptj |
prefix | SQL name (C) | forxmlj and for xml clause |
root | yes | no | forxmlj and for xml clause |
rowname | SQL name (row) | forxmlj and for xml clause |
schemaloc | quoted string with a URL | forxmlj and for xml clause |
statement | yes | no | for xmlj and forxml clause |
tablename | SQL name (resultset) | forxmlj and for xml clause |
targetns | quoted string with a URL | forxmlj and for xml clause |
xmlerror | exception | null | message | all functions with XML operands |
The defaults of options that specify keywords are underlined. The defaults of options that specify SQL names are parenthesized. The defaults of options that specify string values are the empty string, or a single-space character.
|
|