XQuery/Transformation Styles
Motivation
editYou have documents that you want to transform from one format to another. You would like to know what approach works best for your situation.
Methods
editThere 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.
- Template Method
- Filters Method
- Typeswitch Method
- XSLT Method
- Combination of Methods
Template Method
editThis 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 Method
editThis 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 Method
editThis 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 Method
editIf 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 Method
editIf you have many transforms you can use combinations of methods. Large systems frequently use a base typeswitch library and then add customization using filters.