I am trying to render some data from a large XML using XSLT. The XML data is actually kind of graph data and not hierarchical. and elements are related to each other and as such may end up havin a circular reference (the relation types are different however).
I am trying to traverse through the relationships from one element and visiting each related element and so on. In this way, at times I reach one element that I have already traversed. In such a case, I should stop traversing further as otherwise I shall be running in a cycle.
My problem is that I am not able to store the list of elements that I have already traversed and make a look up everytime I start traversing an element, so that I can stop traversing if the element is in the lookup.
Simply said, I wanted to keep the elements in a lookup table and add each element to it as I traverse.
Is there some solution for this?
A recursive template can pass itself parameters that hold a node set of “previously” processed nodes and a queue of nodes to be processed. This is a functional programming equivalent of modifying state variables.
Sample input:
XSL 2.0 stylesheet:
Output (tested):
As specified, no node is processed twice, even though the graph contains cycles.