You are here: Popular FormatsXML (Extensible Markup Language) Reader/WriterxfMapReference Mapping RulesExample
Example
Consider the following XML document:
<?xml version="1.0" encoding="UTF-8"?> <zones> <class> <code v="HP">Historic Park</code> <code v="NE">Natural Environment Park</code> <code v="NP">National Park</code> <code v="PA">Protected Area</code> </class> <zone name="A-1" class="NE"/> <zone name="B-3" class="PA"/> <zone name="D-H" class="HP"/> </zones>
The following xfMap maps each <zone> element into an FME feature. A class attribute is also added to the feature, but we use the class’s long descriptive name rather than class’s code as its value:
<?xml version="1.0" encoding="UTF-8"?> <xfMap> <reference-map> <!-- construct an empty reference-set for other references-sets to persist in. This reference-set will only be destroyed when the </zones> end tag is read --> <mapping match="zones"> </mapping> </reference-map> <reference-content-map> <!-- For each <code> element built a reference-set with one reference belonging to the “codes” reference group, the name of the reference will be the code id, the value in the “v” attribute, and the reference value is the descriptive name of the code. We also make this reference-set persist, it will persist in the parent reference-set, in this case, this is the reference-set that was constructed when the <zones> element got matched. --> <mapping match="class/code"> <references persist="true"> <reference group-name="codes"> <name><extract expr="@v"/></name> <value><extract expr="."/></value> </reference> </references> </mapping> </reference-content-map> <feature-map> <mapping match="zone"> <feature-type><literal expr="zone"/></feature-type> <attributes> <attribute> <name><literal expr="name"/></name> <value><extract expr="@name"/></value> </attribute> <attribute> <name><literal expr="class"/></name> <value> <refexpr expr="" reference-group="codes"> <arg><extract expr="@class"/></arg> </refexpr> </value> </attribute> </attributes> </mapping> </feature-map> </xfMap>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Feature Type: `zone' Attribute(string): `class' has value `Natural Environment Park' Attribute(string): `name' has value `A-1' Attribute(string): `xml_type' has value `xml_no_geom' Geometry Type: Unknown (0) =============================================================== +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Feature Type: `zone' Attribute(string): `class' has value `Protected Area' Attribute(string): `name' has value `B-3' Attribute(string): `xml_type' has value `xml_no_geom' Geometry Type: Unknown (0) =============================================================== +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Feature Type: `zone' Attribute(string): `class' has value `Historic Park' Attribute(string): `name' has value `D-H' Attribute(string): `xml_type' has value `xml_no_geom' Geometry Type: Unknown (0) ===============================================================