StoredNode previousNode = (StoredNode) oldNode.getPreviousSibling();
if (previousNode == null)
{previousNode = this;}
else
{previousNode = getLastNode(previousNode);}
final NodePath currentPath = getPath();
final NodePath oldPath = oldNode.getPath(currentPath);
DBBroker broker = null;
try {
broker = ownerDocument.getBrokerPool().get(null);
//May help getReindexRoot() to make some useful things
broker.getIndexController().setDocument(ownerDocument);
//Check if the change affects any ancestor nodes, which then need to be reindexed later
StoredNode reindexRoot = broker.getIndexController().getReindexRoot(oldNode, oldPath, false);
//Remove indexes
if (reindexRoot == null)
{reindexRoot = oldNode;}
broker.getIndexController().reindex(transaction, reindexRoot, StreamListener.REMOVE_SOME_NODES);
//TODO: fix once range index has been moved to new architecture
final StoredNode valueReindexRoot = broker.getValueIndex().getReindexRoot(this, oldPath);
broker.getValueIndex().reindex(valueReindexRoot);
//Remove the actual node data
broker.removeNode(transaction, oldNode, oldPath, null);
broker.endRemove(transaction);
newNode.nodeId = oldNode.nodeId;
//Reinsert the new node data
broker.insertNodeAfter(transaction, previousNode, newNode);
final NodePath path = newNode.getPath(currentPath);
broker.indexNode(transaction, newNode, path);
if (newNode.getNodeType() == Node.ELEMENT_NODE)
{broker.endElement(newNode, path, null);}
broker.updateNode(transaction, this, true);
//Recreate indexes on ancestor nodes