Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way of removing rogue ampersands in XML?

(TLDR at the bottom)

We have a legacy system that has implemented its own XML reader/writer. The problem is that it allows a literal "&" inside a property value.

<SB nae="Name" net="HV & DD"/>

When I am reading the data using XDocument.Parse() method, this fails of course. I am looking at ways of sanitizing the data.

I am attempting to use regex to identify cases where this is happening. To illustrate, consider this:

&(?!amp\;)

This will identify ampersand with a negative lookahead to ensure it isn't actually a correctly escaped ampersand. When I have identified these cases, I can substitute with a proper &

Of course, there is a problem that this will match other escaped character such &gt &lt &quot etc, so I need to unmatch those as well. Maybe using a more general form, like a regex unmatching ampersand followed by 2-4 characters and then semicolon.

But my worry is that there are other cases for ampersands that I am not thinking of and that are not represented in the few samples I have got. I am looking for a safe way that will not mess up proper xml.

TLDR: How do I identify ampersands that are not part of proper xml, but are cases of unescaped ampersands in property values?

like image 359
Tormod Avatar asked Jan 21 '26 02:01

Tormod


1 Answers

You can substitute the following regex pattern with &amp;:

&(?!(?:#\d+|#x[0-9a-f]+|\w+);)

Demo: https://regex101.com/r/3MTLY9/2

like image 85
blhsing Avatar answered Jan 23 '26 14:01

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!