5 min read
Find and replace in an XML file without breaking the tags
By Quentin Delepierre, Salesforce Commerce Cloud consultant
Replacing a word in an XML file looks simple, until the editor also renames a tag or an attribute. The file is still XML, but the tool that imports it no longer recognizes it. Here is why it happens, and how to replace only the values.
Got your own XML? Drop it in and see it as a table, free and with no account. For an Excel file you can edit and then rebuild into an XML that keeps its structure, a free account is all you need.
Simple Excel table, free with no account, for files up to 1 MB. File that rebuilds into XML: free account, 3 free credits, no credit card.
How do I replace a word in an XML file without breaking the tags?
Don't do the replacement in a text editor: it also sees tag and attribute names, and “Replace all” changes them along with the values. Use a table instead, where each tag or attribute is a column header and each value is a cell. Select the cells to change, without row 1, run the find-and-replace on that selection only, then rebuild the XML: it comes back with its original tags and attributes. The preview of your file is free and needs no account; the transformation into a table costs 1 credit, and the rebuild is free.
Why a plain-text find and replace breaks XML
A text editor doesn't know XML: it sees characters. Searching for “color” finds the word in a description's value, but also in a tag name (<color>), in an attribute name (color-family) and in its value. “Replace all” changes every one of them. Invented example:
- The tag name changed (<color> became <colour>): the tool that imports the file only knows <color>, so it may refuse the file or ignore the value.
- The attribute name changed the same way (color-family).
- Nothing warns you: the file is still well-formed XML, and no basic check sees the difference. The error shows up at import, or not at all.
<products>
<product sku="A1" color-family="warm">
<name>Desk lamp</name>
<color>red</color>
<description>A color-changing lamp.</description>
</product>
<product sku="B2" color-family="cool">
<name>Office desk</name>
<color>blue</color>
<description>Available in one color.</description>
</product>
</products><products>
<product sku="A1" colour-family="warm">
<name>Desk lamp</name>
<colour>red</colour>
<description>A colour-changing lamp.</description>
</product>
<product sku="B2" colour-family="cool">
<name>Office desk</name>
<colour>blue</colour>
<description>Available in one colour.</description>
</product>
</products>Can I use a regular expression to replace text in an XML file?
A regular expression can aim at the text between two tags, but it stays fragile:
- a value on several lines, a CDATA block (a description's HTML) or a tag that carries attributes slips past a pattern written for the simple case;
- a pattern that is right on one file can be wrong on another, and a miss is found at import;
- one stray character in the pattern replaces a piece of the structure, with no message.
How do I replace values in an XML file safely, using a table?
In the table ExcelifyXML produces, structure and values are kept apart: each tag or attribute is a column header, on row 1, and each value is a cell. A find-and-replace limited to the cells can't rename a tag.
- Drop your XML file on ExcelifyXML. The free preview shows the detected row and the first values; confirm with “Transform to CSV — 1 credit”, then choose the Excel file (.xlsx).
- Open the file in your spreadsheet. Select the cells to change, without row 1: for example the description column, from row 2 to the last row.
- Run the find-and-replace on that selection only.
- Save, then drop the Excel file, alone, on the Rebuild page (free). The XML comes back with its original tags and attributes, and your replaced values.
@sku , @color-family , name , color , description
A1 , warm , Desk lamp , red , A color-changing lamp.
B2 , cool , Office desk , blue , Available in one color.@sku , @color-family , name , color , description
A1 , warm , Desk lamp , red , A colour-changing lamp.
B2 , cool , Office desk , blue , Available in one colour.How do I run the find-and-replace in Excel without touching the headers?
- Select the cells before opening the window (Home › Find & Select › Replace). When several cells are selected, the search is limited to the selection. With no selection, it covers the whole sheet, row 1 included.
- “Match case” (under “Options”): “Lamp” doesn't replace “lamp”.
- “Match entire cell contents”: only the cells equal to the searched word are replaced. Handy to turn “red” into “crimson” without touching “dark red”.
- Replacing with nothing empties the cells, and an emptied cell removes its tag from the XML. In an import's Merge mode, the platform then keeps the existing value: to erase a value, do it in the platform.
- Check the result before saving; Ctrl+Z (Cmd+Z on Mac) undoes a replacement.
What if a find-and-replace renamed a header?
A “Replace all” over the whole sheet also changes row 1. In the example, replacing “color” with “colour” over the whole sheet renames the headers “@color-family” and “color”. The rebuild then stops on the “A header was renamed” screen:
- The text says: “Rebuilt as is, the XML would write these values in a tag (or, for a header with @, an attribute) of the new name, which the original file doesn't have and an import would refuse:”.
- Each affected column is listed with its rows and its original header: “renamed header: it replaced “color” (a find-and-replace over the whole sheet also changes the headers)”.
- The advice: “Renamed header: put the original name back in row 1 (undo the find-and-replace, or copy the header from the downloaded data.csv), then drop the file again. For a find-and-replace, select the cells to change first, without row 1.”
- The “Drop the corrected file” button lets you start again. “Rebuild anyway (under the new name)” writes the values of “colour” in a tag of the new name and those of “@colour-family” in a colour-family attribute, which the import would refuse: don't use it for this case.
What this safeguard doesn't see
- It watches row 1. A value replaced by mistake elsewhere in the table (an @sku identifier that contained the searched word, for example) triggers no stop: it's data like any other. Hence selecting first.
- A description's HTML is a value like any other: a replacement in that column changes the HTML in the selected cells, not the structure of the XML.
- In Google Sheets, a find-and-replace on spaces or apostrophes can touch the headers: limit the search to the selected range and check row 1 afterwards. If you forget, the “A header was renamed” stop names both headers.
Drop your XML file and replace its values in a table: the preview is free and needs no account, nothing is charged without your confirmation:
Transform a file