Solved

XML read child as list

  • 1 October 2018
  • 4 replies
  • 44 views

Userlevel 3
Badge +18

I'm trying to convert an XML file to the data I need.

Reading the file as text_line_data gives better results as reading it as XML. Probably because FME does not recognize the schema.

The problem I encounter is that sometimes the attribute I need is a single child attribute. But sometimes it exists multiple times.

Using an XMLFlattener and exposing the child element gives an attribute when it exists 1 time. But with multiple occurrences I need to use a ListExploder to create the attribute.

I now use a Tester to test if the child attribute is empty and then reroute the data to ListExploder. But I doubt there is a simple option I don't see?

icon

Best answer by jdh 1 October 2018, 18:25

View original

4 replies

Badge +22

In the XMLFlattener you can force the cardinality of the child element attribute to always be a list in the advance options by changing it from "+{?}" to "{}".

 

That way you can process all features the same, regardless of whether there is one or more child elements.

 

 

That said, if you want one feature per child element, why don't you use that as the Element to match on either the xml reader, or XMLFragmenter, adding Ancestor Attributes if needed.
Userlevel 2
Badge +17

In the XMLFlattener you can force the cardinality of the child element attribute to always be a list in the advance options by changing it from "+{?}"  to "{}".

 

That way you can process all features the same, regardless of whether there is one or more child elements.

 

 

That said,  if you want one feature per child element, why don't you use that as the Element to match  on either the xml reader, or XMLFragmenter, adding Ancestor Attributes if needed. 
To add to @jdh's suggestion, if there are other child elements in the actual XML document and you need to create list attribute(s) only from specific element(s), you can explicitly specify the XML path(s) of the element(s) in the cardinality setting.

 

For example, the XML Flatten Option (Advanced) with this cardinality setting would read "parent/child" as a list attribute but all other elements as non-list attributes.

 

cardinality="parent/child{} +"
See here to learn more about the "cardinarity":

 

XML Reader/Writer > xfMap > Feature Mapping Rules > Structure Element

 

 

Userlevel 3
Badge +18
To add to @jdh's suggestion, if there are other child elements in the actual XML document and you need to create list attribute(s) only from specific element(s), you can explicitly specify the XML path(s) of the element(s) in the cardinality setting.

 

For example, the XML Flatten Option (Advanced) with this cardinality setting would read "parent/child" as a list attribute but all other elements as non-list attributes.

 

cardinality="parent/child{} +"
See here to learn more about the "cardinarity":

 

XML Reader/Writer > xfMap > Feature Mapping Rules > Structure Element

 

 

Thanks for the addition.

 

 

It seems like I need to use a Specific XML transformer for each parameter I want to use in my Workbench. Especially when I only know what to expect based on history and not based on specification.

 

 

Userlevel 2
Badge +17

In the XMLFlattener you can force the cardinality of the child element attribute to always be a list in the advance options by changing it from "+{?}" to "{}".

 

That way you can process all features the same, regardless of whether there is one or more child elements.

 

 

That said, if you want one feature per child element, why don't you use that as the Element to match on either the xml reader, or XMLFragmenter, adding Ancestor Attributes if needed.
If you need to extract only some specific elements as list attributes, the XMLXQeuryExtractor could also be effective.

 

Example: xml-xquery-extractor-example.zip (FME 2018.1.0.2)

 

 

Reply