I have a xml as below that I’d like to copy n times while incrementing one of its element and one of its attribute.
XML input:
<Person position=1>
<name>John</name>
<number>1</number>
<number>1</number>
</Person>
and I’d like something like below with the number of increment to be a variable.
XML output:
<Person position=1>
<name>John</name>
<number>1</number>
</Person>
<Person position=2>
<name>John</name>
<number>2</number>
</Person>
....
<Person position=n>
<name>John</name>
<number>n</number>
</Person>
Any clue
I. XSLT 1.0 solution:
When this transformation is applied on the following XML document:
the wanted result is produced:
II. XSLT 2.0 solution:
III. Using a DVD-style recursion to avoid stack overflow on large N (XSLT 1.0)
The problem with deep recursion is the exhaustion of the call stack resulting in stack overflow. On different systems this happens with different levels of nesting, but this is typical at around N = 1000.
The above transformation practically has no such problem. The maximum recursion depth with DVC is log2(N), which means that if we need to repeat the action 1000000 (1M) times, the maximum recursion depth is only 19.
IV. Finally, how to avoid recursion at all (XSLT 1.0)
For known, not too big N, the following non-recursive technique works well: