XQuery/Transformation Styles

MotivationEdit

You have documents that you want to transform from one format to another. You would like to know what approach works best for your situation.

MethodsEdit

There are several different methods or styles of transformation that you can use to convert documents from one format to another. Each method has its strengths or weakness based on the type of data that you are going to transform.

  1. Template Method
  2. Filters Method
  3. Typeswitch Method
  4. XSLT Method
  5. Combination of Methods

Template MethodEdit

This method is similar to the "split" functions we used to split a large file into multiple smaller files. The general idea is to create a new in-line template and fill in the values from the old document by specifying path references to the old document values.

For example:

(: read in a document in the old format :)
let $old-format := doc('old-file.xml')/root
 
(: create a new template in the new format that you want to use :)
let $template :=
<new-format>
   <new-element>{$old-format/path-to-old-element/text()}</new-element>
   <new-element2>{$old-format/path-to-old-element2/text()}</new-element2>
</new-format>
 
(: you can comment this out when you are debugging :)
let $store := xmldb:store($new-collection, 'new-file.xml', $template)
return $template

Filters MethodEdit

This method uses a series of "node filters" that use recursion on each node of a document. The node filter approach is ideal if you are only making a few changes, such as renaming a few elements, adding a few elements or inserting a few new attributes.

The following article covers node filters: XQuery/Filtering_Nodes

Typeswitch MethodEdit

This method is ideal if you have a set of rules for transforming each element to one or more other elements. It is a bit more complex since each element can have its own transformation rule. For large documents that have a consistent but nested structure it is the most general purpose and the most similar to anyone that has use template matching rules in XSLT transforms.

XSLT MethodEdit

If you have an existing XSLT transform you can also use this in your transformation system. The problem with XSLT is that it does not use the eXist indexing system and transforms on large documents can be very slow. XSLT programs also must be carefully set up to include the correct import statements which can get very complex within an XML database. There are also few open source XSLT 2.0 software systems that support extensions such as math functions.

Combination MethodEdit

If you have many transforms you can use combinations of methods. Large systems frequently use a base typeswitch library and then add customization using filters.

Last modified on 28 January 2014, at 14:46