XSD: specify a choice must have at least one from a list but no more than one of each - xsd

This has been driving me mad for hours. I've read every relevant XSD question on SO and the rest of the Internet it seems and still the answer eludes me.
I need an XML schema that requires at least one of list of elements be present, but each element may appear only 0 or 1 times.
This is similar to this question:
XML schema construct for "any one or more of these elements but must be at least one"
but I was unable to constrain the upper limit: I am apparently using maxOccursincorrectly.
Here's where I left off with my schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Selects">
<xs:sequence minOccurs="2" maxOccurs="4">
<xs:choice>
<xs:element name="aaa" minOccurs="1" maxOccurs="1"/>
<xs:element name="bbb" minOccurs="1" maxOccurs="1"/>
<xs:element name="ccc" minOccurs="1" maxOccurs="1"/>
<xs:element name="ddd" minOccurs="1" maxOccurs="1"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:element name="baseElement">
<xs:complexType>
<xs:sequence>
<xs:element name="MyChoice" type="Selects"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I've tried the minOccurs and maxOccurs on the choice and the element with no luck. Here is XML that validates, though I don't want it to:
<?xml version="1.0" encoding="UTF-8"?>
<baseElement xsi:noNamespaceSchemaLocation="myTest.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyChoice>
<ddd/>
<ddd/>
</MyChoice>
</baseElement>
Here's an example of what I would like, if possible:
<?xml version="1.0" encoding="UTF-8"?>
<baseElement xsi:noNamespaceSchemaLocation="myTest.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyChoice>
<ddd/>
<aaa/>
<ccc/>
</MyChoice>
</baseElement>
I would like it to complain about the multiple ddd elements but allow any or all of the others in any order. I get an error if I only have one element under MyChoice so at least something works.
What am I doing wrong? How do I prevent multiple of the same element from validating?
UPDATE
This was my solution (from comments on answer below):
Actually, xs:all did the trick. I swapped the choice for all and added minOccurs="0" maxOccurs="1" to each element. With xs:all, minOccurs must be either 0 or 1 and maxOccurs must be 1. Thanks for your help - I'm good to go now!

Just move the <xs:sequence minOccurs="2" maxOccurs="4"> from around the choice to the point where you want to use it further down. (you can also remove the min/max occurs = 1 as this is what xs:choice does)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="Selects">
<xs:choice>
<xs:element name="aaa" />
<xs:element name="bbb" />
<xs:element name="ccc" />
<xs:element name="ddd" />
</xs:choice>
</xs:complexType>
<xs:element name="baseElement">
<xs:complexType>
<xs:sequence minOccurs="2" maxOccurs="4">
<xs:element name="MyChoice" type="Selects" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This validates the following:
<baseElement xsi:noNamespaceSchemaLocation="myTest.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyChoice>
<bbb></bbb>
</MyChoice>
<MyChoice>
<ccc></ccc>
</MyChoice>
</baseElement>
UPDATE
I think you are reaching the limit of what you can achieve with XSD. I can't see any way you can do this other than by defining a "version" of the MyChoice type for every single combination possible (which will then need different names MyChoice1, MyChoice2 etc)
You can also use xs:all
<xs:complexType name="Selects">
<xs:all minOccurs=2 maxOccurs=4>
<xs:element name="aaa" />
<xs:element name="bbb" />
<xs:element name="ccc" />
<xs:element name="ddd" />
</xs:all>
</xs:complexType>
but this will not prevent you from having four <ddd/>'s

Related

XSD field dependent on another field [duplicate]

I have an XSD to validate an XML file. The structure is as follows:
<root>
<child>
<size>2</size>
<childElement>Element 1</childElement>
<childElement>Element 2</childElement>
</child>
</root>
The number of childElements is dependent on the size provided i.e. if size was set as 3, not more than 3 childElements can be added.
I have tried using xs:alternative but it does not seem to work:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="child" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="size" type="xs:integer" maxOccurs="1"/>
<xs:element name="childElement" type="xs:string" maxOccurs="1">
<xs:alternative test="#size>1" maxOccurs="#size"/>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Is there a way of using xs:alternative or another tag to achieve this, or is this outside the realm of possibility with XSD?
Design recommendation: If your XML design can still be changed, eliminate the size element and convey that information implicitly rather than explicitly. By eliminating the duplication of information, you'll not need to check that the duplication is consistent.
If your XML design cannot still be changed, or if you choose not to change it...
XSD 1.0
Not possible. Would have to be checked out-of-band wrt XSD.
XSD 1.1
Possible using xs:assert:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
vc:minVersion="1.1">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="child">
<xs:complexType>
<xs:sequence>
<xs:element name="size" type="xs:integer"/>
<xs:element name="childElement" maxOccurs="unbounded"/>
</xs:sequence>
<xs:assert test="count(childElement) = size"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

XSD circular imports

Is it valid to have two xsd's import each other?
For example, the first one is 'MyService.xsd=48 and it looks like:
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:tns="http://documentation" xmlns:ns1="http://documentHistory" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" version="1.0" targetNamespace="http://documentation">
<xs:import namespace="http://documentHistory" schemaLocation="MyService.xsd=49" />
<xs:complexType name="item">
<xs:sequence>
<xs:element name="internalId" type="xs:long" minOccurs="0" />
<xs:element name="readOnly" type="xs:boolean" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="data">
<xs:sequence>
<xs:element name="histories" type="ns1:history" nillable="true" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
The second xsd is MyService.xsd=49 and looks like:
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:tns="http://documentHistory" xmlns:ns1="http://documentation" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" version="1.0" targetNamespace="http://documentHistory">
<xs:import namespace="http://documentation" schemaLocation="MyService.xsd=48" />
<xs:complexType name="history">
<xs:complexContent>
<xs:extension base="ns1:item">
<xs:sequence>
<xs:element name="dateReceived" type="xs:dateTime" minOccurs="0" />
<xs:element name="dateSent" type="xs:dateTime" minOccurs="0" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
Notice that the first one imports the second one and the second one imports the first one. I have an issue that I'm trying to track down and I don't know if this is part of the problem or not. The issue I'm trying to solve has to do with TCL not seeming to parse my wsdl correctly. After TCL parses the wsdl into a dict, the 'history' object doesn't have the properties from the 'item' object, even though the xsd shows that history should extend from item.
The question is: is it valid for two xsd's to import each other or does this look like a problem?
Also, I already looked at this post XSD circular import but found the answer difficult to understand. Any help / insight would be greatly appreciated. Thanks!

"Type [namespace:type] is not declared" yet is imported correctly, I think

Im using Visual Studio 2013 to develop an XSD Schema that imports another schema for use of its "Neck" type. For some reason Visual Studio doesnt like my use of type="wn:Neck", resulting in the error mentioned in the title. Below is my parent schema and after that is the child schema. Visually the schema looks correct but VS2013 disagrees. Does anyone know why this is happening? Ive seen similar questions but haven't found a direct solution to this issue.
Parent
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wn="http://fxb.co/xsd/warmoth/neck.xsd"
targetNamespace="http://fxb.co/xsd/warmoth/customitem.xsd">
<xs:import namespace="http://fxb.co/xsd/warmoth/neck.xsd" schemaLocation="./Neck.xsd"/>
<xs:element name="CustomItems">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomItemOption">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Neck" type="wn:Neck" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Child
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://fxb.co/xsd/warmoth/neck.xsd" >
<xs:element id="Neck" name="Neck">
<xs:complexType>
<xs:sequence>
<xs:element name="Headstock">
<xs:complexType>
<xs:attribute name="active" type="xs:boolean" default="true" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
In the parent XSD, change
<xs:element minOccurs="0" maxOccurs="unbounded" name="Neck" type="wn:Neck" />
to
<xs:element minOccurs="0" maxOccurs="unbounded" ref="wn:Neck" />
because you wish to reference the Neck element, not type, from the namespace of the child XSD.

Restricting number of element count in <all> XSD Schema

What I'm trying to do is, declare an parent element called "data", which are having 10 sub element of these one element are conditional.
My XSD is:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="data" >
<xs:complexType>
<xs:sequence>
<xs:element name="sub_data" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all >
<xs:element ref="A"/>
<xs:element ref="B" minOccurs="0" maxOccurs="1"/>
<xs:element ref="C"/>
<xs:element ref="D"/>
<xs:element ref="E"/>
<xs:element ref="F"/>
<xs:element ref="G"/>
<xs:element ref="H"/>
<xs:element ref="I"/>
<xs:element ref="J"/>
<xs:element ref="K"/>
<xs:element ref="L"/>
<xs:element ref="M"/>
<xs:element ref="N"/>
<xs:element ref="element_group"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="status"/>
</xs:complexType>
</xs:element>
<xs:element name="O" type="xs:string" substitutionGroup="element_group">
<xs:element name="P" type="xs:string" substitutionGroup="element_group">
<xs:element name="Q" type="xs:string" substitutionGroup="element_group">
</xs:schema>
Requirement is:
All element from A to N are appearing in any order.
Element P,Q and R is also part of data but only one element appear from among 3. Order is also any.
More important I have cover 1st and 2nd point but I want one more restriction is that only and only four element will be become the part of <data> that means element count from <A> to <Q> is exact four,
Combination can any of them from <A> to <Q> but final count is only four, please help me.
Now currently i am unable to set maxOccures in <all> , it not compiling the xsd after setting maxOccures.
The simplest way to handle this is probably to use XSD 1.1 and use an assertion on the parent to specify that there must be exactly (or at most) four children. You will also need to make each child of the all-group optional, since thirteen of them will not appear.
The best way might be to redesign your XML to work better with your schema language instead of fighting it. It's hard to give advice on that, though, since your example is abstract enough to make it unclear why you are imposing the requirements you mention.

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found

I am trying to understand <any> element in xsd. I had two xsds.
Book Catalogue.xsd
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="BookCatalogue">
<xs:complexType>
<xs:sequence>
<xs:element name="Book" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Title" type="xs:string" />
<xs:element name="Author" type="xs:string" />
<xs:element name="Date" type="xs:string" />
<xs:element name="ISBN" type="xs:string" />
<xs:element name="Publisher" type="xs:string" />
<xs:any namespace="##any" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Reviewer.xsd
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="Reviewer">
<xs:complexType>
<xs:sequence>
<xs:element name="Name">
<xs:complexType>
<xs:sequence>
<xs:element name="First" type="xs:string" />
<xs:element name="Last" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But if i validate the below xml based on above xsd, i am getting cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'p:Reviewer'. error. Does both xsd file should not be in same namespace?
<?xml version="1.0" encoding="UTF-8"?>
<pr:BookCatalogue xmlns:pr="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com AddRequest.xsd ">
<pr:Book>
<pr:Title>pr:Title</pr:Title>
<pr:Author>pr:Author</pr:Author>
<pr:Date>pr:Date</pr:Date>
<pr:ISBN>pr:ISBN</pr:ISBN>
<pr:Publisher>pr:Publisher</pr:Publisher>
<p:Reviewer xmlns:p="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com Children.xsd ">
<p:Name>
<p:First>p:First</p:First>
<p:Last>p:Last</p:Last>
</p:Name>
</p:Reviewer>
</pr:Book>
</pr:BookCatalogue>
Two options...
Option One: If you do not want to have to have the definition of p:Reviewer present, add processContents="lax" to your xs:any element:
<xs:any namespace="##any" minOccurs="0" processContents="lax"/>
Per XML Schema Part 0: Primer Second Edition:
The lax value of the processContents attribute instructs an XML
processor to validate the element content on a can-do basis: It will
validate elements and attributes for which it can obtain schema
information, but it will not signal errors for those it cannot obtain
any schema information.
See also XML Validation in Java: processContents=“lax” seems not to work correctly.
You should also carefully adjust your xsi:schemaLocation values to point to the actual filename of each XSD for each namespace in play. Here is your XML instance with the changes that I made:
<?xml version="1.0" encoding="UTF-8"?>
<pr:BookCatalogue
xmlns:pr="http://www.w3schools.com"
xmlns:p="http://www.w3schools.com/1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com BookCatalogue.xsd http://www.w3schools.com/1 Reviewer.xsd">
<pr:Book>
<pr:Title>pr:Title</pr:Title>
<pr:Author>pr:Author</pr:Author>
<pr:Date>pr:Date</pr:Date>
<pr:ISBN>pr:ISBN</pr:ISBN>
<pr:Publisher>pr:Publisher</pr:Publisher>
<p:Reviewer>
<p:Name>
<p:First>p:First</p:First>
<p:Last>p:Last</p:Last>
</p:Name>
</p:Reviewer>
</pr:Book>
</pr:BookCatalogue>
Note: Make sure that the targetNamespace in Review.xsd matches what's declared for it in BookCatalogue.xml's xsi:schemaLocation attribute.
Option Two: If you do want to insist that the definition of p:Reviewer be present, just make the above changes to be sure that Review.xsd can be found per the xsi:schemaLocation mechanism. No processContents setting is required; it defaults to strict.

Resources