MySQL to update an XML attribute

前端 未结 3 1366
我在风中等你
我在风中等你 2020-12-20 02:15

In data load, it seems some XML attributes mapped incorrectly and I\'m now trying to correct this, but am struggling with MySQL\'s handling of this XML column.

I wan

相关标签:
3条回答
  • 2020-12-20 02:54

    UPDATE biblioitems SET marcxml = UpdateXML(marcxml,'datafield[@tag="520"]/subfield[@code="3"]/@code', 'code="a"') WHERE biblionumber = '220405';

    Note that the UpdateXML function requires an existing node to be found. If you want to insert an attribute, you have to replace an existing attribute with more than one. for example to insert attribute d in the x element: select updateXML('<x a="aaa" b="bbb">xxxxxx<c>cccc</c></x>', 'x/@a', 'a="aaa" d="ffffd"')

    0 讨论(0)
  • 2020-12-20 03:02

    You can specifically target the attribute you wish re rewrite with the attribute::att axis.

    Example MySQL code to verify behavior

    SELECT UpdateXML('<root><sub att="foo" xatt="bar">Content Text</sub><sec att="etc">Container</sec></root>', '/root/sub/attribute::att', 'att="something"')
    

    The result of the query will be

    <root><sub att="something" xatt="bar">Content Text</sub><sec att="etc">Container</sec></root>
    

    Remember to be specific in your XPATH query, because if multiple targets matches, nothing will be updated. (observed by testing it)

    0 讨论(0)
  • 2020-12-20 03:13

    The third argument to UpdateXML should be the new XML fragment with which to replace the portion of the document matched by the XPath given in the second argument.

    You can create the XML fragment using ExtractValue:

    UPDATE biblioitems
    SET    marcxml = UpdateXML(marcxml,
             'datafield[@tag="520"]',
             CONCAT(
               '<datafield tag="520" ind1="a" ind2="',
                  ExtractValue(marcxml, 'datafield[@tag="520"]/attribute::ind2'),
               '">',
               '  <subfield code="a">',
                 ExtractValue(marcxml, 'datafield[@tag="520"]/subfield'),
               '  </subfield>',
               '</datafield>'
             )
           )
    WHERE  biblionumber = 220405;
    

    See it on sqlfiddle.

    0 讨论(0)
提交回复
热议问题