Reputation: 1395
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.
Upvotes: 3
Views: 2475
Reputation: 626903
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
[a-zA-Z]
- any ASCII letter[a-zA-Z0-9]*
- zero or more ASCII letters and/or digits.Upvotes: 4