XSD restriction with mathematical constants - xsd

My question is quite straight-forward. With XSD, is it possible to restrict a value with mathematical constants like PI. For example, when you want a radian value between 0 and PI.
If not, is there an elegant and simple way to do it ?
Thank you for your help.

You can define a simpleType with the range you want:
<xs:simpleType name="radianType">
<xs:restriction base="xs:double">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="3.14159265358979323846"/>
</xs:restriction>
</xs:simpleType>
And reuse it in elements and attributes:
<xs:element name="horizon" type="radianType"/>

Related

JAXB Schema design, having enum dynamic value

I have following schema
<xs:simpleType name="enumType">
<xs:restriction base="xs:string">
<xs:enumeration value="STRING_ONE"/>
<xs:enumeration value="STRING_TWO"/>
</xs:restriction>
</xs:simpleType>
As long as i get a known string, I can swithc and it to my jaxbobject which expects a enumType object. But what if i don't know this string ? is there a way to handle it based on JAXB Schema ? I know based on enum its not possible.
Edit: Trying to make it more clear
Wit the given Schema design, my JAXBObject, which accepts enumType as a parameter, can only have 2 input values i.e. STRING_ONE or STRING_TWO which will be converted to XML as part of marshalling.
The question is, that what if, I want to handle a situation through my schema design that if I can have an object of enumType then good otherwise I can give an XML to JAXBObject instead of enumType and it still parses it.
From the XSD point of view, it sounds like you want to be able to write
<xs:simpleType name="soft-enumeration">
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="STRING_ONE"/>
<xs:enumeration value="STRING_TWO"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:union memberTypes="xs:string"/>
</xs:simpleType>
</xs:union>
</xs:simpleType>
This type includes the enumeration you describe, but it then unions it with xs:string, so as to accept other values as well. If your schema interface gives you access to information about which member type of the union was used, then you can use that information to treat instances of STRING_ONE and STRING_TWO differently from other strings.
I have no idea whether jaxb does anything useful with this idiom, however; there you're on your own.

XSD: restrict attribute to xs:float or ""

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.

How to define a Constant in XSD

Is there a way to define a constant value and use that constant in the preceeding XSD? I have a common value I want to use for various xs:element tag's maxOccurs attributes. Like constants in other languages, I want to make the change in one place should the value backing MyConst were to ever change.
<!-- Can I do this? -->
<ConstantValue id="MyConst" value="10"/>
...
<xs:element name="sandwich_meat" type="xs:string" minOccurs="0" maxOccurs="MyConst"/>
<xs:element name="sandwich_name" type="xs:string" minOccurs="0" maxOccurs="MyConst"/>
You can try to define a simpleType with a restriction:
<xs:simpleType name="AConstantHere">
<xs:restriction base="xs:string">
<xs:enumeration value="CONSTANT_VALUE_HERE"/>
</xs:restriction>
</xs:simpleType>
It allows only one value.
No it is not allowed that way. However you can define your own type with a fixed value in it somewhere on top of your XSD (place dosen matters) and use that type for the elements.
It's not possible with plain schema, but maybe XML entities will do the trick?

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