import in XSD and its implementation - xsd

I have 2 separate XSDs with some common attributes. I want to create another XSD and put all my common attributes in the separate XSD and import them in the 2 XSDs i have already rather than repeating them or duplicating them in both the XSDs.
Is there any reference for such implementation?

We do it like this:
Shared "library" xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.common.namespace/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.common.namespace/">
<xs:attribute name="ACommonAttribute" type="xs:float" default="1.7"/>
</xs:schema>
Left xsd with same target namespace:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://www.common.namespace/" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.common.namespace/">
<xs:include schemaLocation="Common.xsd"/>
<xs:element name="MyLeftElement">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute ref="ACommonAttribute"/>
</xs:complexType>
</xs:element>
</xs:schema>
Right xsd with different target namespace (needs an Import instead if an Include statement)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://another.namespace/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:libs="http://www.common.namespace/" targetNamespace="http://another.namespace/">
<xs:import namespace="http://www.common.namespace/" schemaLocation="Common.xsd"/>
<xs:complexType name="RightComplexType">
<xs:sequence>
<xs:element name="Bit">
<xs:complexType>
<xs:attribute ref="libs:ACommonAttribute"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>

Related

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.

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.

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>

Including / importing local schemas that have namespaces

Here is a schema file, midi.xsd that defines a type, note, used to store MIDI note values:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="note">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="127"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Here is another schema file, octaves.xsd which uses midi.xsd to help define the layout to be enforced on an XML file containing data about octaves:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="midi.xsd"/>
<xs:element name="octaves">
<xs:complexType>
<xs:sequence>
<xs:element name="octave">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="midi">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="xs:integer" />
<xs:element name="from" type="note" />
<xs:element name="to" type="note" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="index" type="xs:integer" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This is fine, and works exactly as you would expect it to, except that I have another requirement: I want note to be in its own namespace, midi, so that
<xs:element name="from" type="note" />
becomes
<xs:element name="from" type="midi:note" />
Try as I might, I cannot get this to work. My attempts have included use of the targetNamespace attribute in various places, the import element, and liberal use of xmlns:midi="...", but to no avail. I'd post one of these attempts here, were it not so cringe-worthy.
Could some kind soul point me in the right direction? I'm pretty sure the problem is to do with the fact that midi.xsd is a local file; it has never been, and never will be, hosted on a web server.
Change midi.xsd to be:
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetnamespace="/my/midi/namespace">
And then change octaves.xsd to say:
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:midi="/my/midi/namespace">
<xs:import namespace="/my/midi/namespace" schemaLocation="midi.xsd"/>
...
<xs:element name="from" type="midi:note" />
Note the use of xs:import rather than <xs:include> The two are very different - you use import for bringing in other namespaces, and include for inline inclusion of other schema files into the current namespace.
Note also that /my/midi/namespace can be anything you want, it's an arbitrary identifier.
I'm pretty sure the problem is to do with the fact that midi.xsd is a local file
Nope, not relevant.

Resources