Search code examples
regexxquerybasex

How to delete elements with regex in BaseX


I have a BaseX database that has the following format

<root>
    <node1>
        <value1>abctetabc</value1>
        <value2>...</value2>
    </node1>
    <node1>
        <value1>abctatabc</value1>
        <value2>...</value2>
    </node1>
</root>

I would like to ask about how can I delete nodes that their value includes tet. Have I to use regex?

the normal delete is executed like this

XQUERY delete root/node1[value1='abctatabc']

How can I do a search for a substring?


Solution

  • If you want to delete nodes in a database, you can use delete node root/node1[value1='abctatabc'] (see XQuery Update in the BaseX documentation). If you want to delete it without changing the original document, you can use the update keyword:

    document {
      <root>
          <node1>
              <value1>abctetabc</value1>
              <value2>...</value2>
          </node1>
          <node1>
              <value1>abctatabc</value1>
              <value2>...</value2>
          </node1>
      </root>
    } update {
      delete node root/node1[value1 = 'abctatabc']
    }
    

    Of course you can also look for substrings (via fn:contains) or use regular expressions (via fn:matches):

    delete node root/node1[matches(value1, 'abc')]