languagetool-org/languagetool · error · SAXException

Please set min and max attributes on the first token in the

Error message

Please set min and max attributes on the first token in the AND group.\n You attempted to set these attributes on the token no. + (andGroupCounter + 1) + .\n Line: + pLocator.getLineNumber() + , column: + pLocator.getColumnNumber() + .

What it means

XMLRuleHandler throws this SAXException when min/max occurrence attributes are set on a token that is not the first element of an AND group. In an AND group, occurrence constraints are only meaningful on the first token, because the group is matched in parallel; setting them on later group members (andGroupCounter > 0) is invalid. The message names which group token and the XML line/column.

Source

Thrown at languagetool-core/src/main/java/org/languagetool/rules/patterns/XMLRuleHandler.java:631

    if (maxOccurrence != 1) {
      patternToken.setMaxOccurrence(maxOccurrence);
      maxOccurrence = 1;
    }
    if (posToken != null) {
      patternToken.setPosToken(obtainPosToken(posToken, posRegExp, posNegation));
      posToken = null;
    }
    if (chunkTag != null) {
      patternToken.setChunkTag(chunkTag);
      chunkTag = null;
    }
    if (tokenReference != null) {
      patternToken.setMatch(tokenReference);
    }
    if (inAndGroup && andGroupCounter > 0) {
      patternTokens.get(patternTokens.size() - 1).setAndGroupElement(patternToken);
        if (minOccurrence !=1 || maxOccurrence !=1) {
            throw new SAXException("Please set min and max attributes on the " +
                    "first token in the AND group.\n You attempted to set these " +
                    "attributes on the token no. " + (andGroupCounter + 1) + "." + "\n Line: "
                    + pLocator.getLineNumber() + ", column: "
                    + pLocator.getColumnNumber() + ".");
        }
    } else if (inOrGroup && orGroupCounter > 0) {
      patternTokens.get(patternTokens.size() - 1).setOrGroupElement(patternToken);
    } else {
      if (minOccurrence < 1) {
        patternTokens.add(patternToken);
      }
      for (int i = 1; i <= minOccurrence; i ++) {
        patternTokens.add(patternToken);
      }
      minOccurrence = 1;
    }
    if (inAndGroup) {
      andGroupCounter++;

View on GitHub (pinned to 2e990059ce)

Solutions

  1. Remove min/max from non-first tokens in the AND group; set them only on the first token
  2. Restructure the rule (split into multiple patterns or use a different grouping) if you need per-element repetition
  3. Validate the rule with LanguageTool's rule tests after editing

Example fix

// before
<and><token>a</token><token min="2" max="3">b</token></and>
// after
<and><token min="2" max="3">a</token><token>b</token></and>
Defensive patterns

Strategy: validation

Validate before calling

// Check AND-group members after the first for min/max attributes
for (int i = 1; i < andGroupTokens.size(); i++) {
  Element t = andGroupTokens.get(i);
  if (t.hasAttribute("min") || t.hasAttribute("max")) {
    throw new IllegalArgumentException("min/max only allowed on first token of AND group");
  }
}

Try / catch

try {
  loader.getRules(is, filename);
} catch (SAXException e) {
  if (e.getMessage().contains("first token in the AND group")) {
    log.error("Move min/max to the first AND-group token", e);
  } else throw e;
}

Prevention

When it happens

Trigger: Parsing an <and> group whose second-or-later token has min=".."/max=".." set while inAndGroup && andGroupCounter > 0, e.g. <token min="2"/> inside an AND group after the first token.

Common situations: Authors trying to express 'repeat this member of the AND group' — repetition must instead be modeled outside the group; refactoring a plain token sequence into an AND group while keeping per-token min/max.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of languagetool-org/languagetool@2e990059ce (2026-09-06). Data as JSON: /api/errors/edf70ebb1589f83d. Report an issue: GitHub.