Question

XQuery: replace element value with counter


Badge +1
Hi all,

 

 

I'm getting stucked.

 

Following problem is to be solved (hope the schematic sample will describe it):

 

 

inpu xml:

 

<greatgrandfather> <grandfather> <father> <child>foo</child> </father> <father> <child>boo</child> </father> </grandfather>

 

<grandfather> <father> <child>doo</child> </father> </grandfather> </greatgrandfather>

 

 

expected output xml:

 

<greatgrandfather> <grandfather> <father> <child>0</child> </father> <father> <child>1</child> </father> </grandfather> <grandfather> <father> <child>2</child> </father> </grandfather> </greatgrandfather>

 

 

goal

 

My goal is to replace the child's value with their index appearance within this xml, kind of counter.

 

 

 

What I already did:

 

I took the transformer XMLUpdater with

 

UpdateType = Replace contents

 

XML Path = greatgrandfather/grandfather/father/child XQuery = let $count := 0 for $elem in //child let $count := $count +1 return $count  

 

This gives me back

 

<greatgrandfather> <grandfather> <father> <child>1 1 1</child> </father> <father> <child>1 1 1</child> </father> </grandfather> <grandfather> <father> <child>1 1 1</child> </father> </grandfather> </greatgrandfather>

 

 

 

I don't see the wood for the trees. Can anybody help me?

 

 

 

Thanks in advance!

2 replies

Badge +3
hi,

 

 

I think you should count the father where child>0 ; elem in father

 

also i think it will never be 0. There appeaar to be no childless father in your example... ;)
Badge +1
Thanks for the quick answer.

 

 

Well, I found a better solution:

 

XQueryUpdater:

 

for $elem at $d in //SymbolInstance

 

return replace value of node $elem with $d

Reply