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.
Related
I would like to enforce a rule on the attribute which should not allow a value defined in enumeration values associated with another attribute.
Here is my sample schema.
<xs:complexType name="component">
<xs:attribute name="type" type="componentMainType"/>
<xs:attribute name="category" type="forbiddenCategoryTypes" use="optional"/>
</xs:complexType>
<xs:simpleType name="forbiddenCategoryTypes">
<xs:restriction base="xs:string">
**<xs:pattern value="not in forbiddenCategoryTypes"/>**
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="componentMainType">
<xs:restriction base="xs:string">
<xs:enumeration value="Component1"/>
<xs:enumeration value="Component2"/>
<xs:enumeration value="Component3"/>
<xs:enumeration value="Component4"/>
<xs:enumeration value="Component5"/>
</xs:restriction>
</xs:simpleType>
In my opinion not possible, pattern only takes a regex. I could not find any other relevant restriction.
I would suggest to change approach to regex with values (I know it is not optimal solution).
I'm trying to define an element type in XSD, for which i want an optional attribute, which if present can either contain a float, or be empty (but still present).
i.e:
<xs:element name="MyElement">
<xs:complexType>
<xs:attribute name="optionalFloatAttribute" type="xs:float" use="optional"/>
</xs:complexType>
</xs:element>
Needs "fixing" to allow all of the following xml:-
<MyElement/>
or
<MyElement optionalFloatAttribute=""/>
or
<MyElement optionalFloatAttribute="3.14159"/>
The only way I can see of doing this is to change type to xs:string, and use xs:restriction with a regular expression. But this doesn't seem very ideal to me. Is there a better way?
And I have to be able to support these variations of the xml - the program and existing xml is legacy, and I am trying to back-create a schema to match the myriad variations I see in what we have to regard as valid xml.
You can define custom type for that by combining float and empty string:
<xs:element name="MyElement">
<xs:complexType>
<xs:attribute name="optionalFloatAttribute" type="emptyFloat" use="optional"/>
</xs:complexType>
</xs:element>
<xs:simpleType name="emptyFloat">
<xs:union>
<xs:simpleType>
<xs:restriction base='xs:string'>
<xs:length value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base='xs:float'>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
Or using regExp:
<xs:simpleType name="emptyFloat">
<xs:restriction base="xs:string">
<xs:pattern value="-?\d*\.?\d*"/>
</xs:restriction>
</xs:simpleType>
If you could stand using an element rather than an attribute you could make the xs:float nillable. This way you can use the xsi:nil="true" in your instance document to indicate that the element has no value:
<!-- definition -->
<xs:element name="quantity" type="xs:float" nillable="true" />
<!-- instance -->
<quantity xsi:nil="true" />
No equivalent for attributes though.
I don't think there's a way to handle this and use xs:float. Fundamentally it comes down to the fact that empty string isn't a valid number. You'd either normally expect a value of 0, or for the element to be missing altogether. There's a good explanation as the answer to the following question:
Empty elements for primitve datatypes forbidden in XSD
It seems that the option of using xs:string and a regexp might be your best plan.
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.
I'm currently building an XSD to validate some XML I'm working on, I'm guessing this probably isn't possible but I'm wondering if there is some way to enforce an attribute that is a ";" delimited list for example
<nbsp style="cell-width:1.29;background-color:#99CC00;"/>
similar to the way the style attribute works in html.
Thanks in advance
You can specify a type which must match a specific pattern.
Example:
<simpleType name='better-us-zipcode'>
<restriction base='string'>
<pattern value='[0-9]{5}(-[0-9]{4})?'/>
</restriction>
</simpleType>
Use a regular expression to validate the content.
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
see also
http://www.w3schools.com/Schema/el_attribute.asp
and
http://www.w3schools.com/schema/schema_simple_attributes.asp
and
http://www.w3schools.com/schema/schema_facets.asp
test your regexp here: http://regexlib.com/RETester.aspx