Question

XQuery using for loops

  • 22 January 2020
  • 7 replies
  • 39 views

Badge +1

Hi,

I have achieved the below XML using the following xquery in an XMLUpdater:

 

XML:

 

<base>

 

<level id="Level-1"/>

 

<group id="A" category="line">

 

<section id="A1">

 

<definition>

 

<edges>

 

<edge id="110992"/>

 

</edges>

 

</definition>

 

</section>

 

<section id="A2"/>

 

<section id="A3"/>

 

<section id="A4"/>

 

</group>

 

</base>

<base>

 

<level id="Level-1"/>

 

<group id="B" category="line">

 

<section id="B1">

 

<definition>

 

<edges>

 

<edge id="128677"/>

 

</edges>

 

</definition>

 

</section>

 

<section id="B2"/>

 

<section id="B3"/>

 

<section id="B4"/>

 

<section id="B5"/>

 

</group>

 

</base>

 

XMLUpdater

Update Type: Replace Contents

XML Path: /base/group/section[@id=fme:get-attribute("CODE")]

Value:

<definition>

 

<edges>{

 

for $edge_id at $i in fme:get-list-attribute("_list2{}.EDGE_ID")

 

return

 

<edge id="{$edge)_id[$i]}" />

 

}</edges>

 

</definition>

 

However as you can see in the XML output, only the first 'edge id' attributes in each group have been updated. I don't understand why the list of EDGE_ID values which I passed through the for loop in the XMLUpdater didn't work. _list2 was created by using a ListBuilder on the CODE attribute.

Any ideas?? I am very new to XQuery..

 

Thankyou


7 replies

Userlevel 2
Badge +17

I cannot find out any wrong part in your XQuery expression, except there is an excess bracket (maybe a typo). Please quote correct XML fragments with indentations using code block like this, in order to make easier to understand the situation.

<definition>
    <edges>{
        for $edge_id at $i in fme:get-list-attribute("_list2{}.EDGE_ID")
        return
        <edge id="{$edge_id[$i]}" />
    }</edges>
</definition>

 

Make sure that the "Update" features have the list attribute "_list2{}.EDGE_ID" and also it contains multiple elements.

Badge +1

I cannot find out any wrong part in your XQuery expression, except there is an excess bracket (maybe a typo). Please quote correct XML fragments with indentations using code block like this, in order to make easier to understand the situation.

<definition>
    <edges>{
        for $edge_id at $i in fme:get-list-attribute("_list2{}.EDGE_ID")
        return
        <edge id="{$edge_id[$i]}" />
    }</edges>
</definition>

 

Make sure that the "Update" features have the list attribute "_list2{}.EDGE_ID" and also it contains multiple elements.

Hi @takashi, thanks for your reply.  I'm not exactly sure what you mean by the 'Update' features. Are you referring to the features in the 'FME Feature Attributes' list in the 'Value' dialogue box of the XMLTemplater?  If so, then the _list2{}.EDGE_ID is definitely in there.   Each record in the attribute CODE which I used to build this list (_list2{}.EDGE_ID) has a different number of list elements.  Some have just one but others have 2 or 3.  In my code output above you can see that section ID A1 has only one edge ID which is correct, however all the other section IDs (A2, A3, etc) do not have any edge ID values which is what I am trying to fix...

Userlevel 2
Badge +17

I cannot find out any wrong part in your XQuery expression, except there is an excess bracket (maybe a typo). Please quote correct XML fragments with indentations using code block like this, in order to make easier to understand the situation.

<definition>
    <edges>{
        for $edge_id at $i in fme:get-list-attribute("_list2{}.EDGE_ID")
        return
        <edge id="{$edge_id[$i]}" />
    }</edges>
</definition>

 

Make sure that the "Update" features have the list attribute "_list2{}.EDGE_ID" and also it contains multiple elements.

I meant that the features entered via the "Update" port of the XMLUpdater. 

Badge +1

I meant that the features entered via the "Update" port of the XMLUpdater.

Thanks @takashi. I'm wondering if I need to have a for loop within the XML Path: /base/group/section[@id=fme:get-attribute("CODE")] if that is even possible. I think that only the first section id is getting populated and all subsequent ones are being ignored.

Userlevel 2
Badge +17

I meant that the features entered via the "Update" port of the XMLUpdater.

No, XPath (XML Path) is not XQuery so for statement cannot be used.

Badge +1

I cannot find out any wrong part in your XQuery expression, except there is an excess bracket (maybe a typo). Please quote correct XML fragments with indentations using code block like this, in order to make easier to understand the situation.

<definition>
    <edges>{
        for $edge_id at $i in fme:get-list-attribute("_list2{}.EDGE_ID")
        return
        <edge id="{$edge_id[$i]}" />
    }</edges>
</definition>

 

Make sure that the "Update" features have the list attribute "_list2{}.EDGE_ID" and also it contains multiple elements.

I worked it out!  I had the GROUP BY in the XMLUpdater set to 'CODE'.  I removed that and then altered the 'VALUE' slightly to the following and it worked :) 

  1. <definition>
  2.     <edges>{
  3.         for $edge_id in fme:get-list-attribute("_list2{}.EDGE_ID")
  4.         return
  5.         <edge id="{$edge_id}" />
  6.     }</edges>
  7. </definition>
Userlevel 2
Badge +17

I meant that the features entered via the "Update" port of the XMLUpdater.

Can you please share your workspace and minimal sample of the source dataset?

Reply