XMLFlattener
Flattens content of XML element(s) into feature attributes.
This transformer provides a quick and easy way to output any content or attributes of an XML element as feature attributes by specifying the element name or path. Optionally, the XML attributes from the ancestors of the specified elements can be fetched as FME attributes as well.
The XMLFlattener does not currently support multiple matches, so for generation of multiple features from the source XML, please use the XMLFragmenter.
Input Ports
Input features that contain the XML document information.
Output Ports
Each feature will contain attributes that are flattened from the XML element(s).
Parameters
XML Source
Selecting from the list enables that selection's corresponding parameter:
- XML File: enables the XML File field.
- XML Attribute: enables the XML Attribute field.
Flatten Paths
This parameter specifies which elements to flatten. The Flatten Paths are whitespace-separated xfMap match expressions or each expression can be specified on a new line. For more information, see the FME Readers/Writers manual: XML (Extensible Markup Language) Reader/Writer > xfMap.
Type directly in the text box or click the browse button to display the editor or select Set to Attribute Value to pick a feature attribute containing the element expressions.
The Feature Paths are whitespace-separated xfMap match expressions. For more information, see the FME Readers/Writers manual: XML (Extensible Markup Language) Reader/Writer > xfMap.
Example: input.xml
<shiporder> <orderperson>John Smith</orderperson> <shipto> <name>Ola Nordmann</name> <address>Langgt 23</address> <city>4000 Stavanger</city> <country>Norway</country> </shipto> <item id="1" status=”pending”> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item id="2" status=”shipped”> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder>
This table lists some Flatten Paths xfMap expressions shown in the above example.
"orderperson" | Add the text content of orderperson element into orderperson feature attribute |
"shipto" | Add the text content of the children of shipto element into the following feature attributes: name, address, city and country. |
“item[@id=1]” or equivalently <item id=“1”> | This matches the element in the document named item whose attribute id is equal to 1. It adds the text content of the children as feature attributes: title, note, quantity and price. |
“orderperson shipto <item id=“1”>” | The three previous matched expressions combined, each separated by newline. |
Customize Attributes
This parameter can be specified to extract the children of the matched elements as xml fragments.
For example, If Elements to Match is shipto and Elements As XML Fragments is set to country , then the output feature has an extra attribute "xml_fragment_country{0}" with its value set to "<country>Norway</country>"
The Options button opens the XML Flatten Options dialog. These options control how the content of the elements are to be mapped as feature attributes.
The default view is Basic mode, where several options are listed:
- Enable Flattening is selected by default.
- Ignore Specific Sub-Elements > Sub-Elements To Ignore: Specify the children of the matched elements (from Flatten Paths) that should be ignored. In the example above, if Flatten Paths is “shipto”, then if this option is set to “country city” then both country and city contents are ignored in the output.
- Skip Empty Elements : specify whether empty elements should be mapped as empty feature attributes
- Add Custom Prefix > Prefix: specify the prefix for all the feature attributes that will be added from the flattened XML structure
- Include XML Child Position > Attribute Name: specify the attribute name whose value will be the position of the child element within its parent.
- Add Ancestor Attributes > Ancestor Element(s) : specify the parent element(s) of the elements in Flatten Path whose XML attributes will also be added as feature attributes in the output.
The Advanced button opens the Advanced Editor, which provides additional options for customizing the feature attributes. The functionality of each option is described in the table below. The options here allow customization of the attributes and attribute lists of the matched XML subtree that will be added to FME Features.
Option (with example value) | Description | Default Value | Possible Values |
---|---|---|---|
separator="." |
This value of this option is used as the separator in the naming of the attributes of the children of the matched elements. |
period (.) |
any string |
open-list-brace="{" |
Specifies the open list index delimiter brace for the flattened attributes. |
Default value if not specified: { |
any string |
close-list-brace="}" |
Specifies the close list index delimiter brace for the flattened attributes. |
Default value if not specified: } |
any string |
map-empty-elements="yes" |
If set to yes, any empty elements will be added onto the features as attributes with empty values. Otherwise, the attributes will not be added onto the features. |
Default value if not specified: yes |
yes | no Possible values: any string |
matched-prefix="attributes" |
This option controls whether FME feature attributes produced will be prefixed with matched element's name. If the value is yes, then both the matched element's attributes and all its children are prefixed If the value is no, then none of the feature attributes are prefixed with matched element's name If the value is children, then only the children of the matched element are prefixed If the value is attributes, then only the attributes of the matched element are prefixed. |
Default value if not specified: yes |
yes | no | children | attributes |
matched-attributes="yes" |
If this option is set to yes then the attributes of the matched element are mapped as FME feature attributes. Otherwise, the attributes of the matched element are ignored. matched-prefix option can also be set to attributes or yes to allow the attributes to be prefixed with the name of the matched element. |
Default value if not specified: yes |
yes | no |
matched-ancestor-attributes="" |
The option controls whether XML attributes from ancestor of the matched element should be included as FME feature attributes. - 'parent' or '1' : XML attributes of the parent of the matched element are added - 'grandparent' or '2' : XML attributes of the grandparent of the matched element are added - 'root' or '-1' : XML attributes of the root of the document are added - any non-negative number : XML attributes of the ancestor by going up x levels from the matched element are added. 0 is the matched element. To include more than one ancestor, multiple values can be separated by a space. For example: To get the attributes from root, parent and grand parent, we can specify matched-ancestor-attributes="parent grandparent root" |
parent | grandparent | root |
|
cardinality="+{?}" |
This option can be specified as a space separated list of cardinality directives. |
Default value: +{?} (Treat child elements as a list if there are more than one with the same name) |
Possible values: Refer to the xfMap section in the XML Reader documentation. |
except="" |
The except attribute accepts the same types of expressions as the match or except attribute of a mapping rule. For example, the expression except="parent/child{2}" could be used to exclude the second <child> element contained in a <parent> element from the output of the structure subrule. |
any path expression |
|
structure-prefix="" |
This option can be set to non-empty string that serves as a prefix to every attribute that is generated for a matched element. |
any string |
|
child-position-attribute="" |
When this option is set to non-empty string, each child element will generate an additional feature attribute whose value will be the position of the child element within its parent. |
any string |
|
attribute-identifier="" |
XML attributes can be differentiated from leaf elements. By setting this option to a non-empty string, the XML reader will append a prefix to the attributes in the leaf elements. |
All the options have more detailed examples and descriptions in the FME Readers/Writers manual: XML (Extensible Markup Language) Reader/Writer.
Expose Attributes
Exposes any attributes so they can be used by other transformers. Type directly in the text box or click the browse button to display the editor and add attributes there.
Editing Transformer Parameters
Using a set of menu options, transformer parameters can be assigned by referencing other elements in the workspace. More advanced functions, such as an advanced editor and an arithmetic editor, are also available in some transformers. To access a menu of these options, click beside the applicable parameter. For more information, see Transformer Parameter Menu Options.
Transformer Categories
FME Licensing Level
FME Professional edition and above
Technical History
FME Factory Used: XFMapFactory
Search FME Community
Search for samples and information about this transformer on the FME Community.