| # XML Replace About the XML replacement functionality.

- General Information
- Usage Examples:
  - Replace text in all entry titles and notes
  - Replace all HTTP URLs by HTTPS URLs
  - Replace group icons
  - Delete entry strings by name
  - Delete entry attachments by name extension
  - Reset background colors
  - Auto-Type:
    - Disable auto-type for entries with empty fields
    - Convert `{DELAY=` to upper-case
    - Prepend `{DELAY=50}` to all sequences without a `{DELAY=`
    - Change `{DELAY=` values
    - Remove `{DELAY=x}` from all sequences
    - Reset default sequences that contain `{DELAY=`
    - Add an auto-type association to all entries
  - Copy entry URLs into title fields
  - Copy entry titles into empty user name fields
  - Ensure first line is not empty

## General Information

XML Replace is a powerful feature that modifies a database by manipulating its XML representation.

KeePass creates a KDBX XML DOM of the current database in memory, performs the operation specified by the user (e.g. remove nodes or replace text), tries to load the modified XML, and merges the current database with the modified database.

This is a feature for experts. Use with caution!

XML Replace can be invoked via 'Tools' → 'Database Tools' → 'XML Replace'.

A few links that might be helpful for working with XPath and regular expressions:

- XPath:
  - Wikipedia: XPath
  - MSDN: XPath Reference
  - MSDN: XPath Examples
- Regular expressions:
  - MSDN: Regular Expression Language - Quick Reference

KeePass protects history entries; XML Replace cannot be used to modify these. Furthermore, any changes to database properties (database name/description, etc.) may be ignored.

## Usage Examples

Replace text in all entry titles and notes
Select nodes: | `//Entry/String[Key='Title' or Key='Notes']/Value`
Action: | Replace data
Data: | Inner text
Find what: | `TheTextToFind`
Replace with: | `TheReplacement`
Within all entry titles and notes, this replaces all occurences of `TheTextToFind` by `TheReplacement`.

Replace all HTTP URLs by HTTPS URLs
Select nodes: | `//Entry/String[Key='URL']/Value`
Action: | Replace data
Data: | Inner text
Find what: | `^http:`
Replace with: | `https:`
Regular expressions: | Activated
Within all entry URL fields, this replaces all HTTP URLs by HTTPS URLs.

Replace group icons
Select nodes: | `//Group/IconID`
Action: | Replace data
Data: | Inner text
Find what: | `^48$`
Replace with: | `36`
Regular expressions: | Activated
This assigns the ZIP package icon to all groups that currently have a closed folder as icon. All icon IDs can be found in the icon picker dialog.

Delete entry strings by name
Select nodes: | `//Entry/String[Key='TheName']`
Action: | Remove nodes
Removes all entry strings named `TheName`.

Delete entry attachments by name extension
Select nodes: | `//Entry/Binary[substring(Key, string-length(Key) - 3) = '.jpg']`
Action: | Remove nodes
Removes all entry attachments that have a name ending in '.jpg'. The '`3`' in the node selection XPath expression is the length of '`.jpg`' - 1. If you'd e.g. want to search for attachments that have a name ending in '`.abcde`', you'd need to replace the '`3`' by '`5`'.

Reset background colors
Select nodes: | `//Entry/BackgroundColor`
Action: | Remove nodes
Sets the background color of all entries to the default (transparent/alternating).

Disable auto-type for entries with empty fields
Select nodes: | `//Entry/String[(Key='UserName' or Key='Password') and Value='']/../AutoType/Enabled`
Action: | Replace data
Data: | Inner text
Find what: | `True`
Replace with: | `False`
Disables auto-type for all entries that have an empty user name field or an empty password field.

Convert `{DELAY=` to upper-case
Select nodes: | `//DefaultSequence | //KeystrokeSequence`
Action: | Replace data
Data: | Inner text
Find what: | `{DELAY=`
Replace with: | `{DELAY=`
Converts all `{DELAY=` codes within auto-type sequence overrides and associations to upper-case (by default the case sensitivity option is turned off, thus the 'Find what' text matches all cases). In KeePass 2.x, placeholders are case-insensitive. However, this XML Replace operation may be useful as preparation for the following example (which matches `{DELAY=` in a case-sensitive way).

Prepend `{DELAY=50}` to all sequences without a `{DELAY=`
Select nodes: | `(//DefaultSequence | //KeystrokeSequence)[not(contains(., '{DELAY=')) and (. != '')]`
Action: | Replace data
Data: | Inner text
Find what: | `^(.*)$`
Replace with: | `{DELAY=50}$1`
Regular expressions: | Activated
Prepends a `{DELAY=50}` to all auto-type sequence overrides and associations that do not contain any `{DELAY=` already and are not empty. Note that the node selection is case-sensitive (independent of the data case sensitivity option), thus you need to ensure that all `{DELAY=` codes are upper-case before performing this operation. This can e.g. be done using the XML Replace operation mentioned above.

Change `{DELAY=` values
Select nodes: | `//DefaultSequence | //KeystrokeSequence`
Action: | Replace data
Data: | Inner text
Find what: | `\{DELAY=[\d\s]*\}`
Replace with: | `{DELAY=50}`
Regular expressions: | Activated
Sets the values of all `{DELAY=` codes within auto-type sequence overrides and associations to 50.

Remove `{DELAY=x}` from all sequences
Select nodes: | `//DefaultSequence | //KeystrokeSequence`
Action: | Replace data
Data: | Inner text
Find what: | `\{DELAY=[\d\s]*\}`
Replace with: | (Leave empty)
Regular expressions: | Activated
Removes all `{DELAY=x}` codes from all auto-type sequences.

Reset default sequences that contain `{DELAY=`
Select nodes: | `//DefaultSequence[contains(., '{DELAY=')]`
Action: | Remove nodes
If a sequence has been specified in the field 'Override default sequence' (in the entry dialog) and it contains `{DELAY=`, the sequence is reset, i.e. the option 'Inherit default auto-type sequence from group' is activated.

Add an auto-type association to all entries
Select nodes: | `//Entry/AutoType`
Action: | Replace data
Data: | Outer XML
Find what: | `</AutoType>\Z`
Replace with: | `<Association><Window>* - Notepad</Window><KeystrokeSequence>{PASSWORD}</KeystrokeSequence></Association></AutoType>`
Regular expressions: | Activated
Adds an auto-type association to all entries: the window title '`* - Notepad`' is associated with the sequence '`{PASSWORD}`'.

Copy entry URLs into title fields
Select nodes: | `//Entry`
Action: | Replace data
Data: | Inner XML
Find what: | `(?s)(<Key>Title</Key>\s*)(<Value>.*?</Value>|<Value\s*/>)(.*?<Key>URL</Key>\s*)(<Value>.*?</Value>|<Value\s*/>)`
Replace with: | `$1$4$3$4`
Case-sensitive: | Activated
Regular expressions: | Activated
Copies the entry URL into the title field of the entry (overwriting any existing data in the title field).

Copy entry titles into empty user name fields
Select nodes: | `//Entry/String[Key='UserName' and Value='']/..`
Action: | Replace data
Data: | Inner XML
Find what: | `(?s)(<Key>Title</Key>\s*?<Value>)(.*?)(</Value>.+?<Key>UserName</Key>\s*?)<Value />`
Replace with: | `$1$2$3<Value>$2</Value>`
Case-sensitive: | Activated
Regular expressions: | Activated
Copies the entry title into the user name field of the entry, if this field is empty.

Ensure first line is not empty
Select nodes: | `//Entry/String/Value`
Action: | Replace data
Data: | Inner text
Find what: | `(?s)^(\r?\n)`
Replace with: | `--$1`
Regular expressions: | Activated
For all multi-line fields, this inserts '`--`' into the first line of the field value, if this line is empty and the value has at least two lines. For example, Sample data is replaced by -- Sample data