Learn XQuery |
Home:Learn XQuery:XQuery Examples:MySQL Examples:Processing XML using MySQL and XQuery Processing XML using MySQL and DataDirect XQuery®A partner company XYZ sends you book orders using their proprietary format:
<order> Our company's canonical XML format relies on the following structure, and it exposes the following information:
<order> While the basic format of the order XML and canonical XML is similar, there are a few differences that we'll need to address before the order can be processed. While the structural hierarchy of the XML is the same (both have The following XQuery takes care of the simple format changes we need to implement. Note that we have decided to reference the incoming order as an external variable so that we can bind the right content dynamically as part of our application:
declare variable $order as document-node(element(*, xs:untyped)) Now that the formatting discrepancies have been addressed, how do we add the missing information about title, publisher and publishing date? Our company relies on MySQL database to keep detailed information about all the books available in inventory. In particular, the "books_dbo" schemata (in MySQL terms) contains a "booksxml" table that contains all the details about the books in inventory that we need. Let's look at this table in the MySQL Query Browser:
DataDirect XQuery is able to access MySQL schemata and tables. And it’s able to do that in a performant and scalable way, as described in more details in a DataDirect white paper. For example, you can access data from the “booksxml” table for a specific ISBN number this way:
for $booksxml in collection("booksxml")/booksxml In this case, the result of this XQuery is:
<booksxml> This result is the XML representation of one specific row in the MySQL "booksxml" database table. If you are curious about how DataDirect XQuery® is retrieving that result, take a look at the query plan that DataDirect XQuery is actually executing:
(To learn more about query plan in DataDirect XQuery®, visit us here.) As you can see, the selection of the matching ISBN number is entirely pushed as SQL query to the database; the XQuery engine “only” does the job of rendering the returned information as XML. In our case, however, we want to do something a bit more complicated: We want to join data available in the incoming XML order with data available in the MySQL database, and we want to return an XML document consistent with our canonical format. Merging together the two XQueries described above, we can accomplish this quite easily:
declare variable $order as document-node(element(*, xs:untyped)) A single XQuery allows us to process an incoming order which uses a proprietary XML format, to augment the information available in that format with data stored in our MySQL database, and to return a result consistent with our desired XML canonical format. Executing XQuery ProgrammaticallyDataDirect XQuery® implements the XQJ (XQuery for Java API) interface, which provides an easy to use, flexible way to execute XQueries and to consume results. XQJ makes it easy for XQuery execution to become part of your Java application, or part of services exposed by your application server. If you are familiar with JDBC, you will notice many similarities between XQJ and JDBC. Take a look at the following code snip from a Java application:
... In a "real life" scenario, the result of the XQuery would probably be consumed differently, either saving it to a file/stream, consuming it as SAX or StAX events, or maybe materializing it as an in-memory XML representation (DOM, the least scalable of the options). Now we know how to merge data available in XML documents and MySQL, and how to format a result consistent with a specific XML Schema. But what if a partner company is sending us order information in something other than XML? Next MySQL Example |
|





