2

I have a XML file I use to manually route users to specific pages in a website.

Currently, we have separate entries for every variation of possible searches (plural, typos etc.). I would like to know if there is a way I can condense it with regex to something like so:

<OnSiteSearch>

    ...

    <Search>
        <SearchTerm>(horses?|cows?) for sale</SearchTerm>
        <Destination>~/some/path.html</Destination>
    </Search>

    ...

</OnSiteSearch>

Is something like this possible? I've looked online for regex and XML but it seems to be about validating content between the XML tags and not about using regex as the content.

Antonio
  • 731
  • 7
  • 28
  • 3
    Yes, although you'll have to escape any special XML characters with a CDATA block if you end up needing to use them. – CAustin Feb 14 '18 at 00:56

1 Answers1

3

Yes, a regex can be stored in XML as long as you mind XML escaping rules to keep the XML well-formed:

  • Element content: Escape any < as &lt; and any & as &amp; when writing the regex; reverse the substitution before using the regex.
  • Attribute value: Follow rules for element content plus escape any " as &quote; or any ' as &apos; to avoid conflict with chosen attribute value delimiters.
  • CDATA: No escaping needed, but make sure your regex doesn't include the string ]]>.
kjhughes
  • 106,133
  • 27
  • 181
  • 240