XSD - XNOR gate of 2 nodes - xsd

I trying to create a XSD which validates XML only if both nodes A and B exist or both does not exist (XNOR gate).
I have checked the internet, however, without any luck.
<root>
<A>a</A>
<B>b</B>
<C>c</C>
</root>

I have found a solution:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="root"/>
<xs:complexType name="root">
<xs:sequence>
<xs:choice>
<xs:sequence>
<xs:element name="a" type="xs:string" />
<xs:element name="b" type="xs:string" />
</xs:sequence>
<xs:sequence></xs:sequence>
</xs:choice>
<xs:element name="c" type="xs:string" />
</xs:sequence>
</xs:complexType>

Related

How to get specific elements to be repeated in XSD

I am trying to pass few elements and 1 element(product) which contains sku and quantity should repeat but when i am trying this whole all 6 are repeating in the payload.I tried in many ways, it doesn't work and I can't find the problem.
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="customReference"/>
<xs:element name="shippedFromCustomId"/>
<xs:element name="shippedToCustomId"/>
<xs:element name="orderNumber"/>
<xs:element name="referenceNumber"/>
<xs:element name="products">
<xs:complexType>
<xs:sequence>
<xs:element name="product" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="sku"/>
<xs:element name="quantity"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
Hope this clarifies where i stuck at, Please share if you have any related information.
Thanks in advance!!``
Using your xsd, the following XML will be valid:
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="issue.xsd">
<customReference></customReference>
<shippedFromCustomId></shippedFromCustomId>
<shippedToCustomId></shippedToCustomId>
<orderNumber></orderNumber>
<referenceNumber></referenceNumber>
<products>
<product>
<sku></sku>
<quantity></quantity>
</product>
<product>
<sku></sku>
<quantity></quantity>
</product>
<product>
<sku></sku>
<quantity></quantity>
</product>
</products>
</root>

"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.

Unique element in XSD sequence

I'd like to have an XSD to valide an XML containing file elements with many aliases but each alias with a different value.
This is my XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<folder>
<files>
<fileList>
<file>
<title>Document1</title>
<fileAlias>
<alias>Alias1</alias>
<alias>Alias2</alias>
</fileAlias>
</file>
</fileList>
</files>
</folder>
The former XML is good because Alias1 != Alias2
How could I specify it in my XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="folder" type="folderType" />
<xs:complexType name="folderType">
<xs:sequence>
<xs:element name="files" type="filesType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="filesType">
<xs:sequence>
<xs:element name="fileList" type="fileListType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="fileListType">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="file" type="fileType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="fileType">
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="fileAlias" type="fileAliasType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="id" type="xs:string" />
<xs:attribute name="type" type="xs:int" />
</xs:complexType>
<xs:complexType name="fileAliasType">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="alias" type="xs:string" minOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:schema>
I've tried with xsd:unique but I think is for attributes not for XML element text values.
If I understand well your need then xs:unique should be the right way. Following modification of fileType complex type should be enough.
<xs:complexType name="fileType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="fileAlias" type="fileAliasType" minOccurs="0" maxOccurs="unbounded">
<xs:unique name="alias_unique">
<xs:selector xpath="alias"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:string"/>
<xs:attribute name="type" type="xs:int"/>
</xs:complexType>
This will validate
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2013 sp1 (http://www.altova.com)-->
<folder xsi:noNamespaceSchemaLocation="unique.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<files>
<fileList>
<file id="String" type="0">
<title>String</title>
<fileAlias>
<alias>String1</alias>
<alias>String2</alias>
<alias>String3</alias>
</fileAlias>
</file>
</fileList>
</files>
</folder>
This won't
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2013 sp1 (http://www.altova.com)-->
<folder xsi:noNamespaceSchemaLocation="unique.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<files>
<fileList>
<file id="String" type="0">
<title>String</title>
<fileAlias>
<alias>String1</alias>
<alias>String1</alias>
<alias>String3</alias>
</fileAlias>
</file>
</fileList>
</files>
</folder>

Override minoccurs of an element from parent xsd

is it possible to override the minOccurs/maxOccurs attributes of an element in the parent xsd? we can easily extend the parent xsd and create a new xsd with different namespace, but am trying to override with same parent namespace.
Lets say we have an xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:cust="http://test.com/schema/cust" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.com/schema/cust" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="customer-type">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="hobby" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:element name="customer" type="cust:customer-type"/>
</xs:schema>
can i create extension/restriction of above xsd with same namespace which will restrict/change the <cust:hobby> minOccurs to be 1?
Okay, i was complicating the solution. Before this i was trying to use override function in xml 1.0 which is available only from xml 1.1.
A simple redefine would do the trick:
If the name of your base xsd is cust.xsd then the below redefinition overrides the minOccurs of "hobby" element to 1.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:cust="http://test.com/schema/cust" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.com/schema/cust" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:redefine schemaLocation="cust.xsd">
<xs:complexType name="customer-type">
<xs:complexContent>
<xs:restriction base="cust:customer-type">
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="hobby" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
</xs:schema>

xsd choice for multiple elements

I would like my Xml file to look like the following
<root>
<name>a</name>
<age>23</age>
</root>
or
<root>
<empno>b<empno>
<designation>ase</designation>
</root>
is it possible to create a XML schema for the above using a "choice" indicator?something like below.
<xsd:element name="root">
<xsd:complexType>
<xsd:choice>
<xsd:element name="name"/>
<xsd:element name="age" />
or
<xsd:element name="empno"/>
<xsd:element name="designation" />
<xsd:choice>
</xsd:complexType>
<xsd:element>
Is it possible to do like this?
Yes, You are almost there.. just missing a sequence .. Since it's the set of fields you have to wrap them under sequence..
These sequence tags will be under <Choice> tag. Now either of these set of tags (Sequence) will be validated.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="root"/>
<xs:complexType name="root">
<xs:choice>
<xs:sequence>
<xs:element name="empno" type="xs:string" />
<xs:element name="designation" type="xs:string" />
</xs:sequence>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="age" type="xs:unsignedByte" />
</xs:sequence>
</xs:choice>
</xs:complexType>
</xs:schema>
-----------------------------------------------------------------------------------------
I would like to add one more suggestion here:
I observe that you use nested declarations .. like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="trunk">
<xs:complexType>
<xs:sequence>
<xs:element name="branch1" type="xs:string"/>
<xs:element name="branch2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="other" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Always prefer usage of Custom types! Like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="root"/>
<xs:complexType name="root">
<xs:sequence>
<xs:element name="trunk" type="trunk"/>
<xs:element name="other" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="trunk">
<xs:sequence>
<xs:element name="branch1" type="xs:string"/>
<xs:element name="branch2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
This improves readability and you can reuse the complexType/simpleType ..
hope it helps..

Resources