Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:33:42+00:00 2026-05-23T09:33:42+00:00

I have an xml structure that looks like this: <data> <a> <element name=aName>123</element> <element

  • 0

I have an xml structure that looks like this:

<data>
  <a>
    <element name="aName">123</element>
    <element name="xyz">foobar</element>
  </a>
  <b>
    <element name="aName">foo</element>
    <element name="otherName">bar</element>
  </b>
</data>

Which is saved in an XML column in a DB2 database. Within /a/element/@name is unique and /b/element/@name is unique etc.

Now I want to insert a new element tag in either ,. If an element with the same name attribute already exists, I want to replace that element (order of element tags do not matter). I did this like that:

update mytable set myXmlColumn=xmlquery('copy $new := $old modify
  (
    do delete $new/data/a/element[@name=$newName],
    do insert $insert as last into $new/data/a
  ) return $new'
  passing
    'aName' as "newName",
    xmlparse(document '<element name="aName">aaa</element>') as "insert"),
    myXmlColumn as "old"
) where id=123

This inserts the new element all right, removing another element with the same name if it existed. But know I want this to work even if the tag does not exist yet (in this case an empty tag should be created and the new element should be added to it) and it should still work if myXmlColumn was previously null.

For the latter requirement I found a solution although it strikes me as rather inelegant: In the passing section I can write coalesce(myXmlColumn,xmlparse(document '<data><a /></data>')), which will make the statement work if myXmlColumn is null. But what if it is not null, but just the is missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:33:43+00:00Added an answer on May 23, 2026 at 9:33 am

    I found a solution that works, although it still feels awkward. I wonder if there is a more elegant solution…

    update mytable set myXmlColumn=xmlquery('copy $xml := (if (not($xml/data/a)) then transform copy $xml := $xml modify (do insert <a/> as last into $xml/data) return $xml else $xml) modify
      (
        do delete $xml/data/a/element[@name=$newName],
        do insert $insert as last into $xml/data/a
      ) return $xml'
      passing
        'aName' as "newName",
        xmlparse(document '<element name="aName">aaa</element>') as "insert"),
        COALESCE(myXmlColumn, xmlparse(document '<data/>')) as "xml"
    ) where id=123
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a XML Structure that looks like this. <sales> <item name=Games sku=MIC28306200 iCat=28
I have a piece of XML that is structured similar to this: <root> <score
I have a huge bunch of XML files with the following structure: <Stuff1> <Content>someContent</name>
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following sample XML structure: <SavingAccounts> <SavingAccount> <ServiceOnline>yes</ServiceOnline> <ServiceViaPhone>no</ServiceViaPhone> </SavingAccount> <SavingAccount> <ServiceOnline>no</ServiceOnline>
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
Here's my problem.I have 2 xmlfiles with identical structure, with the second xml containing
I have xml where some of the element values are unicode characters. Is it
I have XML files in a directory that I wish to get over to
I got a XML-document with several values, this is part of it: <result> <element>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.