Vignesh
Vignesh

Reputation: 364

Xml schema regex to not allow white spaces alone

In xml, i have a tag say <membercode>

The regex pattern for the field currently is <xs:pattern value="[a-zA-Z0-9\s]{1,3}" />

The membercode must accept values as follows:

1) "XY"  -> pass

2) "XY " -> pass

3) "   " -> i need to have this failed. but it is getting passed.

4) " XY " -> need to be failed 

EDIT: Requirements:

1) total allowed characters must be maximum 3.

2) With in the allowed 3 chars, there can be spaces. ex: "XY", "XY "," XY" , "X "

3) But if the total chars exceed 3 or if there is only spaces(no alphanumeric chars) then it should not be allowed.

tried whitespace collapse property, but in that case the maxlength is applied after the collapse is done. so values more than max length is also allowed.

hence tried below things..

<xs:pattern value="[a-zA-Z0-9]{1,3}[\s?]" />
<xs:pattern value="[a-zA-Z0-9]{1,3}|[a-zA-Z0-9\s?]{1,3}" />

but couldn't get hold on the correct solution.

Please point me the correct approach for this..

Many thanks for the suggestions!

Upvotes: 2

Views: 3316

Answers (2)

sergioFC
sergioFC

Reputation: 6016

You're on the right track using <xs:whiteSpace> restriction, but the value should be preserve in order to not modify the original whitespace.

You can use this pattern:

[\w\d ]*[\w\d][\w\d ]*

The central part ([\w\d]) says that a letter or a digit must appear. Before and after that compulsory alphanumeric character, the string can optionally have any letter/digit/space combination ([\w\d ]*). The restrictions <xs:whiteSpace> and <xs:maxLength> ensure that there is no more than 3 length strings including original white-spaces.

<xs:element name="elem">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:whiteSpace value="preserve"/>
            <xs:maxLength value="3"/>
            <xs:pattern value="[\w\d ]*[\w\d][\w\d ]*"></xs:pattern>
        </xs:restriction>
    </xs:simpleType>
</xs:element>

Note that you can change \w to a-zA-Z if you don't want the underscore character that is also included in \w.

Upvotes: 4

pjanssen
pjanssen

Reputation: 1111

This should work:

[a-zA-Z0-9][a-zA-Z0-9\s]{1,2}

[\w\d][\w\d\s]{1,2} equivalent

Does this help you out?

\s?[\w\d]{1,3}\s?

Upvotes: 2

Related Questions