How can we create a node set from values….
I have n numbers 1,2,3…….n.
I want to create a node set
<MYNMUMS>
<MYNUM>1</MYNUM>
<MYNUM>2</MYNUM>
<MYNUM>3</MYNUM>
<MYNUM>4</MYNUM>
....
<MYNUM>N</MYNUM>
</MYNMUMS>
divide-and-conquerxslt
How can we create a node set from values….
I have n numbers 1,2,3…….n.
I want to create a node set
<MYNMUMS>
<MYNUM>1</MYNUM>
<MYNUM>2</MYNUM>
<MYNUM>3</MYNUM>
<MYNUM>4</MYNUM>
....
<MYNUM>N</MYNUM>
</MYNMUMS>
Best Answer
As easy as that:
XSLT 1.0 solution:
This transformation:
when applied on any XML document (not used), produces the desired output:
Do note the following:
The template
generateNumNodes
calls itself recursively.This recursion is both time
( O(N) )
, and space( O(log2(N)) )
efficient and practically does overflow the stack -- no SO here!The above feature is achieved by implementing the recursion in a DVC (
DiVide and Conquer
) style.Unlike
tail-recursion
it will be successfully executed on any compliant XSLT processor.The maximum recursion depth needed to generate 1000000 (one million numbers) is just 19.
XSLT 2.0 solution:
Even more elementary, no recursion, just using the XPath 2.0
to
operator: