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(parsed_xml_expression)
A parsed_xml_expression is a sql_query_expression whose datatype is image, binary, or varbinary.
If the parameter of xmlrepresentation() 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 A, “The sample_docs Example Table.”.
Example 1 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))
-----------
0
Example 2 Columns of datatype image can contain both parsed XML documents (generated by the xmlparse function) and unparsed XML documents. After the update commands in this 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'
Example 3 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)
Example 4 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.
Example 5 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)
Example 6 This command restores the sample_docs table to its original state.
update sample_docs set image_doc = null