I am stuck on an XSD check, I need to check if a code composed of 4 letters is valid, the first and the second two letters must be in alphabetic order example:
ABCD or CDAB or EFBC etc...
And letters cannot be repeated.
I've got this so far:
<xs:simpleType name="NameAddress">
<xs:restriction base="xs:string">
<xs:minLength value="0"/>
<xs:maxLength value="4"/>
<xs:pattern value="(A){0,1}(B){0,1}(C){0,1}(D){0,1}(E){0,1}(F){0,1}(G){0,1}(H){0,1}(J){0,1}(K){0,1}(L){0,1}(M){0,1}(P){0,1}(Q){0,1}(R){0,1}(S){0,1}"/>
</xs:restriction>
</xs:simpleType>
But it only checks the alphabetic order in the sequence so if I write ABCD or CDEF it works, but it doesn't look behind and CDAB will fail.
Is there any way to perform this?
Thanks to all!
Related
I have a restriction that must be :
4 digits or 0.
the values must be 4 ditits number or the number 0- the answer "refuse to answer".
i know how to make each restriction separatly but not together in the same elemnt.
can you help me ?
thank you
You can use a regular expression pattern to create your 2 options of 1 digit range 0 or 4 digits and I assume range 0-9 for each since you didn't specify.
<xs:element name="foo">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:pattern value="[0]|[0-9][0-9][0-9][0-9]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
I want to define an element in XML schema that will allow for an empty string or some specific pattern, e.g.,:
<Code/>
<Code></Code>
<Code> </Code>
<Code>11111</Code>
<Code>111111</Code> - INVALID
<Code>AAAAA</Code> - INVALID
How can I modify my existing restriction?
<xs:element name="Code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{5}" />
</xs:restriction>
</xs:simpleType>
</xs:element>
Add \s as another choice to your regex to allow whitespace characters [#x20\t\n\r] (That is: "regular" space, tab, line feed, carriage return. Non-breaking space is not included.)
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="\s*|[0-9]{5}" />
</xs:restriction>
</xs:simpleType>
Use "^$|pattern" it should work as ^$ matches just null values.
i"m trying to validate if an xml element is a multiply of 5 by using xsd. does any 1 have an idea how to do it ?
Thanks,
Itay
I'm pretty sure you can't use general formula for validation. But in your specific case you can use a pattern match to match multiples of 5
<xs:element name="myelement">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]*[05]{1}"></xs:pattern>
</xs:restriction>
</xs:simpleType>
</xs:element>
I have some XML documents over which I have no control whatsoever. Their structure is well-defined, but it is described in a bunch of PDFs, which, despite being very exact, don't make automated validation very tractable. I'm trying to write a XML schema to make (most of) the rules in those PDFs executable.
All the elements are mandatory. But about half of them can be either empty or have simple typed content.
When defining datatypes for these elements, I defined two versions of each: a "normal" one, and another that can be empty. I did this by defining unions with an empty datatype:
<xs:simpleType name="empty">
<xs:restriction base="xs:string">
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="codPostal">
<xs:restriction base="xs:string">
<xs:pattern value="^[0-9]{4}-[0-9]{3}$"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="opt_codPostal">
<xs:union memberTypes="empty codPostal"/>
</xs:simpleType>
Is there a less repetitive way of doing this?
You can use xs:nillable.
In XSD
<xs:simpleType name="codPostal">
<xs:restriction base="xs:string">
<xs:pattern value="^[0-9]{4}-[0-9]{3}$"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="OptionalString" type="codPostal" nillable="true" />
In Document
<OptionalString xsi:nil="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
This is most useful for non-string types (e.g. datetime etc) as for strings you could just use zero length.
<OptionalString />
Unfortunately you need to specify the "nil" attribute on the document. As far as I know, the only non-intrusive way to do what you want is the union type approach that you've already chosen.
Currently I have an Xsd validating with this rule
<xs:simpleType name='shipTo'>
<xs:restriction base='xs:string'>
<xs:minLength value='6'/>
</xs:restriction>
</xs:simpleType>
I need to allow blanks as well, but if a value is entered, it's minimum length should still be 6.
Can I do this without resorting to this xs:pattern and regex?
<xs:simpleType name='shipTo'>
<xs:restriction base='xs:string'>
<xs:pattern value='^(?:|[\w]{6,})$'/>
</xs:restriction>
</xs:simpleType>
The regex will work, but you should really make the element that you will be assigning shipTo to optional, and not include it in the XML file if it has no value.