postgres - 修改xml(MySql UpdateXml替代)

时间:2013-10-15 08:25:40

标签: mysql sql xml postgresql

我正在将mysql数据库迁移到postgres并遇到一些关于一些基本xml功能的障碍。在MySql中,我有一些存储过程可以替换xml文档中的节点,但在postgres中无法找到任何方法。

这是我从mysql存储的proc:

CREATE DEFINER=`root`@`localhost` PROCEDURE `SP_UpdateExamFilesXmlNode`(examFileId int, xPathExpression varchar(128), xmlNode longtext)
BEGIN
    DECLARE xmlData longtext;
    DECLARE newXmlData longtext;
    DECLARE xmlNodeCount int;
    SET xmlData = NULL;
    SELECT xml_data INTO xmlData FROM sonixhub.exam_files WHERE id = examFileId;

    IF xmlData IS NOT NULL THEN
        -- check if the node already exists and if it does then simply update it
        SET xmlNodeCount = ExtractValue(xmlData, CONCAT('count(',xPathExpression,')'));
        IF xmlNodeCount > 0 THEN
            SET newXmlData = UpdateXML(xmlData, xPathExpression, xmlNode);
        -- if node doesn't exist then we have to add it manually
        ELSE
            SET newXmlData = REPLACE(xmlData, '</ImageXmlData>', CONCAT(xmlNode, '</ImageXmlData>'));
        END IF;
        UPDATE sonixhub.exam_files SET xml_data = newXmlData WHERE id = examFileId;
    ELSE
        -- there is no xml found so create xml from scratch and insert the node
        SET xmlData = CONCAT('<ImageXmlData>',xmlNode,'</ImageXmlData>');
        UPDATE sonixhub.exam_files SET xml_data = xmlData WHERE id = examFileId;
    END IF;
END

有没有办法在postgres函数中复制这个功能,而不是将逻辑移动到应用程序本身?

编辑 - 找到我的问题的解决方案

我找到了一个使用postgres xml和字符串格式化函数混合的解决方案。

examFileId用于查找要使用xml更新的行,使用表信息更改代码  在我的情况下是硬编码的根节点,但您可以将其更改为您喜欢的任何内容。

以下是您调用该函数的方法:

-- this adds <DicomTags> node to your xml value in the table, if <DicomTags> already exists then it's replaced by the one passed in
select update_exam_files_xml_node(1, '/ImageXmlData/DicomTags', '<DicomTags><DicomTag>xxx</DicomTag></DicomTags>');

-- this adds <Settings> node to your xml value in the table, if <Settings> already exists then it's replaced by the one passed in
select update_exam_files_xml_node(1, '/ImageXmlData/Settings', '<Settings>asdf</Settings>');


CREATE OR REPLACE FUNCTION update_exam_files_xml_node(examFileId int, xPathExpression text, xmlNode text)
  RETURNS void AS
$BODY$
    DECLARE xmlData xml;
    DECLARE newXmlData xml;
    DECLARE xmlNodeCount int;
    DECLARE replaceTag text;
BEGIN
    SELECT xml_data INTO xmlData FROM exam_files WHERE id = examFileId;

    IF xml_is_well_formed(xmlNode) = false THEN
        PERFORM add_error_log('update_exam_files_xml_node', 'xmlNode is not well formed xml');
        RETURN;
    END IF;

    IF xmlData IS NOT NULL THEN
        -- check if the node already exists and if it does then simply update it
        IF xmlexists(xPathExpression PASSING BY REF xml(xmlData)) = true THEN
            -- get the node name
            replaceTag := regexp_replace(xPathExpression, '/.*/', '');

            -- replace the existing node with the newly passed in node
            newXmlData := xml(regexp_replace(xmlData::text, '<'||replaceTag||'>.*</'||replaceTag||'>', xmlNode));

        -- if node doesn't exist then we have to add it manually
        ELSE
            newXmlData := xml(REPLACE(xmlData::text, '</ImageXmlData>', xmlNode||'</ImageXmlData>'));
        END IF;
        UPDATE exam_files SET xml_data = newXmlData WHERE id = examFileId;
    ELSE
        -- there is no xml found so create xml from scratch and insert the node
        xmlData := '<ImageXmlData>'||xmlNode||'</ImageXmlData>';
        UPDATE exam_files SET xml_data = xmlData WHERE id = examFileId;
    END IF;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

1 个答案:

答案 0 :(得分:0)

很高兴你有一个解决方案。老实说,由于与语言层次结构有关的问题,字符串格式化函数在SGML中可能有点难以可靠地使用。即正则表达式对他们能做什么有严格的限制。

更好的解决方案可能是采用非常不同的方向,并在PL / PerlU或PL / Python中编写函数,并使用现有的XML处理功能来处理这些语言。这可能会为您提供更好,更强大的解决方案。