XSD Validation - Allow All Except String That Contains - xsd

Quite new at XSD validations.
Is there any way at all to validate string values - to allow all values a user may enter except certain values that contain a keyword.
For example, I have a system that users can submit their names.
I want to allow any name that comes through except for strings that contain 'Joh'
i.e. Allow Mary, Eric, Simon, etc and reject John, Johan, Bjohn, Benjoh, etc
So in my understanding (coming from PHP), 'Not Equal To' is '!=' but this wouldn't work here.
And wildcards wouldn't work as for any values that contain 'joh' as '%joh%'
<xs:element name="Names">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value!="%joh%"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Is there anyway around this?

Related

XSD multiple conditions

I have an XML schema with the following piece of code:
<xs:attribute name="extension" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="((18)|(19)|(20))[0-9][0-9]((0[1-9]|1[0-2]))((0[1-9]|1[0-9]|2[0-9]|3[0-1]))" />
</xs:restriction>
<xs:length value="8" />
</xs:simpleType>
</xs:attribute>
This piece of code determines if a date of birth of people alive is correct or not. It is given that anybody is born in any year starting with 18, 19 or 20. Then followed by any two numbers, a month between 01-12 and a day between 01-31. The date of birth must contain 8 numbers to validate.
The question is if there is any way to develop this further? For instance if there is a year of birth 1809, it will validate even though it is impossible for someone in that age to be alive. Also a year in the future will validate - say 2058.
I there a way to put like "minyear" and "maxyear" where the first 4 numbers of the string cannot be less than $minyear or and greater than $maxyear?
I may also add that in direct connection with this 8 number string comes additional four numbers that are controlfigures and has nothing to do with years, month and day. That is why the base is string.
Any ideas?
/Paul
Got this far:
<xs:attribute name="extension" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="(([1][8][9][0-9]|[1][9][0-9][0-9]|[2][0][0-1][0-9])(0[1-9]|1[0-2]))((0[1-9]|1[0-9]|2[0-9]|3[0-1]))" />
</xs:restriction>
<xs:length value="8" />
</xs:simpleType>
</xs:attribute>
This piece of code will restrict valid years from 1890 until 2019. The only problem is that the year 2019 will pass, besides that it works.
However it is not perfection and when the year 2020 comes it will let pass all years up to 2029, so is there a way to rule out that imperfection?
/Paul
Yes! Solved:-)
The pattern section should be like this:
<xs:pattern value="(((189)[0-9]|(19)[0-9][0-9]|((200)[0-9]|(201)[0-8]))(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1]))" />
This works in XML Schema 1.0. Perhaps there is a slicker solution in 1.1?
/Paul

SOAP UI - <xs:choice> and <xs:element> as siblings under <xs:sequence> not interpreted correctly

I am using SOAPUI for sending SOAP requests to my web service.
When I have <xs:choice> and <xs:element> as siblings under <xs:sequence>, the choice element does not appear as a combo box, instead appears as sequence of elements.
In this case, my SOAP request in SOAPUI shows all the elements ignoring <xs:choice>.
Please let me know if somebody ran into this problem and got any solution
What I understand you are looking for is numerated list
Like this
<xs:simpleType name="regularType">
<xs:restriction base="xs:token">
<xs:enumeration value="yes"/>
<xs:enumeration value="no"/>
</xs:restriction>
</xs:simpleType>
and add as many choices as you need.
Edit: I know it's 4 months old question, but I guess someone in the future might benefit from this.

Add pattern to anyURI data type in xsd

How can I guarantee that the url element starts with "http://"?
<xs:element name="url" type="xs:anyURI"/>
You can add a xs:restriction for a Regular Expression using xs:pattern:
<xs:element name="url">
<xs:simpleType>
<xs:restriction base="xs:anyURI">
<xs:pattern value="http://.+" />
</xs:restriction>
</xs:simpleType>
</xs:element>
This will match to anything that starts with http://. It will match:
http://www.stackoverflow.com
http://somethingsomethingsomething
http://123456789!!!!!
http://0
It will not match https URLs:
https://github.com
If you want to match https as well you can change the pattern to
https?://.+
which means that the s is allowed and is optional.
If you want to match only valid URLs then you need to improve it to check for characters, followed by a dot, more characters, a valid domain suffix, etc. If you search for URL validation via regex you will find several examples. You can also try this resource. And to experiment with Regex, Regex 101 is a great resource.
Pattern matching in XSD has some restrictions. Check this SO question/answer which discusses that.

Is there a way to restrict a string to be ASCII only in XSD?

Although the XML file being described by the XSD Schema may contain any unicode characters in general, there are some fields where only ASCII is allowed. (As these strings are going to be passed to another system which only accepts ASCII.)
Is there a way to specify that in XSD?
A regexp with all possible ASCII characters would be a possibility I suppose, but I feel there must be a better way.
You can try that :
<xs:simpleType name="basicLatin">
<xs:restriction base="xs:string">
<xs:pattern value="\p{IsBasicLatin}*"/>
</xs:restriction>
</xs:simpleType>
Unfortunately, for your requirement there isn't a way to restrict without using patterns.

Help in defining conditional params in WSDL

I am working on defining WSDL and it has i/p param in the operation.
WSDL will be tested by SOATest.
Query:
I have a param which is conditional and need help in defining.
I want something like this..
if Name is selected then it should force user to select LastName, else both will be null
Dummy Ex:
<xs:simpleType name="FullName">
<xs:restriction base="xs:string">
<xs:enumeration value="Name" />
<xs:enumeration value="LastName" />
</xs:restriction>
</xs:simpleType>
There is no way to do this using WSDL or XSD (which WSDL depends on).
There are sometimes hacks you can use, but they're worse than the alternative. The alternative is to simply verify in code that if Name is selected then Lastname must be supplied.

Resources