vagelis
vagelis

Reputation: 464

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?

Upvotes: 1

Views: 306

Answers (1)

Christian Gr&#252;n
Christian Gr&#252;n

Reputation: 6229

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')]

Upvotes: 1

Related Questions