XMLSERIALIZE operator

XMLSERIALIZE is a SQL/XML operator that you can use to convert an XML type to a character type. There is no other way to convert the type of a Derby XML value.

Attention: Serialization is performed based on the SQL/XML serialization rules. These rules, combined with the fact that Derby supports only a subset of the XMLSERIALIZE syntax, dictate that the results of an XMLSERIALIZE operation are not guaranteed to be in-tact copies of the original XML text. For example, assume that [xString] is a textual representation of a well-formed XML document. You issue the following statements:
INSERT INTO x_table (id, xcol)
    VALUES (3, XMLPARSE(DOCUMENT '[xString]' PRESERVE WHITESPACE));

SELECT id, XMLSERIALIZE(xcol AS VARCHAR(100))
    FROM x_table WHERE id = 3;

There is no guarantee that the result of the XMLSERIALIZE operator will be identical to the original [xString] representation. Certain transformations can occur as part of XMLSERIALIZE processing, and those transformations are defined in the SQL/XML specification. In some cases the result of XMLSERIALIZE might actually be the same as the original textual representation, but that is not guaranteed.

When an XMLSERIALIZE operator is specified as part of the top-level result set for a query, the result can be accessed from JDBC by using whatever JDBC getXXX methods are allowed on the string-data-type argument that is included in the XMLSERIALIZE syntax. If you attempt to select the contents of an XML value from a top-level result set without using the XMLSERIALIZE operator, Derby throws an error. Derby does not implicitly serialize XML values.

Syntax

XMLSERIALIZE ( xml-value-expression AS string-data-type )
xml-value-expression
Can be any Derby XML value, including an XML result sequence generated by the XMLQUERY operator. The xml-value-expression argument cannot be a parameter.

string-data-type
Must be a SQL character string type, such as CHAR, VARCHAR, LONG VARCHAR, or CLOB. If you specify a type that is not a valid character string type, Derby throws an error.

Examples

In the x_table table, to display the contents of the xcol XML column, use this statement:
SELECT ID, 
    XMLSERIALIZE(
    xcol AS CLOB) 
FROM x_table
To retrieve the results from JDBC, you can use the JDBC getCharacterStream() or getString() method.
To display the results of an XMLQUERY operation, use the following statement:
SELECT ID, 
    XMLSERIALIZE(
        XMLQUERY('//student[@age>20]' 
           PASSING BY REF xcol EMPTY ON EMPTY)
    AS VARCHAR(50))
FROM x_table

Usage note

Derby requires that a JAXP parser (such as Apache Xerces) and Apache Xalan are listed in the Java classpath for the XML functions to work. If either the JAXP parser or Xalan is missing from the classpath, attempts to use the XMLSERIALIZE operator will result in an error. In some situations, you may need to take steps to place the parser and Xalan in your classpath. See "XML data types and operators" in the Derby Developer's Guide for details.