Xsd, Validate empty or minimum length string - xsd

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.

Related

It's possible to use multiple value in single enumeration tag?

I'm using XSD validation in WEB API project.
now i'm using following method for check string value.
<xs:simpleType name="CoverageTierPattern">
<xs:restriction base="xs:string">
<xs:enumeration value="EE"/>
<xs:enumeration value="ES"/>
<xs:enumeration value="EC"/>
<xs:enumeration value="Fam"/>
<xs:enumeration value="WO"/>
<xs:enumeration value="WP"/>
<xs:enumeration value="NE"/>
<xs:enumeration value="RC"/>
</xs:restriction>
</xs:simpleType>
this list value must be added more in future.
So i need to know it's possible to use values in single enumeration element
like
<xs:enumeration value="EE|ES|EC|..."/>
other wise if any other method present plz tell me.
You can use pattern element instead of enumeration like this:
<xs:pattern value="EE|ES|EC|..."/>
Full example from here in the middle of the page.
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

CDATA to ignore & in XSD

I need the xml parser/validator to ignore the presense of &
How do I accomplish it by using CDATA in xsd.
This is snippet of xsd:
<xs:simpleType name="values">
<xs:restriction base="xs:string">
<xs:enumeration value="IN & OUT"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
I tried using CDATA as follows but in vain as I get xsd validation error:
<xs:simpleType name="values">
<xs:restriction base="xs:string">
<xs:enumeration value="IN <![CDATA[&]]> OUT"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
Any help is appreciated.
Thansk in advance.
I believe you can use the entity reference & instead of the character &.
Try using an entity reference:
<xs:simpleType name="values">
<xs:restriction base="xs:string">
<xs:enumeration value="IN & OUT"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
or a decimal reference:
<xs:simpleType name="values">
<xs:restriction base="xs:string">
<xs:enumeration value="IN & OUT"/>
<xs:enumeration value="XYZ"/>
</xs:restriction>
</xs:simpleType>
Hmm. I find it interesting how that line in the second snippet is actually black instead of the properly coloured elements.
May I ask what the validation error is? It may help in pinpointing what is exactly wrong with that line of code. It may be true that the parser will ignore the & sign, but have you tried replacing the & with '&' ? The thing is, I have a feeling you're setting the enumeration value to
"IN <![CDATA[&]]> OUT"/>
Which would... obviously not pass the validation. Either that or the parser completely passes over that line and only takes in XYZ as an enum value.
What's the error.
Have you tried replacing & with
&
Since it's an escape entity?
Cheers.

XML schema restriction that allows empty elements or specific pattern

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.

How to describe math formula by using xsd?

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>

How to allow typed values to be empty with an XML schema?

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.

Resources