xmltest

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.

Syntax

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_expression

Description

NoteFor information on processing I18N data, see Chapter 6, “XML Support for I18N.”

NoteSee Chapter 3, “XML Language and XML Query Language,” for the following topics:

Options

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.

Exceptions

If the value of the xml_data_expression is not valid XML, or is an all blank or empty string:

Examples

These examples use the sample_docs table described in Appendix A, “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        
-----  
false
 

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 examples, the xmlerror options determine the treatment of invalid XML documents by the xmltest predicate.

This command restores the sample_docs table to its original state:

delete from sample_docs
where name_doc='invalid doc'