Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying multiple patterns in a restriction in a XSD

Tags:

java

regex

xml

xsd

I have following XSD element:

<xsd:element name="password">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <!-- Length check -->
            <xsd:pattern value=".{9,12}" />
            <!-- Format check -->
            <xsd:pattern value=".*\d{2,}.*" />
            <xsd:pattern value=".*[A-Z]{1,}.*" />
            <xsd:pattern value=".*[a-z]{1,}.*" />
            <xsd:pattern value=".*\p{Punct}.*" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

I want to apply each of those patterns individually. It should first check that length is proper. If so then check that it has at least 2 digits and so on. Instead, it concatenates all the expressions together and tries to apply them together.

This is very poor design. If only one pattern is allowed <xsd:restriction> should define the cardinality of <xsd:pattern> to be 1. Allowing multiple <xsd:pattern> gives the impression that multiple patterns are supported.

Is there a way to apply multiple patterns to a XSD element?

like image 632
Kshitiz Sharma Avatar asked Mar 18 '23 21:03

Kshitiz Sharma


1 Answers

Multiple patterns per restriction are supported, but they do not mean AND; they mean OR:

Note: An XML <restriction> containing more than one <pattern> element gives rise to a single ·regular expression· in the set; this ·regular expression· is an "or" of the ·regular expressions· that are the content of the <pattern> elements.

If you want multiple regex constraints to apply concurrently, write them in a single <pattern>.

For min/max length constraints, rather than using <pattern>, consider using <minLength> and <maxLength> facets.

like image 77
kjhughes Avatar answered Apr 02 '23 18:04

kjhughes