Solved

Expresion with function in XMLTemplater for recursive loop for nested xml?


Badge

I have a simple list n=74 codes which I read using the regular ROOT and SUB template expression: {fme:get-attribute("code")} but because of a nested comparison, they are nested as shown below. I get errors for the missing closing tags (</inv:And>) and If I pre-fill the closing tags, I get an error because they do not match the opening tags.

This is perfect for a recursive loop, but a function would be needed within the xmlTemplater? or a script? or 2 loops one for the top half and another for the bottom half? Below is the end result (partial code) of the desired end result. I can manually add the last code <code>code n</code>. The rest is symmetrical. FME desktop 2018.

Addition: terribly sorry I did not mention that code is a string.

icon

Best answer by takashi 31 May 2019, 17:03

View original

4 replies

Userlevel 2
Badge +17

If you have n input features with "code" attribute containing 1-n, this workflow might help you. Assuming that the code of the first feature is 1.

0684Q00000ArKRlQAN.png

XML Template Expression (common to both Root and SUB)

declare namespace inv="replace/this/with/actual/namespace.name";
let $i := xs:integer(fme:get-attribute("code"))
return
<inv:And>
    <code>code {$i}</code>
    {fme:process-features("SUB", "code", $i+1)}
</inv:And>
Badge

@takashi

Dear Mr. Takashi, thank you for your advise. I am sorry I did not clarify that code is a string, but, I added a num column to the data and tried what is below in both Root and SUB expressions. I get the error below, and cannot see why as it is declared. So with num as an integer (1 to 74), code as a string, e.g, 333-22-2,5, and both columns in a spreadsheet, the following expression

declare namespace inv="<http://etc, etc/inv>";

let $i := xs:integer(fme:get-attribute("num"))

let $code := xs:string(fme:get-attribute("code"))

return

<inv:And>

<inv:code>{$code}</inv:code>

{fme:process-features("SUB","code",$i+1)}

</inv:And>

gives me these error messages:

XMLTemplater_2(XMLTemplaterFactory): "i": undefined undeclared variable

XMLTemplater_2(XMLTemplaterFactory): An error occurred while parsing the ROOT template..

Userlevel 2
Badge +17

If I understand your requirement correctly, this expression would work as desired.

declare namespace inv="replace/this/with/actual/namespace.name";
let $i := xs:integer(fme:get-attribute("num"))
return
<inv:And>
    <inv:code>{fme:get-attribute("code")}</inv:code>
    {fme:process-features("SUB", "num", $i+1)}
</inv:And>
Badge

If I understand your requirement correctly, this expression would work as desired.

declare namespace inv="replace/this/with/actual/namespace.name";
let $i := xs:integer(fme:get-attribute("num"))
return
<inv:And>
    <inv:code>{fme:get-attribute("code")}</inv:code>
    {fme:process-features("SUB", "num", $i+1)}
</inv:And>

I worked, thank you! :)

Reply