background image
<< RETURNING SEQUENCE | YEAR function >>

XMLSERIALIZE operator

<< RETURNING SEQUENCE | YEAR function >>
Derby Reference Manual
149
SELECT ID,
XMLSERIALIZE(
XMLQUERY('//student[@age>20]' PASSING BY REF xcol EMPTY ON EMPTY)
AS VARCHAR(50))
FROM x_table
The result set for this query contains a row for every row in
x_table
, regardless of
whether or not the XMLQUERY operator actually returns results.
In the
x_table
table, to search the XML column
xcol
and return the ages for any
students named BC, use the following statement:
SELECT ID,
XMLSERIALIZE(
XMLQUERY('string(//student[text() = "BC"]/@age)' PASSING BY REF
xcol EMPTY ON EMPTY)
AS VARCHAR(50))
FROM x_table
WHERE
XMLEXISTS('//student[text() = "BC"]' PASSING BY REF xcol)
The result set for this query contains a row for only the rows in
x_table
that have a
student whose name is BC.
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 XMLQUERY 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.
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.