Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Schema Pattern Error

I am trying to write an XML schema. The specifics require me to validate a field so that it starts with a letter, followed by alphanumeric characters (e.g. Foo3x4 is valid, 3Foo or Foo3_ are not).

This is how I wrote it:

<xsd:simpleType name="nameType">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="^[a-zA-z][a-zA-Z0-9]*$" />
    </xsd:restriction>
</xsd:simpleType>

But if I try to validate a document that has as nameType the value H0, it gives me the following error (translated in English by me):

cvc-pattern-valid: The value "H0" is not valid as facet for the pattern "^[a-zA-z][a-zA-Z0-9]*$" for the type nameType

I can't get why. Using it on RegExr (/^[a-zA-z][a-zA-Z0-9]*$/gm), it works. Am I missing something in the pattern specification?

One last detail. The validation error is given by the marshaller in the JAXB Java framework.

like image 677
Tu.Ma. Avatar asked Feb 27 '26 14:02

Tu.Ma.


1 Answers

You need to remove ^ and $ as XSD patterns are anchored implicitly and ^ and $ are not used as anchors in these patterns, and fix the typo in the [a-zA-z] character class, it should be [a-zA-Z] if you need to match any ASCII letter (see this related answer, [A-z] does not only match ASCII letters).

Use

<xsd:pattern value="[a-zA-Z][a-zA-Z0-9]*" />

This pattern will actually match the following

  • implicitly matches the start of string
  • [a-zA-Z] - any ASCII letter
  • [a-zA-Z0-9]* - zero or more ASCII letters and/or digits.
  • implicitly matches the end of string
like image 167
Wiktor Stribiżew Avatar answered Mar 02 '26 05:03

Wiktor Stribiżew



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!