Recursive UDFs
This topic explains how to search for data in a source XML file with the help of a recursive UDF. To test the recursive UDF, you will need the following mapping: MapForceExamples\RecursiveDirectoryFilter.mfd. In the mapping below, the FilterDirectory UDF receives data from the source file Directory.xml and the simple input component SearchFor that supplies the .xml extension. After the data has been processed by the UDF, it is mapped to the target file.
The main mapping (see screenshot below) describes the general mapping layout. The way the UDF processes the data is defined separately, in the function's mapping (See UDF Implementation below).
Goal
Our goal is to list files with a .xml extension in the output while preserving the entire directory structure. The subsections below explain the mapping process in detail.
Source file
Below is an extract from the source XML file (Directory.xml) that contains information about files and directories. Note that the files in this listing have different extensions (e.g., .xml, .dtd, .sps). According to the schema (Directory.xsd), the directory element can have file children and directory children. All directory elements are recursive.
<directory name="ExampleSite">
<file name="blocks.sps" size="7473"/>
<file name="blocks.xml" size="670"/>
<file name="block_file.xml" size="992"/>
<file name="block_schema.xml" size="1170"/>
<file name="contact.xml" size="453"/>
<file name="dictionaries.xml" size="206"/>
<file name="examplesite.dtd" size="230"/>
<file name="examplesite.spp" size="1270"/>
<file name="examplesite.sps" size="20968"/>
...
<directory name="output">
<file name="examplesite1.css" size="3174"/>
<directory name="images">
<file name="blank.gif" size="88"/>
<file name="block_file.gif" size="13179"/>
<file name="block_schema.gif" size="9211"/>
<file name="nav_file.gif" size="60868"/>
<file name="nav_schema.gif" size="6002"/>
</directory>
</directory>
</directory>
UDF implementation
To see the internal implementation of the UDF, double-click its header in the main mapping. The UDF is recursive, that is, it includes a call to itself. Because it is connected to the recursive element directory, this function will be called as many times as there are nested directory elements in the source XML instance. To support recursive calls, the function must be regular.
The implementation of the UDF consists of two parts: (i) defining the files and (ii) defining the directory to be searched.
Defining files
The UDF processes the files as follows: The contains function checks whether the first string (the file name) contains the substring .xml (supplied by the simple input component SearchFor). If the function returns true, the file name with a .xml extension is written to the output.
Processing child directories
Child directories of the current directory are sent as input to the current UDF. The UDF thus iterates through all directory elements and check whether files with the .xml extension exist.
Output
When you click the Output pane, MapForce will display only files with the .xml extension (see extract below).
<directory name="ExampleSite">
<file name="blocks.xml" size="670"/>
<file name="block_file.xml" size="992"/>
<file name="block_schema.xml" size="1170"/>
<file name="contact.xml" size="453"/>
...
<directory name="output">
<directory name="images"/>
</directory>
</directory>