How to validate string that contain 2 restriction - xsd-validation

I need to validate string with 2 restrictions. The string length is 6. First 3 elements is between [-1000-1000] and the other 3 elements is between [-3.14 -3.14].
For example: 1000 1000 900 3 2 3.14
I've tried this:
<xs:complexType name="aaa">
<xs:simpleContent>
<xs:extension base="bb">
<xs:attribute name="size" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="bb">
<xs:union memberTypes="listtry listtry2 ">
</xs:union>
</xs:simpleType>
<xs:simpleType name="listtry">
<xs:restriction base="mytry">
<xs:length value="6"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="mytry">
<xs:list itemType="try1"></xs:list>
</xs:simpleType>
<xs:simpleType name="try1">
<xs:restriction base="xs:double">
<xs:maxInclusive value="100000"/>
<xs:minInclusive value="-100000"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="listtry2">
<xs:restriction base="mytry2">
<xs:length value="6"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="mytry2">
<xs:list itemType="tryPI"> </xs:list>
</xs:simpleType>
<xs:simpleType name="tryPI">
<xs:restriction base="xs:double">
<xs:maxInclusive value="3.14"/>
<xs:minInclusive value="-3.14"/>
</xs:restriction>
</xs:simpleType>

You can create rules for each individual value (as you have done), but their is no way to say a list of values has different validation rules for each element.
The closest I think your going to get is an regex (xs:pattern) that restricts each value. Something like this
-?\d{1,4} -?\d{1,4} -?\d{1,4} -?\d(.\d+)? -?\d(.\d+)? -?\d(.\d+)?
(I'm not great at regex stuff, but I think this is close!)
A better approach perhaps is to break the 6 values out into separate 2 (maybe 6) element/attributes. Then you can apply the validation rules you have already. to each group of values.
I Splitting the value across 2 attributes you would get something like
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2017 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Thing">
<xs:complexType>
<xs:attribute name="Values3x1000" type="list3Value1000Type" />
<xs:attribute name="Value3Pi" type="list3ValuePiType" />
</xs:complexType>
</xs:element>
<xs:simpleType name="value1000Type">
<xs:restriction base="xs:double">
<xs:maxInclusive value="1000" />
<xs:minInclusive value="-1000" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="listValue1000Type">
<xs:list itemType="value1000Type" />
</xs:simpleType>
<xs:simpleType name="list3Value1000Type">
<xs:restriction base="listValue1000Type">
<xs:length value="3" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="valuePiType">
<xs:restriction base="xs:double">
<xs:maxInclusive value="3.14" />
<xs:minInclusive value="-3.14" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="listValuePiType">
<xs:list itemType="valuePiType" />
</xs:simpleType>
<xs:simpleType name="list3ValuePiType">
<xs:restriction base="listValuePiType">
<xs:length value="3" />
</xs:restriction>
</xs:simpleType>
</xs:schema>

Related

Schema validation failed xs:date

I am receiving "Schema validation failed" from the server when calling my WCF service.
The error message returned states that the validation is failing on an element of type "date"
Response from server
<s:Fault>
<s:Code>
<s:Value>s:VCE1001</s:Value>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-GB">Schema validation failed.</s:Text>
</s:Reason>
<s:Detail>
<FaultDetails xmlns="http://schemas.datacontract.org/2004/07/project.Error_Handler" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Information>Element Name: ExampleDateElement, Line Number: 0, Line Position: 0</Information>
</FaultDetails>
</s:Detail>
</s:Fault>
Request to server
<Message xsi:type="q1:Example_OutboundMessage" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:q1="project_messages">
<q1:Element1>123456789123</q1:Element1>
<q1:Element2>
<Nested1>string123</Nested1>
<Nested2>string123</Nested2>
</q1:Element2>
<q1:Element3>2017-03-28</q1:Element3>
<q1:ExampleDateElement>2017-03-29</q1:ExampleDateElement>
<q1:Element4>64bc7ab8-b418-4d23-8d31-47312e838c92</q1:Element4>
</Message>
Schema
<xs:complexType name="Example_OutboundMessage">
<xs:complexContent>
<xs:extension base="vct:OutboundMessage_Type">
<xs:sequence>
<xs:element name="Element1" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:documentation>
Info here
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="12" />
<xs:maxLength value="13" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Element2" minOccurs="1" maxOccurs="1" type="vct:Example_Type">
<xs:annotation>
<xs:documentation>
Info here.
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="Element3" minOccurs="1" maxOccurs="1" type="xs:date">
<xs:annotation>
<xs:documentation>
Info here.
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="ExampleDateElement" minOccurs="1" maxOccurs="1" type="xs:date">
<xs:annotation>
<xs:documentation>
Info here.
</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="Element4" minOccurs="1" maxOccurs="1">
<xs:annotation>
<xs:documentation>
Info here.
</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1" />
<xs:maxLength value="255" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:extension>
</xs:complexContent>
Cannot see anything wrong with the date that is passed in for "ExampleDateElement".
Also the schema seems to pass for the date element above, even though the date formats are the same. It even fails if the dates are exactly the same.

Can I add a attribute and a restriction to a xsd type inline?

I need to imply a restriction and an attribute on a type. I know it can be done this way
<xs:simpleType name="Name">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="15"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="MyCustomeName">
<xs:simpleContent>
<xs:extension base="mc:Name">
<xs:attribute name="MyTypeOfName" fixed="MCN"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
Is there a way to do all in one line "inline"
I can add the restriction inline but then I cannot add the attribute as in
<xs:simpleType name="MyCustomName">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="15"/>
<xs:attribute name="MyTypeOfName" fixed="MCN"/> --> This gives an error
</xs:restriction>
</xs:simpleType>
The reason why I want to do this... is we expose the XSD to a 3rd party and they have issues with the inheritance of the types hence I want to do all inline.
It's not possible to merge a restriction and extension into one block in XML schema. The solution that you show with <simpleType> and <complexType> IMHO is the way to do so.
If you don't want to define the <complexType> you can define directly the <element>, but it's basically the same that you're doing without the possibility to reuse your <complexType>:
<xs:simpleType name="Name">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="15"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="MyCustomeName">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="Name">
<xs:attribute name="MyTypeOfName" fixed="MCN"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Hope this help,

How to remove or exclude a schema that WCF adds that isn't used?

I've created a webservice in Visual studio with WCF and when looking at the generated WSDL it includes a reference to http://schemas.microsoft.com/2003/10/Serialization which contains a lot of type specifications. I do not use any of these specifications. Does anyone know why WCF adds this and if there's any way to get rid of it?
The xml added to my wsdl looks like this when using the singleWsdl argument on my service:
<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="anyType" nillable="true" type="xs:anyType" />
<xs:element name="anyURI" nillable="true" type="xs:anyURI" />
<xs:element name="base64Binary" nillable="true" type="xs:base64Binary" />
<xs:element name="boolean" nillable="true" type="xs:boolean" />
<xs:element name="byte" nillable="true" type="xs:byte" />
<xs:element name="dateTime" nillable="true" type="xs:dateTime" />
<xs:element name="decimal" nillable="true" type="xs:decimal" />
<xs:element name="double" nillable="true" type="xs:double" />
<xs:element name="float" nillable="true" type="xs:float" />
<xs:element name="int" nillable="true" type="xs:int" />
<xs:element name="long" nillable="true" type="xs:long" />
<xs:element name="QName" nillable="true" type="xs:QName" />
<xs:element name="short" nillable="true" type="xs:short" />
<xs:element name="string" nillable="true" type="xs:string" />
<xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte" />
<xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt" />
<xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong" />
<xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort" />
<xs:element name="char" nillable="true" type="tns:char" />
<xs:simpleType name="char">
<xs:restriction base="xs:int" />
</xs:simpleType>
<xs:element name="duration" nillable="true" type="tns:duration" />
<xs:simpleType name="duration">
<xs:restriction base="xs:duration">
<xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?" />
<xs:minInclusive value="-P10675199DT2H48M5.4775808S" />
<xs:maxInclusive value="P10675199DT2H48M5.4775807S" />
</xs:restriction>
</xs:simpleType>
<xs:element name="guid" nillable="true" type="tns:guid" />
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}" />
</xs:restriction>
</xs:simpleType>
<xs:attribute name="FactoryType" type="xs:QName" />
<xs:attribute name="Id" type="xs:ID" />
<xs:attribute name="Ref" type="xs:IDREF" />
</xs:schema>
Maybe it's too late now, but I ran into this issue and was able to solve it. The reason I wanted to remove it was because I was generating clients in both Java and RPG, and the wsdl2java and wsdl2rpg tools generate large, ugly data structures based on that schema.
In my case, it was because I was using the DataContract serializer, and some of the objects I was serializing had members of types that didn't have the [DataContract] attribute. To make that extra schema go away, everything being serialized, all the way down the type tree needed to have that attribute.
It also had this problem whenever a there was an inherited public member on the service contracts.
I agree there is no need to remove it. Also many times it is used and you do not know about it. The way to change default wsdl generatio is by implementing IWsdlExportExtension:
http://msdn.microsoft.com/en-us/library/system.servicemodel.description.iwsdlexportextension.aspx
The XSD definition for microsoft's WSDL implementation imports this namespace (see here).
Why do you want to remove it?

Can we add new attribute or change type of existing attribute to a "Referenced Element"?

In my XML schema I have an element being referenced tens of times by other elements but with different enumerated values for one of its attribute.
For now, instead of creating this element in global space and referencing it later, I am creating a new instance wherever it is needed. This approach has increased my schema size enormously because of repeated creation of almost same element many times. It also may have adverse effect on efficiency of the schema.
The only way that I see is to create element once and then reference it many times but my problem is: one of the attribute of this referenced element is required to have a different set of enumerations for each referencing element.
My question is:
Is it possible to to add an attribute to a "Referenced Element" in XML Schema?
Something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.myDomain.com" xmlns="http://www.myDomain.com" elementFormDefault="qualified">
<xs:simpleType name="myValues1">
<xs:restriction base="xs:string">
<xs:enumeration value="value1" />
<xs:enumeration value="value2" />
</xs:restriction>
</xs:simpleType>
<xs:element name="myElement">
<xs:complexType mixed="true">
<xs:attribute name="attr1" type="xs:string" />
<xs:attribute name="attr2" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="MainElement1">
<xs:complexType>
<xs:sequence>
<xs:element ref="myElement">
<xs:complexType>
<xs:attribute name="myAtt" type="myValues1" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="mainAtt1" />
</xs:complexType>
</xs:element>
</xs:schema>
Or can we change type of an existing attribute of a "Referenced Element" in XML Schema?
something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.myDomain.com" xmlns="http://www.myDomain.com" elementFormDefault="qualified">
<xs:simpleType name="myValues1">
<xs:restriction base="xs:string">
<xs:enumeration value="value1" />
<xs:enumeration value="value2" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="myValues2">
<xs:restriction base="xs:string">
<xs:enumeration value="value3" />
<xs:enumeration value="value4" />
</xs:restriction>
</xs:simpleType>
<xs:element name="myElement">
<xs:complexType mixed="true">
<xs:attribute name="attr1" type="xs:string" />
<xs:attribute name="attr2" type="xs:string" />
<xs:attribute name="myAtt" type="myValues1" />
</xs:complexType>
</xs:element>
<xs:element name="MainElement1">
<xs:complexType>
<xs:sequence>
<xs:element ref="myElement">
<xs:complexType>
<xs:attribute name="myAtt" type="myValues2" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="mainAtt1" />
</xs:complexType>
</xs:element>
</xs:schema>
You cannot override the content model of a referenced element. The point of the reference is that it points to exactly the same element every time.
If you really want the element to have different content, you are better off defining multiple global complex types and using them, rather than using an element reference:
<xs:complexType name="Type1" mixed="true">
<xs:attribute name="attr1" type="xs:string" />
<xs:attribute name="attr2" type="xs:string" />
<xs:attribute name="myAtt" type="myValues1" />
</xs:complexType>
<xs:complexType name="Type2" mixed="true">
<xs:complexContent>
<xs:extension base="Type1">
<xs:attribute name="myAtt2" type="myValues2" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="MainElement1">
<xs:complexType>
<xs:sequence>
<xs:element name="myElement" type="Type1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
I would not worry too much about performance. The reference to the global types will be resolved only once at schema load time.

XML and Sql Server 2005

i have a problem that I want to resolve in a best possible way. The thing is that I made a schema that looks like this:
print("
<xs:complexType name="rentACarT">
<xs:sequence>
<xs:element name="poslovnice" type="poslovniceT" />
<xs:element name="korisnici" type="korisniciT" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="poslovniceT">
<xs:sequence>
<xs:element name="poslovnica" type="poslovnicaT" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="poslovnicaT">
<xs:sequence>
<xs:element name="naziv" type="xs:string" />
<xs:element name="adresa" type="adresaT" />
<xs:element name="grad" type="gradT" />
<xs:element name="vozila" type="vozilaT" />
<xs:element name="zaposlenici" type="zaposleniciT" />
<xs:element name="posudbe" type="posudbeT" />
</xs:sequence>
<xs:attribute name="id" type="xs:int" />
</xs:complexType>
<xs:complexType name="vozilaT">
<xs:sequence>
<xs:element name="vozilo" type="voziloT" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="zaposleniciT">
<xs:sequence>
<xs:element name="zaposlenik" type="zaposlenikT" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="korisniciT">
<xs:sequence>
<xs:element name="korisnik" type="korisnikT" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="posudbeT">
<xs:sequence>
<xs:element name="posudba" type="posudbaT" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="posudbaT">
<xs:sequence>
<xs:element name="idVozila" type="xs:int" />
<xs:element name="jmbgZaposlenika" type="jmbgT" />
<xs:element name="jmbgKorisnika" type="jmbgT" />
<xs:element name="datumPosudbe" type="xs:date" />
<xs:element name="ugovoreniDatumPovratka" type="xs:date" />
<xs:element name="stvarniDatumPovratka" type="xs:date" />
</xs:sequence>
<xs:attribute name="id" type="xs:integer" />
</xs:complexType>
<xs:complexType name="voziloT">
<xs:sequence>
<xs:element name="registracija" type="xs:string" />
<xs:element name="modelVozila" type="modelVozilaT" />
<xs:element name="godinaProizvodnje" type="godinaT" />
<xs:element name="cijenaPosudbePoDanu" type="xs:double" />
</xs:sequence>
<xs:attribute name="id" type="xs:int" />
</xs:complexType>
<xs:complexType name="zaposlenikT">
<xs:sequence>
<xs:element name="ime" type="xs:string" />
<xs:element name="prezime" type="xs:string" />
<xs:element name="spol" type="spolT" />
<xs:element name="datumZaposlenja" type="xs:date" />
<xs:element name="grad" type="xs:double" />
</xs:sequence>
<xs:attribute name="jmbg" type="jmbgT" />
</xs:complexType>
<xs:complexType name="korisnikT">
<xs:sequence>
<xs:element name="ime" type="xs:string" />
<xs:element name="prezime" type="xs:string" />
<xs:element name="spol" type="spolT" />
<xs:element name="adresa" type="adresaT" />
<xs:element name="grad" type="gradT" />
<xs:element name="status" type="statusKorisnikaT" />
</xs:sequence>
<xs:attribute name="jmbg" type="jmbgT" />
</xs:complexType>
<xs:complexType name="modelVozilaT">
<xs:sequence>
<xs:element name="tipVozila" type="tipVozilaT" />
<xs:element name="marka" type="xs:string" />
<xs:element name="model" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="tipVozilaT">
<xs:restriction base="xs:string">
<xs:enumeration value="auto" />
<xs:enumeration value="kombi" />
<xs:enumeration value="kamion" />
<xs:enumeration value="limuzina" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="godinaT">
<xs:restriction base="xs:int">
<xs:minInclusive value="1980" />
<xs:maxInclusive value="2050" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="jmbgT">
<xs:restriction base="xs:string">
<xs:pattern value="([0-9]){13}" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="spolT">
<xs:restriction base="xs:string">
<xs:enumeration value="m" />
<xs:enumeration value="f" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="statusKorisnikaT">
<xs:restriction base="xs:string">
<xs:enumeration value="stalni" />
<xs:enumeration value="novi" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="gradT">
<xs:sequence>
<xs:element name="postanskiBroj" type="xs:int" />
<xs:element name="naziv" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="adresaT">
<xs:sequence>
<xs:element name="ulica" type="xs:string" />
<xs:element name="kucniBroj" type="xs:int" />
</xs:sequence>
</xs:complexType>
");
Now, I don't want to put the entire xml document into one row. At least I think it is bad.
I would like to move "poslovnice"(contains multiple "poslovnica") in one table and "vozila" in other...
And is it better to hold each "poslovnica" in one row in table Poslovnica or have all elements "poslovnica" in one row?
And what about uniqueness since SQL Server doesn't support unique xml data type, I've seen that you can use triggers and functions, is there any other way?
Also, if I break the xml document, and have poslovnice in one table and their vozila in other, how to achieve to show that vozilo belongs to poslovnica, should I put id of poslovnica in each vozilo in xml or should I reference rows in SQL Server?
Or would you reccomend some other database?
Thank you very much, I am sorry if I wasn't clear enough :( !
Miroslav
well first you have to ask yourself why do you want it stored in XML in the db?
this seems like a perfect scenario to have tables Poslovnica, Vozilo and Zaposlenik and have intermediate tables that hold info on which employee rented what car where.
i don't see a reason to store this in xml at all.