/**
 * xmlSecReplaceNodeBuffer:
 * @node:       the current node.
 * @buffer:         the XML data.
 * @size:       the XML data size.
 * 
 * Swaps the @node and the parsed XML data from the @buffer in the XML tree.
 *
 * Returns 0 on success or a negative value if an error occurs.
 */
int
xmlSecReplaceNodeBuffer(xmlNodePtr node, 
                        const xmlSecByte *buffer, xmlSecSize size) 
{
    xmlNodePtr results = NULL;
    xmlSecAssert2(node != NULL, -1);
    xmlSecAssert2(node->parent != NULL, -1);

    if (xmlParseInNodeContext(node->parent, buffer, size, 0, &results) == XML_ERR_OK) {
        xmlNodePtr next = NULL;
        while (results != NULL) {
            next = results->next;
            xmlAddPrevSibling(node, results);
            results = next;
        }

        xmlUnlinkNode(node);
        xmlFreeNode(node);  
    } 
    else {
        xmlSecError(XMLSEC_ERRORS_HERE,
                    NULL,
                    "xmlParseInNodeContext",
                    XMLSEC_ERRORS_R_XML_FAILED,
                    "Failed to parse content");
        return(-1);         
    }
    return(0);
}
