How to allow negative values, xml schema? - xsd

<xs:element name="Quantity" minOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="10" />
<xs:minLength value="1" />
<xs:pattern value="[0-9]*" />
</xs:restriction>
</xs:simpleType>
</xs:element>

Turning skaffmans comment into an answer: Choose integer for type.
<xs:element name="Quantity" type="xs:integer">

Yes. Adding to #Filburt's answer, you don't need to explicitly mention minOccurs="1" in such cases because by default minOccurs and maxOccurs are equal to 1.

Related

How to Extend a XSD Restriction/Pattern Value

I am trying to restrict the values for two Elements that can share (most of) the same attribute 'type' values. I'd like to be able to extend those values for one of the Elements (see sample code below -- the 'End' element's 'Value' attribute can have the same entries as the 'Start' element's 'Value' attribute, but there can be additional values). I don't think my solution in the example is correct; is there a simple solution that I can follow?
<xsd:simpleType name="StartAndEndTypeType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="(value1|value2|value3"/>
</xsd:restriction>
</xsd:simpleType>
<xs:element name="Start">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute name="Value" type="StartAndEndTypeType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="End">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute name="Value" type="StartAndEndTypeType|value4"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
This can be done in a number of ways.
Note I've changed your pattern facet to an enumeration as it works better for the example (but a pattern facet could be put back if required)
1 - Restricting a type
The StartAndEndTypeType contains all the values required, and then you restrict the ones you don't want in the StartType.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2019 BETA (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="StartAndEndTypeType">
<xs:restriction base="xs:string">
<xs:enumeration value="value1" />
<xs:enumeration value="value2" />
<xs:enumeration value="value3" />
<xs:enumeration value="value4" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="StartType">
<xs:restriction base="StartAndEndTypeType">
<xs:enumeration value="value1" />
<xs:enumeration value="value2" />
<xs:enumeration value="value3" />
</xs:restriction>
</xs:simpleType>
<xs:element name="Start">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute name="Value" type="StartType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="End">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute name="Value" type="StartAndEndTypeType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
2 - Extend the base definition using a union
You define the base type StartAndEndTypeType, then add to it the additional values you want to allow using an xs:union.
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2019 BETA (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="StartAndEndTypeType">
<xs:restriction base="xs:string">
<xs:enumeration value="value1" />
<xs:enumeration value="value2" />
<xs:enumeration value="value3" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="EndType">
<xs:union memberTypes="StartAndEndTypeType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="value4" />
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
<xs:element name="Start">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute name="Value" type="StartAndEndTypeType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="End">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute name="Value" type="EndType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>

XML Schema: how to ensure a "fixed" element to be not empty?

I have the following code:
<xs:element name="Lang" fixed="de-CH" nillable="false">
<xs:simpleType>
<xs:restriction base="xs:language">
<xs:minLength value="5"/>
<xs:maxLength value="5"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
I would like to ensure that the element Lang is not empty. If I delete fixed attribute, the validation for non-emptiness works. Is it a way to do it without removing fixed?
I managed to achieve both fixedness and non-emptyness using xs:pattern restriction:
<xs:element name="Lang">
<xs:simpleType>
<xs:restriction base="xs:language">
<xs:minLength value="5"/>
<xs:maxLength value="5"/>
<xs:pattern value="de-CH"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
What about
<xs:element name="Lang">
<xs:simpleType>
<xs:restriction base="xs:language">
<xs:enumeration value="de-CH" />
</xs:restriction>
</xs:simpleType>
</xs:element>

XSD - Restricting attribute values to another element attribute value

I have the following XML:
<Content name="contentName1">
<!-- Some sub elements here -->
</Content>
<Sequence Name="sequenceName1">
<Content name="contentName1" />
<!-- Some sub elements here -->
</Sequence>
with the following XSD
<xs:element maxOccurs="unbounded" name="Content">
<xs:complexType>
<xs:attribute name="Name" type="xs:string" use="required" />
<!-- other definitions here -->
</xs:complexType>
</xs:element>
<xs:element maxOccurs="unbounded" name="Sequence">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Content">
<xs:complexType>
<xs:attribute name="ContentName" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
In the XSD, how can I tell to the ContentName attribute of the Content elements of Sequence to only accepts value declared in the ContentName of Content elements?
e.g: with the XML provided above, only contentName1 will be accepted in the Content of sequence.
Identity constraint definitions are used for enforcing the unique, primary key and foreign key relations. you need to first define a key element for the content element and then use a keyref in the inner content element for the schema validator to enforce the condition you mentioned. Refer the below link it has some examples as well, also the tutorial in xfront for xsd covers some examples -
http://www.w3.org/TR/xmlschema11-1/#Identity-constraint_Definition_details
http://www.xfront.com/files/xml-schema.html
i am not good in xsd too, but maybe you will change <xs:attribute name="Name" type="xs:string" use="required" /> to <xs:attribute name="Name" type="contentNames" use="required" />
and create
<xs:simpleType name="contentNames" >
<xs:restriction base="xs:token">
<xs:enumeration value="contentName1"/>
<xs:enumeration value="contentName2"/>
<xs:pattern value="contentName[1234567890][1234567890]"/>
<xs:enumeration value="contentName1"/>
</xs:restriction>
</xs:simpleType>
for
<xs:pattern value="contentName[1234567890][1234567890]"/>
contentName1-99 but dont know if you can use <xs:enumeration/> too, you can try

XSD Definition for Enumerated Value

I'm stuck trying to define an XSD containing a field that can have only one of the following three values:
Green
Red
Blue
Essentially, I want to define a strict enumeration at the Schema level.
My First attempt appears wrong and I'm not sure about the "right" way to fix it.
<xs:element name="color">
<xs:complexType>
<xs:choice>
<xs:element name="green"/>
<xs:element name="red"/>
<xs:element name="blue"/>
</xs:choice>
</xs:complexType>
</xs:element>
By using an automatic XML generator, it treats those element names as string objects:
<xs0:color>
<xs0:green>text</xs0:green>
</xs0:color>
You can define an enumeration within the context of a simpleType.
<xs:simpleType name="color" final="restriction" >
<xs:restriction base="xs:string">
<xs:enumeration value="green" />
<xs:enumeration value="red" />
<xs:enumeration value="blue" />
</xs:restriction>
</xs:simpleType>
<xs:element name="SomeElement">
<xs:complexType>
<xs:sequence>
<xs:element name="Color" type="color" />
</xs:sequence>
</xs:complexType>
</xs:element>
This solution worked for me:
<xs:element name="color">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="green"/>
<xs:enumeration value="red"/>
<xs:enumeration value="blue"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

XSD complains with "Multiple elements with name 'B', with different types, appear in the model group."

I have the following XSD:
<xs:complexType name="typeBroken">
<xs:choice>
<xs:element name="B">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="FOO|BAR" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:sequence>
<xs:element name="A">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="5" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="B">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="3" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:choice>
</xs:complexType>
So, I would like the presence of 'A' to make 'B' to have a different validation. Is this possible? For example:
<test><B>FOO</B></test>
<test><A>HELLO</A><B>BAZ</B><test>
Should both validate. While:
<test><B>BAZ</B></test>
Should NOT validate. However, I am getting from xsd:
cos-element-consistent: Error for type 'typeBroken'. Multiple elements with name 'B', with different types, appear in the model group.
Do I understand your requirements correctly? You want to have a <B>...</B> and optionally an <A>....</A> before that (but not required)?
How about this schema then?
<xs:complexType name="typeBroken">
<xs:sequence>
<xs:element name="A" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="5" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="B">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="FOO|BAR" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:choice>
</xs:complexType>
Define a sequence where the first element, <A>, is optional (minOccurs="0") while the second one is not optional.
Does that solve your requirement?
Marc

Resources