datatemplate processing algorithm input: - datatemplate element t - data tree rooted at node d - element for which processing is happening, e . do the magic to save the contents of e. . create a list with form previousNodes[Node, namespace:string, tagName:string, templateIdentifier:string]. . for each node N with N.tagName, N.dataNode, N.templateIdentifier: add N to previousNodes[N.dataNode, N.namespace, N.tagName, N.templateIdentifier] . remove all nodes from e. . run processDataNode(d, '', e) processDataNode(dataNode, mode, destination): . find the first matching rule for dataNode with mode mode. if no rules match, assume a template that just consists of a element. . for each node templateNode that is a top-level child of this condition's template: run processTemplateNode(dataNode, templateNode, destination) processTemplateNode(dataNode, templateNode, destination) . if templateNode is a 'nest' element: . let mode be the mode on the element. . for each child node childNode of dataNode that matches the filter on the element: . run processDataNode(childNode, mode, destination) . return . search previousNodes[] for [dataNode, templateNode.namespace, templateNode.tagName, templateNode.templateIdentifier] if you have a match previousNode: . remove previousNode from the previousNodes[] list. . remove all the children of previousNode. . move previousNode into destination. . for each attribute A on previousNode: . if templateNode has that attribute: update the previousNode[A] to match templateNode[A] . if templateNode doesn't have that attribute: remove previousNode[A] . for each attribute A on templateNode: . if previousNode doesn't have that attribute: add previousNode[A] to match templateNode[A] . let newNode be previousNode if you don't have a match previousNode: . shallow clone templateNode to cloneNode . set cloneNode.dataNode to dataNode . add cloneNode to destination. . let newNode be cloneNode . if newNode is an element or text node, perform attribute or text expansion on newNode. . for each node childTemplateNode that is a child of templateNode: . run processTemplateNode(dataNode, childTemplateNode, destination)