Skip to content

define \w as [^\p{P}\p{Z}\p{C}] in the xsd regex parser - #145

Merged
pjfanning merged 4 commits into
apache:trunkfrom
aizu-m:xsd-regex-word-class
Nov 15, 2026
Merged

pjfanning merged 4 commits into
apache:trunkfrom
aizu-m:xsd-regex-word-class

Conversation

@aizu-m

@aizu-m aizu-m commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Differential run of one schema and one set of documents through XmlBeans and the JDK's Xerces validator, pattern facet on xs:string:

DISAGREE [pattern \W+] xmlbeans=true xerces=false
   xml: <t:e>+</t:e>
   xe: cvc-pattern-valid: Value '+' is not facet-valid with respect to pattern '\W+' for type 'T'.
DISAGREE [pattern [^\w]+] xmlbeans=true xerces=false
   xml: <t:e>Dž</t:e>
DISAGREE [pattern \w+] xmlbeans=false xerces=true
   xml: <t:e>ſ</t:e>

Found while auditing the pattern facet for values an instance can slip past. The run produced 63 disagreements where XmlBeans accepts and Xerces rejects, and 67 the other way round. Every one of them is on \w, \W or [^\w]. The other escapes agree.

XML Schema Part 2, Appendix F, defines \w as [#x0000-#x10FFFF]-[\p{P}\p{Z}\p{C}]: every character that is not punctuation, a separator or "other". ParserForXMLSchema.getRange builds xml:isWord from the XML 1.0 Letter and Digit tables instead. Those are Unicode 2.0 name characters. So symbols (+, ^, |, $), combining marks, letter numbers (Ⅷ), titlecase letters (Dž) and letters added after Unicode 2.0 (ſ, ff) sit outside \w. A facet written with \W or [^\w] accepts them and one written with \w rejects them. Only schema mode (option X, which is what pattern facets use) is affected. Xerces corrected the same port in 2.12.0 (XERCESJ-1651).

The fix mirrors Xerces: \W is the union of the P, Z and C category tokens the class already has, and \w is its complement. \d, \s, \i, \c and the Perl-mode word class are untouched. With the change the differential run has no remaining \w disagreement. Regression test added to RegularExpressionTest; it fails before the change and passes after. ./gradlew test passes.

@pjfanning

Copy link
Copy Markdown
Member

Why does xmlbeans need to become strict after over 20 years and no-one complaining about it being lax?
Can we at least make lax vs strict configurable?

@aizu-m

aizu-m commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Fair point. Nobody has complained because the lax side is quiet. A facet like [^\w]+ is usually written to keep word characters out, and with the old tables +, $ or Dž get through it and validate. I only noticed it when running the same schemas through XmlBeans and the JDK's Xerces side by side. Xerces changed the same code in 2.12.0, so a schema that passes there can still disagree here.

That said, I agree it shouldn't change under existing users. Pushed c788baa:

  • default is the old behaviour again. \w and \W use the XML 1.0 Letter and Digit tables, same as before this PR.
  • -Dxmlbean.regex.strictword=true switches to the spec definition ([^\p{P}\p{Z}\p{C}]).

I went with a system property, not an XmlOptions setting. Pattern facets get compiled when the type system loads (SchemaRegularExpression.forPattern), and no options are in scope at that point. The \w ranges are also cached in a static table in ParserForXMLSchema. It follows the existing xmlbean.uniqueparticleattribution style. The tests cover both modes: the default keeps the old tables, and the strict test sets the property.

@pjfanning
pjfanning force-pushed the xsd-regex-word-class branch from c788baa to 5a4553e Compare November 15, 2026 19:43
aizu-m and others added 4 commits November 15, 2026 20:46
…operty

\w and \W in pattern facets now follow the XML Schema definition
([#x0000-#x10FFFF]-[\p{P}\p{Z}\p{C}]) by default. The pre-5.5.0 XML 1.0
Letter and Digit tables are available per type system with
XmlOptions.setCompileLegacyRegexWordClass(), replacing the
xmlbean.regex.strictword system property.

The choice is a new "L" regex option, so it is per expression rather than
global. Compiled .xsb files record it with a FLAG_SCHEMA_REGEX_WORD type
flag; types written by older versions lack the flag and load with the
legacy tables, so existing precompiled schema jars keep their behaviour.
Older runtimes ignore the flag, so no xsb version bump is needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pjfanning
pjfanning force-pushed the xsd-regex-word-class branch from 5a4553e to c769a1c Compare November 15, 2026 19:47
@pjfanning

Copy link
Copy Markdown
Member

I merged this with an XmlOptions setting. The new behaviour is the default but users can use xmloptions if they need the old behaviour.

@pjfanning
pjfanning merged commit 4aec5c4 into apache:trunk Nov 15, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants