I am using xsd to generate the object available in the OperationContract. The address, city, state and zip elements of the XSD are common
<xs:element name="Address" nillable="true">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
and used throughout the XML.
<xs:element ref="Address" />
When I compile the schema the classes generate correctly using the common elements.
When I run the service the OperationContext contains the expected request from the client:
<NameLast>Last</NameLast>
<NameFirst>First</NameFirst>
<Address xmlns="http://tempuri.org/">123 2nd St</Address>
<City xmlns="http://tempuri.org/">Somewhere</City>
However the common elements have the xmlns attribute (shown above) and in the received object all common elements contain null values.
My reputation is not high enough to show screenshot, but all data NOT in common elements are passed correct. Such as NameLast = "Last", Address = Null.
I am new to using Schemas and would appreciate any direction. Thanks.
I believe what you want is this, placed before your closing </xs:schema> tag:
<xs:simpleType name="AddressType">
<xs:restriction base="xs:string">
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
and used throughout the schema:
<xs:element name="Address" type="AddressType" />
Related
I am an experienced programmer but just recently took on a job maintaining an app that uses xml schema. They want to add some validation on an item that accepts Longitude. They want to continue to accept a blank and also 0, 0.0000000, or if another value is entered they want to make sure that at the least it is in the United States. (i.e. between -125 and -67)
The current xml schema simply allows any value.
<xs:element name="Location">
<xs:complexType>
<xs:sequence>
<xs:element name="LocLongitude"/>
</xs:sequence>
</xs:complexType>
</xs:element>
There are multiple venders sending this info in. Here is an example of what they may send:
<Location>
<LocLongitude xsi:type="xsd:string"></LocLongitude>
</Location>
Now looking at what the users want I found that I can use a union to encapsulate multiple checks. This is what I am using now.
<xs:element name="Location">
<xs:complexType>
<xs:sequence>
<xs:element name="LocLongitude" nillable="true">
<xs:simpleType>
<xs:union>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:double">
<xs:minInclusive value="0.00000000"/>
<xs:maxInclusive value="0.00000000"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType>
<xs:restriction base="xs:double">
<xs:minInclusive value="-125"/>
<xs:maxInclusive value="-67"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
It validates correctly if I use:
<Location>
<LocLongitude />
</Location>
Now if I use what the current vendors are using (see below):
<Location>
<LocLongitude xsi:type="xsd:string"></LocLongitude>
</Location>
We get an error:
THE XSI:TYPE ATTRIBUTE VALUE '' IS NOT VALID FOR THE ELEMENT 'LOCLONGITUDE', EITHER BECAUSE IT IS NOT A TYPE VALIDLY DERIVED FROM THE TYPE IN THE SCHEMA, OR BECAUSE IT HAS XSI:TYPE DERIVATION BLOCKED.
My question is, can I get this to work while still allowing the vendors to include xsi:type="xsd:string"?
No, the type chosen for xs:type must be validly derived from the type provided by the associated element. You cannot on the one hand define a type that restricts the value space and on the other hand supports a broader xs:type declaration.
See also: How to restrict the value of an XML element using xsi:type in XSD?
I have written an application which receives many variation of XML requests. In our business we have to validate XMLs against XSD at the beginning of any request.
The problem:
As I said above I have to validate them at the beginning and those XMLs have almost the same schema and I need to write a general XSD for them.
I have provided some prototype XML for my question:
XML1:
<_9D94DEB4-7C2D-45A5-A4FB-89FB1CF20672>
<Param1>value</Param1>
<Type>Category</Type>
</_9D94DEB4-7C2D-45A5-A4FB-89FB1CF20672>
XML2: Almost the same schema but root element name is different and it has an extra child element.
<_7603DCD1-F270-43EA-86E3-0FB3161478F6>
<Param1>value</Param1>
<Type>Page</Type>
<SearchText>Sample</SearchText>
</_7603DCD1-F270-43EA-86E3-0FB3161478F6>
As you can see the root element names are different but their schema is almost the same, How could I write a general XSD for them?
Thanks in advance.
The only thing these two XML instances have in common is that both have a Type element whose value is a string. Calling that "almost the same schema" seems rather an exageration. But perhaps there is more commonality than you have shown us?
In principle XSD allows you to validate the instance against a global type in your schema, irrespective of the element name. Whether your particular schema processor provides an API to do that is another question.
Your schema could then simply define the top-level type:
<xs:complexType name="myTopLevelType">
<xs:sequence>
<xs:element name="Param1" type="xs:string"/>
<xs:element name="Type" type="xs:string"/>
etc
</xs:sequence>
</xs:complexType>
If you choose Saxon as your schema validator then you can invoke "validation-by-type" from the Java API but not from the command line. In fact, probably the easiest way to do it is to invoke the validation from XSLT:
<xsl:import-schema schemaLocation="mySchema.xsd"/>
then:
<xsl:copy-of select="doc('instance.xml')/*" type="myTopLevelType"/>
All you need to do is write each of roots as direct children of your schema element and define types in your XSD.
For example:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="_9D94DEB4-7C2D-45A5-A4FB-89FB1CF20672">
...
<xs:complexType>
<xs:attribute name="Param2" type="Param2" use="required">
</xs:attribute>
<xs:attribute name="Type" type="Type" use="required">
</xs:attribute>
<xs:attribute name="SearchText" type="SearchText" use="required">
</xs:attribute>
</xs:complexType>
...
</xs:element>
<xs:element name="_7603DCD1-F270-43EA-86E3-0FB3161478F6">
...
<xs:complexType>
<xs:attribute name="Param1" type="Param1" use="required">
</xs:attribute>
<xs:attribute name="Type" type="Type" use="required">
</xs:attribute>
</xs:complexType>
...
</xs:element>
</xs:schema>
<!-- Your defenition and restriction of types-->
<xs:simpleType name="Param1">
<xs:restriction base="xs:string">
</xs:simpleType>
<xs:simpleType name="Param2">
<xs:restriction base="xs:string">
</xs:simpleType>
<xs:simpleType name="Type">
<xs:restriction base="xs:string">
</xs:simpleType>
<xs:simpleType name="SearchText">
<xs:restriction base="xs:string">
</xs:simpleType>
I have a flag enumeration of commands that can be executed
[Flags]
public enum Operations
{
InstallNothing = 0,
InstallStateDatabase = 1,
InstallStateServer = 2,
InstallStoreDatabase = 4,
InstallStoreServer = 8,
InstallMaintenanceProgram = 16,
InstallOther=32
}
[XmlElement("Commands")]
public Operations Commands { get; set; }
I want to be able to read an XML file and parse it against an xsd. I have this section of my xsd attempting to handle the validation but i don't think this is right.
<xs:element name="commands" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="command" minOccurs="1" maxOccurs="unbounded" >
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="InstallNothing" />
<xs:enumeration value="InstallStateDatabase" />
<xs:enumeration value="InstallStateServer" />
<xs:enumeration value="InstallStoreDatabase" />
<xs:enumeration value="InstallStoreServer" />
<xs:enumeration value="InstallMaintenanceProgram" />
<xs:enumeration value="InstallOther" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
The XMLs are going to be created by hand so i don't want to store the int value as the creators will not know what the int values are supposed to be.
I want my C# code to remain unchanged if possible and redesign my XSD to reflect what is supposed to be in my C# code. Right now, some generated text XML that VS2013 generates has multiple different command elements. I know that is how my XSD is written, but this isn't want a I want. I want one element that has can have any of the strings in the enumeration. How do i set up this XSD and what would an example XML look like of this implementation sending multiple different commands.
I found my answer(s) at
xsd select multiple values from enumeration or equivalent type. I wasn't searching the right thing earlier...
i added a list to my commands element. Here is my xsd after the change:
<xs:element name="commands" maxOccurs="1" minOccurs="1">
<xs:simpleType>
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="InstallNothing" />
<xs:enumeration value="InstallStateDatabase" />
<xs:enumeration value="InstallStateServer" />
<xs:enumeration value="InstallStoreDatabase" />
<xs:enumeration value="InstallStoreServer" />
<xs:enumeration value="InstallMaintenanceProgram" />
<xs:enumeration value="InstallOther" />
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
</xs:element>
and an example xml using it is:
<commands>InstallNothing InstallStateDatabase InstallStateServer </commands>
I am using VS2010 and using the "add service reference" feature, to generate client classes from my WSDL. I am having a problem with one of my elements, which is defined in the WSDL as follows:
<xs:simpleType name="NumberType">
<xs:restriction base="xs:string">
<xs:enumeration value="ONE" />
<xs:enumeration value="TWO" />
<xs:enumeration value="THREE" />
</xs:restriction>
</xs:simpleType>
This type is used in one of my elements like this:
<xs:element name="NumberTypes">
<xs:simpleType>
<xs:list itemType="tns:NumberType" />
</xs:simpleType>
</xs:element>
The problem is that VS is converting this particular element to a string type, when it should be an enumeration. so it converts it to a string NumberTypes which has a get method returning numberTypesField also of type string.
I think the problem is related to the fact that my schema NumberTypes element uses the xs:list, with 'itemType' attribute. if I change this to xs:element with type="tns:NumberType" attribute instead then the enumeration is generated as it should be.
So how can I make the enumeration work with xs:list? Why is it not converting correctly in the first place?
Thanks for any help.
I haven't had much luck getting xs:list to serialize properly. Instead, I just allow for multiple instances of the same node, and .NET knows how to put it into a "list" or "array" properly.
<xs:element minOccurs="0" maxOccurs="unbounded" name="NumberTypes">
...
</xs:element>
I have an import file that needs to have skip and continue on specific errors. I want to ignore the errors for data type, min/max length, and required fields. I want to catch and display errors about items not formatted correctly and in the wrong location.
In this case the file contains a collection of people.
I want to catch are errors:
1: A Children node outside of a person node.
2: A Child outside of a person node.
3: A Person out side of the people node.
I want to ignore errors:
1: Child does not have a name.
2: Person does not have birth date.
<xs:element name="People">
<xs:complexType>
<xs:sequence>
<xs:element name="Person" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="FirstName" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="LastName" minOccurs="1" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="BirthDate" type="Date" minOccurs="1" maxOccurs="1"/>
<xs:element name="Children">
<xs:complexType>
<xs:sequence>
<xs:element name="Child" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:all>
<xs:element name="FirstName" minOccurs="0" maxOccurs="1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="BirthDate" type="Date" minOccurs="1" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
Change your schema as follows:
In the Firstname element declaration under Child, add an attribute, type="xs:string". Also, remove all the content of the element declaration (simpleType and so on). You can make the tag self-closing if you want.
In the Birthdate element declaration, change minOccurs from 1 to 0.
The first change removes the restriction that's currently placed on the child's name that the content be at least one character long. Adding the type attribute is necessary since you're removing the current definition of the element type.
The second change tells the validator that birth date is not required.
Make those changes and the XML that you want to validate, should.
Whether to stop or carry on when invalid input is presented is, in principle, entirely the consuming software's choice, so what you are describing is logically coherent, though perhaps a bit unusual. If you can get the information you need through the API you're using, no reason not to make the software behave as you describe, and that's probably the preferable option. (But I can't help you with it.)
If you can't get the required information through the API (some APIs do assume that validation is just a yes/no kind of thing), one possible fallback alternative would be to validate using a separate schema weakened as described in ssamuel's answer, so that the only validation errors are the ones you wish to regard as fatal.
That is: there are two ways to solve this problem: (1) move past the idea that you must always abort on invalid input: get the validator to give you more information so you can decide to stop or continue. Or (2) move past the idea that there is a a single schema that applies to all processing of the document: use one schema for document creation and a different schema for deciding what to do with the input.