Question

I need to add restriction on a particular field - phoneNumber. Rightnow the restriction looks like this :

        <xsd:element minOccurs="0" name="phoneNumber">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:pattern value="[0-9]{2}[\-][0-9]{10}"/>
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:element>

So, this would work if the phone number have a value

91-1234567890

But for a US contact I want the country code is one 1. So, the pattern needs to be changed to [0-9]{1}[\-][0-9]{10}

1-1234567890

So, I have two questions

  1. How do I give a specific range in this pattern.
  2. Is there a way to decide the pattern during runtime? e.g. If I get the country as India I would keep it as [0-9]{2}[\-][0-9]{10} and if I get it as US then I would modify it to [0-9]{1}[\-][0-9]{10}
Was it helpful?

Solution

Hopefully answer to question 1: [0-9]{1,2}-[0-9]{10} or \d{1,2}-\d{10}

My favorite RegEx site: http://www.regular-expressions.info/reference.html

Question 2:

Conditional RegEx: only possible if the data is in the text — this is not the case here

Conditional XSD => I simply redirect you :-)

How to add conditional validation in XSD :

xsd property set required according to enum value

Hope, that is what you need, if not we can go deeper here, when you post the part of your xml containing the values in question

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top