O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Common Schema Types

Problem

You want to define some typical regular expression type constraints for use in your schema and you don’t want to reinvent the wheel.

Solution

Try one of the following commonly used regular expressions. While regular expression usage can vary from platform to platform, these have all been tested with Java, and will work on validation of a SOAP payload.

Person’s name

This type would be used for two separate elements for first name and last name. Names must be at least 2 characters and no more than 30 characters; must consist of alphabetic characters; and may contain hyphens, single quotes, periods and spaces. Yes, I do know someone whose legal name contains a “.” (hi, J.!). And, I have a cousin with the legal name “M’Lee”:

<xs:element name="name">     
<xs:simpleType>
    <xs:restriction base="xs:string">
       <xs:minLength value="2" />
       <xs:maxLength value="30" />
       <xs:pattern value="[A-Za-z\-. ']{2,30}" />
    </xs:restriction>
</xs:simpleType>
</xs:element>

This is not necessarily the best way to restrict a name, but at least it’s something to work with. The airline industry does not allow for any special characters, including the very common hyphen, for example. Just do what works for you.

U.S. phone number

This handles the common hyphenated field separation as well as using dots or spaces for field separators:

<xs:element name="usPhone"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="\(?\d{3}\)?[ \-\.]?\d{3}[ \-\.]?\d{4}" /> <xs:minLength value="10" /> <xs:maxLength ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required