I have created some XSD schemas and receive the below error when trying to read from it in C#:
The element PartnerPSTNTransfer in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0' has invalid child element 'StartDate' in namespace 'http://localhost/Orders-Connection-v1-0'.
List of possible elements expected: 'StartDate' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0'.
However the expected element is StartDate and it is present in the xml file so I am unsure why this error would be shown. I'll attatch the .xsd for the schemas i created:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="PartnerPSTN"
targetNamespace="http://localhost/Orders-PartnerPSTN-v1.0"
elementFormDefault="qualified"
xmlns="http://localhost/Orders-PartnerPSTN-v1.0"
xmlns:mstns="http://localhost/Orders-PartnerPSTN-v1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:a="http://localhost/Orders-Address-v1-0"
xmlns:c="http://localhost/Orders-Common-v1-0" >
<xs:simpleType name="Contract">
<xs:restriction base="xs:token">
<xs:enumeration value="Monthly_12"></xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="PackageOption">
<xs:restriction base="xs:string">
<xs:enumeration value="Data Only" />
<xs:enumeration value="Free Evening and Weekend" />
<xs:enumeration value="1000 Anytime" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="FeatureOption">
<xs:list>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Feature0"/>
<xs:enumeration value="Feature0.A"/>
<xs:enumeration value="Feature0.B"/>
<xs:enumeration value="Feature0.C"/>
<xs:enumeration value="Feature1"/>
<xs:enumeration value="Feature2"/>
<xs:enumeration value="Feature2.A"/>
<xs:enumeration value="Feature3"/>
<xs:enumeration value="Feature3.A"/>
<xs:enumeration value="Feature3.B"/>
<xs:enumeration value="Feature3.C"/>
</xs:restriction>
</xs:simpleType>
</xs:list>
</xs:simpleType>
<xs:complexType name="PartnerPSTNConfiguration">
<xs:sequence>
<xs:element name="Package" type="PackageOption" />
<xs:element name="Feature" type="FeatureOption" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Schema 2:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="PartnerPSTNTransfer"
targetNamespace="http://localhost/Orders-PartnerPSTNTransfer-v1-0"
elementFormDefault="qualified"
xmlns="http://localhost/Orders-PartnerPSTNTransfer-v1-0"
xmlns:mstns="http://localhost/Orders-PartnerPSTNTransfer-v1-0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:a="http://localhost/Orders-Address-v1-0"
xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0"
xmlns:c="http://localhost/Orders-Common-v1-0"
xmlns:conn="http://localhost/Orders-Connection-v1-0">
<xs:import namespace="http://localhost/Orders-Common-v1-0" schemaLocation="../../Common.xsd" />
<xs:import namespace="http://localhost/Orders-PartnerPSTN-v1.0" schemaLocation="PartnerPSTN.xsd" />
<xs:complexType name="PartnerPSTNTransfer">
<xs:sequence>
<xs:element name="TelephoneNumber" type="c:Landline" />
<xs:element name="StartDate" type="xs:date" />
<xs:element name="Postcode" type="c:RequiredString" />
<xs:element name="Configuration" type="padsl:PartnerPSTNConfiguration" />
</xs:sequence>
</xs:complexType>
<xs:element name="PartnerPSTNTransfer" type="PartnerPSTNTransfer"></xs:element>
</xs:schema>
EDIT
Here is my XML I am trying to validate:
<p:PartnerPSTNTransfer xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0"
xmlns:p="http://localhost/Orders-PartnerPSTNTransfer-v1-0"
xmlns:a="http://localhost/Orders-Address-v1-0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://localhost/Orders-Connection-v1-0">
<p:TelephoneNumber>01111111111</p:TelephoneNumber>
<c:StartDate>2014-10-10T00:00:00</c:StartDate>
<c:Postcode>XX1 1XX</c:Postcode>
<p:Configuration>
<padsl:Contract>MA</padsl:Contract>
<padsl:PackageOption>Weekend</padsl:PackageOption>
<padsl:Featureoption>Feature0 Feature2.A</padsl:Featureoption>
</p:Configuration>
</p:PartnerPSTNTransfer>
EDIT 2
XML so far. I beleive I've removed the original problem although it now says:
The element 'Configuration' in namespace 'http://localhost/Orders-PartnerPSTNTransfer-v1-0' has invalid child element 'Contract' in namespace 'http://localhost/Orders-PartnerPSTN-v1.0'.
and the XML being:
<p:PartnerPSTNTransfer xmlns:padsl="http://localhost/Orders-PartnerPSTN-v1.0"
xmlns:p="http://localhost/Orders-PartnerPSTNTransfer-v1-0"
xmlns:a="http://localhost/Orders-Address-v1-0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://localhost/Orders-Connection-v1-0">
<p:TelephoneNumber>01111111111</p:TelephoneNumber>
<p:StartDate>2014-10-10</p:StartDate>
<p:Postcode>EX20 1LP</p:Postcode>
<p:Configuration>
<padsl:Package>Free Evening and Weekend</padsl:Package>
<padsl:Feature>Feature0 Feature2.A</padsl:Feature>
<padsl:Contract>Monthly_12</padsl:Contract>
</p:Configuration>
</p:PartnerPSTNTransfer>
Your xml instance says it's looking for an element called StartDate which is defined somewhere in a schema under the namespace http://localhost/Orders-Connection-v1-0:
<c:StartDate>2014-10-10T00:00:00</c:StartDate>
where:
xmlns:c="http://localhost/Orders-Connection-v1-0"
Your schema, however, defines this element as belonging to a type called PartnerPSTNTransfer under the namespace http://localhost/Orders-PartnerPSTNTransfer-v1-0
So you need to modify your instance so that StartDate references the correct namespace it's containing type was defined under. You have already referenced the namespace at root level under the namespace prefix of "p" so you should be able to do:
<p:StartDate>2014-10-10T00:00:00</p:StartDate>
(And the same for PostCode element).
EDIT
In response to the second part of your question, the xml instance resolves to an element called Contract defined in type PartnerPSTNConfiguration in namespace http://localhost/Orders-PartnerPSTN-v1.0
However the definition for PartnerPSTNConfiguration in the schema does not contain a definition for Contract, only for Package and Feature:
<xs:complexType name="PartnerPSTNConfiguration">
<xs:sequence>
<xs:element name="Package" type="PackageOption" />
<xs:element name="Feature" type="FeatureOption" />
</xs:sequence>
</xs:complexType>
So to be valid you have to model the Contract element in the PartnerPSTNConfiguration type definition, eg:
<xs:complexType name="PartnerPSTNConfiguration">
<xs:sequence>
...
<xs:element name="Contract" type="SomeType" />
</xs:sequence>
</xs:complexType>
Or, change your instance to not expect it to be there.
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 would like to allow an element to be a xs:date or an empty string.
Here's an XML Schema that I've tried:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:lp="urn:oio:ebst:diadem:lokalplan:1"
targetNamespace="urn:oio:ebst:diadem:lokalplan:1"
elementFormDefault="qualified" xml:lang="DA"
xmlns:m="urn:oio:ebst:diadem:metadata:1">
<xs:import schemaLocation="../key.xsd" namespace="urn:oio:ebst:diadem:metadata:1" />
<xs:element name="DatoVedtaget" type="lp:DatoVedtagetType" />
<xs:complexType name="DatoVedtagetType">
<xs:simpleContent>
<xs:extension base="xs:date">
<xs:attribute ref="m:key" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="DatoVedtagetTypeString">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="m:key" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
I want the element to be DatoVedtagetType in a case it includes a value, and I want it to be DatoVedtagetTypeString if it is empty. How I implement such a conditional functionality this schema?
Per comments on the question, the goal is to have DatoVedtaget be a xs:date or empty. Here is a way to express such a constraint:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:lp="urn:oio:ebst:diadem:lokalplan:1"
xmlns:m="urn:oio:ebst:diadem:metadata:1"
targetNamespace="urn:oio:ebst:diadem:lokalplan:1"
elementFormDefault="qualified"
xml:lang="DA">
<xs:import schemaLocation="../key.xsd" namespace="urn:oio:ebst:diadem:metadata:1" />
<xs:element name="DatoVedtaget" type="lp:DatoVedtagetType" />
<xs:simpleType name="empty">
<xs:restriction base="xs:string">
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="dateOrEmpty">
<xs:union memberTypes="xs:date lp:empty"/>
</xs:simpleType>
<xs:complexType name="DatoVedtagetType">
<xs:simpleContent>
<xs:extension base="lp:dateOrEmpty">
<xs:attribute ref="m:key" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
Rather than using a union type as #kjhughes does, my own preferred solution is to use a list type allowing zero or one occurrences:
<xs:simpleType name="dateOrEmpty">
<xs:list itemType="xs:date" maxLength="1"/>
</xs:simpleType>
One reason for the preference is that it's less code. Another reason is that if you're writing schema-aware XSLT or XQuery code, the resulting value is easier to manipulate (the atomized value has type xs:date? which is easier to manipulate, e.g. to test for empty, than a union type).
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 trying to validate an Xml file
My xsd schema fragment:
<xs:attribute name="PostIndex" use="optional">
<xs:annotation>
<xs:documentation>Post Index</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="6"/>
<xs:pattern value="\d{0}|\d{6}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
XML file fragment:
<Atr1>
<Atr2 Atr="A9F130BE-3974-4698-B9F9-72037BC0E97F" PostIndex="123456" />
<Atr2 Atr3="123" Atr4="11111" />
</Atr1>
when I run the validation code, it passes schema validation I have error:
The 'PostIndex' attribute is invalid - The value '123456' is invalid
according to its datatype 'String' - The Enumeration constraint
failed.
Here is the XSD and XML I used, it works fine. So please post your entire XSD and XML
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="computer">
<xs:annotation>
<xs:documentation xml:lang="it-IT">Definizione di un computer</xs:documentation>
<xs:documentation xml:lang="en-US">Definition of a computer</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute name="PostInt">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="6"/>
<xs:pattern value="\d{0}|\d{6}"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>
XML
<computer PostInt="123456" />
I used following online validator