This example shows you how to import a demo XQuery library module into MapForce and call its functions from a mapping. The demo module in this example consists of only one function which calculates tax on decimal amounts as 20% of the amount. In a production scenario, an XQuery module may contain multiple functions.
All functions declared in the XQuery module must return atomic types and their parameters must also be of atomic data types. Otherwise, the module is not eligible for import into MapForce.
You can find the demo XQuery module file at the following path relative to your personal "Documents" folder on the computer where MapForce is installed: <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\module.xq.
xquery version "1.0";
After you import the XQuery module file into MapForce, you will be able to call the demo:calculatetax function from a mapping. Note that calculating the tax amount with the help of an XQuery function is just for demo purposes—you can achieve the same result by using MapForce built-in functions.
A demo mapping which calls the demo:calculatetax function above is available at the following path: <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\CalculateTax_XQuery.mfd. When you open this mapping initially, MapForce displays a warning that it contains one or more components that are not available in XQuery. This warning is normal and it occurs because the mapping references a function from a custom XQuery library module that was not imported yet. To remove the warning and run the mapping, we will import the missing XQuery module into MapForce as shown below.
To import the XQuery module into MapForce:
|1.||Scroll down to the end of the Libraries window and click Add/Remove Libraries. The Options dialog box opens.|
|2.||Click Add and browse for the following file: <Documents>\Altova\MapForce2019\MapForceExamples\Tutorial\module.xq.|
|3.||Click OK to close the dialog box. A message box appears informing you that a new library has been added.|
The imported library and the demo:calculatetax function are now visible in the Libraries window.
Also, the mapping can now be validated and run without warnings. The demo:calculatetax function illustrated in the image below originates from the imported XQuery module, and it can be added to the mapping just like any other built-in function, see Add a Built-in Function to the Mapping.
The mapping explained
The CalculateTax_XQuery.mfd mapping illustrated above takes as input an XML file that stores articles. Each article has a single price expressed as a decimal value, for example:
The mapping produces an XML file which abides by the same schema as the source XML file. Therefore, both the source and target components have the same structure on the mapping. As the mapping connections suggest, nearly all elements are mapped in a straightforward way from target to source—for example, for each Article in the source there will be an Article in the target. The values of all items are copied verbatim from the source XML, except for SinglePrice. The value of SinglePrice is calculated with the help of two functions:
|•||The demo:calculatetax XQuery function calculates the tax amount by taking the original SinglePrice as input.|
|•||The MapForce built-in add function add the tax amount to the original SinglePrice amount and returns the final amount.|
Importantly, the data type of the SinglePrice item is xs:decimal—which corresponds to the input parameter type and the return type of the XQuery function.
The output produced by the mapping when you click the Output tab is illustrated below. Notice the increase of 20% applied to each price compared to the source XML.
© 2019 Altova GmbH