Creating an XML Document in FME with the XMLTemplater Transformer

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2023.1

Introduction

This example demonstrates how you can convert a relational dataset, such as a database table, into an XML document of varying complexity. The XMLTemplater transformer which makes this scenario relatively easy.
The XMLTemplater transformer reads one or more template files containing a mix of XML and XQuery statements. The template can be specified in a file, or within the transformer itself.
 

Step-by-step Instructions

1. Add a Microsoft  Access (JDBC) Reader
Open FME Workbench and start a blank workspace. Add a Microsoft Access (JDBC) reader and select the GNIS_MT_access.mdb file that is available from the Files section on this article. Next, open the Parameters.  
Reader.png

In the Parameters, click on the ellipsis next to Tables and select the V_WFSG table. Click OK twice to finish adding the reader to the canvas. 
Parameters.png

This database contains various locations in Montana. 

2. Add an XMLTemplater
Add an XMLTemplater to the canvas and connect it to the reader feature type. In the parameters, change the Source to File, then click on the ellipsis for template and select the iso19112-SI_LocationInstance.xml template, which is in the Resource folder. 
This template file was created using FME specific XQuery to create the definitions. For more information on XQuery, see the documentation
OtherFile.png

Next, set Write XML Header to No and set the XML Result to _name_xml, which will be used in the next template. 
Templater.png

Running the workspace now creates a unique XML element for each point of interest, but we will need to aggregate them into a single element to be used in the second template. 

3. Aggregate Elements
Add an Aggregator to the canvas and connect it to the XMLTemplator. In the parameters, enable Generate List, set the List Name to _list and set Add to List to All Attributes. Finally, set the Number of Aggregated Features to _element_count. Both the _list and _element_count  attributes will be referenced in the second template. 
Aggregatpr.png 

4. Create the Final XML File
Add another XMLTemplater to the canvas and connect it to the Aggregator. In the parameters, set te Source to File and browse to the wfs-FeatureCollection.xml file for the Template. 
This template file iterates over the aggregated list to build out the XML file. 
WFSFile.png

Next, set Write XML Header to No and set the XML Result to text_line_data, which is the required attribute name to use the Text File writer in the next step. 
Templater2.png

5. Write to XML Using the Text File Writer
We will use the Text File writer to write the XML out as it is a big text-based file that doesn’t require any additional parameters as it is already contained within the file schema. 
Add a Text File writer to the canvas and browse to a location to save the dataset. Name the file state_of_montana-wfs-FeatureCollection.xml. Depending on your operating system, you may need to change the Save As File Type to All Files (*) to prevent a .txt extension from being appended to the filename. Click OK to finish adding the writer. 
Writer.png

6. Run Workspace
Connect the writer feature type to the XMLTemplater_2, then run the workspace. 
Workspace.png


At runtime the first XMLTemplater transformer reads template #1 and processes it using the features and attributes sent to it. The result is an attribute containing the XML snippet. These XML snippet attributes are then used by the next XMLTemplater transformer to continue building the XML output using template #2.
Output.png

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.