Skip to main content
Question

XQuery: replace element value with counter

  • May 12, 2014
  • 2 replies
  • 38 views

gpt_geoinfo
Contributor
Forum|alt.badge.img+2
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!
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • May 12, 2014
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... ;)

gpt_geoinfo
Contributor
Forum|alt.badge.img+2
  • Author
  • Contributor
  • May 16, 2014
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