Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

sql - MySQL to update an XML attribute

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 want to correct the XML attributes (Not Values) for all occurrences of a field (with attribute 'tag="520"') with subfield (with attribute 'code="3"'). The query below returns 0 rows affected, 1 rows found. Any clues as to how to achieve this.

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

XML Fragment included for clarity:

Original Fragment

<datafield tag="300" ind1=" " ind2=" ">
  <subfield code="f">article</subfield>
</datafield>
<datafield tag="520" ind1=" " ind2=" ">
  <subfield code="3">A description of something here</subfield>
</datafield>
<datafield tag="655" ind1=" " ind2=" ">
  <subfield code="a"></subfield>
</datafield>

What I want as the result:

<datafield tag="300" ind1=" " ind2=" ">
  <subfield code="f">article</subfield>
</datafield>
<datafield tag="520" ind1="3" ind2=" ">
  <subfield code="a">A description of something here</subfield>
</datafield>
<datafield tag="655" ind1=" " ind2=" ">
  <subfield code="a"></subfield>
</datafield>

Couldn't work out how to highlight the change in a code block (it's the ind1 attribute in the tag="520" datafield and it's associated subfield attributes)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...