Search code examples
saxonxml-validation

SaxonEE-9.9: Escape Character '@' not allowed in regular expression


I use Saxon EE APIs to validate XML payload with XSDs. My environment is OSGi. One of my user is trying to escape @ character in regex definition. I've shared the snippet of the line which is causing issue.

<xsd:restriction base="xsd:string">
    <xsd:pattern value=".+\@.+\..+"/>
</xsd:restriction>

This causes the following error while loading the xsd (inside saxon library).

Error at xsd:pattern on line 17 column 70 
    Syntax error at char 4 in regular expression: Escape character '@' not allowed

Could anyone please share any hints on how to overcome this issue? Please let me know, if the complete xsd is required.


Solution

  • The XSD rules for regular expressions are rather stricter than those in many other regex dialects: characters can only be escaped if they actually need to be escaped and if the escape sequence has a defined meaning. There's no escape character defined for \@ so it should simply be written as @.

    The reason for the rule is for forwards compatibility: it allows new escape characters to be introduced in the future without breaking existing schemas.