XSD: Restrict base type in derived type - xsd

Following xsd:
AppleType is derived from FruitType
PairType is derived from FruitType
MyBaseType has an element "Fruit" of type FruiteType
I want to define a new type MyDerivedType, derived from MyBaseType with the restriction that
bases element "Fruit" is of type AppleType.
<xsd:schema targetNamespace="http://www.example.org/Test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org/Test/">
<xsd:complexType name="FruitType"/>
<xsd:complexType name="AppleType">
<xsd:complexContent>
<xsd:extension base="FruitType"/>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="PairType">
<xsd:complexContent>
<xsd:extension base="FruitType"/>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="MyBaseType">
<xsd:sequence>
<xsd:element name="Fruit" type="FruitType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="MyDerivedType">
<xsd:complexContent>
<xsd:restriction base="MyBaseType">
<xsd:sequence>
<xsd:element name="Fruit" type="AppleType"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
Using "xsd:extension" (adding sth) fails as this would end with two elements of different type.
Using "xsd:restriction" (removing sth) fails as the element is defined as a different type.
How to restrict element Fruit in derived type to an ApplyType instead of generic FruitType

Related

<choice> not support when writing xsd schema for EMF

I am writing a schema for EMF to describe general graphs. The problem is that if this is directed graph, then the edge should have properties of src_node and dst_node, otherwise it has node_one and node_two. I tried to use "choice" to have mutally exclusive elements but in EMF it functions the same as "sequence". e.g a similar problem for NodeProperty
<xsd:complexType name="NodeProperty">
<xsd:choice>
<xsd:element name="StringValue" type="xsd:string"/>
<xsd:element name="IntValue" type="xsd:int"/>
<xsd:element name="DoubleValue" type="xsd:double"/>
</xsd:choice>
<xsd:attribute name="Property" type="xsd:string"/>
</xsd:complexType>
Another problem in substitutionGroup is that src_node and dst_node are claimed be to unresolved by EMF. The complete code:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:simpleType name="GraphType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Directed"/>
<xsd:enumeration value="Undirected"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="NodeProperty">
<xsd:choice>
<xsd:element name="StringValue" type="xsd:string"/>
<xsd:element name="IntValue" type="xsd:int"/>
<xsd:element name="DoubleValue" type="xsd:double"/>
</xsd:choice>
<xsd:attribute name="Property" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="Node">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0"
name="Node" type="lib:NodeProperty"/>
</xsd:sequence>
<xsd:attribute name="NodeName" type="xsd:string" />
</xsd:complexType>
<xsd:element name="src_node" type="xsd:anyURI" ecore:reference="lib:Node"/>
<xsd:element name="node_one" type="xsd:anyURI" substitutionGroup="src_node"/>
<xsd:element name="dst_node" type="xsd:anyURI" ecore:reference="lib:Node"/>
<xsd:element name="node_two" type="xsd:anyURI" substitutionGroup="dst_node"/>
<xsd:complexType name="Edge">
<xsd:sequence>
<xsd:element ref="src_node"/>
<xsd:element ref="dst_node"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="GeneralGraph">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="type" type="lib:GraphType"/>
<xsd:element name="NodeList">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0"
name="Node" type="lib:Node"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="EdgeList">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="0"
name="Edge" type="lib:Edge"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
Yes. Substitution groups apply to global elements. So move your elements out of your type and reference to them in the type.
<xsd:element name="src_node" type="xsd:anyURI" ecore:reference="lib:Node"/>
<xsd:element name="node_one" type="xsd:anyURI" substitutionGroup="src_node"/>
<xsd:element name="dst_node" type="xsd:anyURI" ecore:reference="lib:Node"/>
<xsd:element name="node_two" type="xsd:anyURI" substitutionGroup="dst_node"/>
<xsd:complexType name="Edge">
<xsd:sequence>
<xsd:element ref="src_node"/>
<xsd:element ref="dst_node"/>
</xsd:sequence>
</xsd:complexType>
Untested.
The idea is that now node_one could be used instead src_node in Edge and node_two instead of node_two.
Note that type of the substitutable must be derived from the type of the substitution group element. So I used anyURI just for example. You'll probably want to rework it.
By the way I generally discourage usage of subsitition groups. Makes your XML unpredictable.

Create excel file from complex xml-schema

I'm looking at the feasibility of using an Excel file for loading data into the EPA GHG schema (ghg_subpartp_inputs_v1.0.xsd) for defining the XML Subpartp Inputs data.
I'm not well versed or practiced in using Excel and xml-schemas.
The SubpartP schema has nested complex elements, and it won't be accepted into Excel via the XML Maps facility.
Error message: "Cannot Load the specified XML or schema source."
I'm thinking it is because it is a partial schema that is meant to be included in others?
Here is the schema itself:
Schema Name : ghg_subpartp_inputs_v1.0.xsd
Description : Inputs Verifier Tool - XML schema for Subpart P inputs
<xsd:complexType name="SubpartPInputsType">
<xsd:sequence>
<xsd:element name="SubpartPUnitInputs" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="UnitName" type="xsd:string"/>
<xsd:element name="Values">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EquationP1FeedstockInputs" type="EquationP1FeedstockInputsType" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="EquationP2FeedstockInputs" type="EquationP2FeedstockInputsType" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="EquationP3FeedstockInputs" type="EquationP3FeedstockInputsType" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="UniqueSubpartPFeedstockName">
<xsd:selector xpath="Values/*"/>
<xsd:field xpath="FeedstockName"/>
</xsd:unique>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="EquationP1FeedstockInputsType">
<xsd:sequence>
<xsd:element name="FeedstockName" type="xsd:string"/>
<xsd:element name="Values">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MassOrVolume" type="MassOrVolumeIndicatorType"/>
<xsd:element name="EquationP1MonthlyInputs" minOccurs="0" maxOccurs="12">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MonthName" type="MonthNameList"/>
<xsd:element name="Values">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Mass" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: kg of fuel or feedstock</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="Volume" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: scf (at standard conditions of 68 degrees F and atmospheric pressure) of fuel or feedstock</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="CarbonContent" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: kg C per kg of fuel or feedstock</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="MolecularWeight" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: kg/kg-mole</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="UniqueEquationP1MonthName">
<xsd:selector xpath="*"/>
<xsd:field xpath="MonthName"/>
</xsd:unique>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="EquationP2FeedstockInputsType">
<xsd:sequence>
<xsd:element name="FeedstockName" type="xsd:string"/>
<xsd:element name="Values">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MassOrVolume" type="MassOrVolumeIndicatorType"/>
<xsd:element name="EquationP2MonthlyInputs" minOccurs="0" maxOccurs="12">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MonthName" type="MonthNameList"/>
<xsd:element name="Values">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Mass" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: kg of fuel or feedstock</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="Volume" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: gallons of fuel or feedstock</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="CarbonContent" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: kg C per kg of fuel or feedstock if measured as mass, or kg C per gallon of fuel or feedstock if measured as volume</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="UniqueEquationP2MonthName">
<xsd:selector xpath="*"/>
<xsd:field xpath="MonthName"/>
</xsd:unique>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="EquationP3FeedstockInputsType">
<xsd:sequence>
<xsd:element name="FeedstockName" type="xsd:string"/>
<xsd:element name="Values">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="EquationP3MonthlyInputs" minOccurs="0" maxOccurs="12">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="MonthName" type="MonthNameList"/>
<xsd:element name="Values">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Mass" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: kg of fuel and feedstock</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="CarbonContent" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Required units of measure for this equation input: kg C per kg of fuel and feedstock</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:unique name="UniqueEquationP3MonthName">
<xsd:selector xpath="*"/>
<xsd:field xpath="MonthName"/>
</xsd:unique>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
What should I be doing to create the Excel file that will create an acceptible XML for this schema?

implementing condition on Maximum occurance of an element in XSD depending on its elements

I am working some schema creation.
My schema structure looks like this.
Schema 1(Embedded schema):
It has two fields:
1.select:Drop Down to allow two values "Heading" and "Title"
2.text
Schema 2:
It has following fields;
1.First--of Type Schema 1: As a field. Max occurrence is 4
The source of main schema looks like this:
<xsd:schema elementFormDefault="qualified" xmlns:tcmi="http://www.tridion.com/ContentManager/5.0/Instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:import namespace="http://www.tridion.com/ContentManager/5.0/Instance"></xsd:import>
<xsd:annotation>
<xsd:appinfo>
<tcm:Labels xmlns:tcm="http://www.tridion.com/ContentManager/5.0">
<tcm:Label ElementName="select" Metadata="false">select</tcm:Label>
<tcm:Label ElementName="text" Metadata="false">text</tcm:Label>
</tcm:Labels>
</xsd:appinfo>
</xsd:annotation>
<xsd:complexType name="Content">
<xsd:sequence>
<xsd:element name="select" minOccurs="0" maxOccurs="1">
<xsd:annotation>
<xsd:appinfo>
<tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml>
<tcm:Size xmlns:tcm="http://www.tridion.com/ContentManager/5.0">1</tcm:Size>
<tcm:listtype xmlns:tcm="http://www.tridion.com/ContentManager/5.0">select</tcm:listtype>
</xsd:appinfo>
</xsd:annotation>
<xsd:simpleType>
<xsd:restriction base="xsd:normalizedString">
<xsd:enumeration value="Heading"></xsd:enumeration>
<xsd:enumeration value="Title"></xsd:enumeration>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="text" minOccurs="1" maxOccurs="1" type="tcmi:MultiLineText">
<xsd:annotation>
<xsd:appinfo>
<tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml>
<tcm:Size xmlns:tcm="http://www.tridion.com/ContentManager/5.0">2</tcm:Size>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
This schema is referred in the main schema as "Field"--First
<xsd:schema elementFormDefault="qualified" targetNamespace="some name space" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="some name space" xmlns:tcmi="http://www.tridion.com/ContentManager/5.0/Instance">
<xsd:import namespace="http://www.tridion.com/ContentManager/5.0/Instance" schemaLocation="location.xsd"></xsd:import>
<xsd:include schemaLocation="Schema 1 location"></xsd:include>
<xsd:annotation>
<xsd:appinfo>
<tcm:Labels xmlns:tcm="http://www.tridion.com/ContentManager/5.0">
<tcm:Label ElementName="First" Metadata="false">First</tcm:Label>
</tcm:Labels>
</xsd:appinfo>
</xsd:annotation>
<xsd:element name="Content">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="First" minOccurs="1" maxOccurs="4" type="Content">
<xsd:annotation>
<xsd:appinfo>
<tcm:ExtensionXml xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:ExtensionXml>
<tcm:EmbeddedSchema xlink:href="Schema 1 location" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:tcm="http://www.tridion.com/ContentManager/5.0" xlink:title="abcd"></tcm:EmbeddedSchema>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The field "First" is following Schema 1. So In turn It has 2 fields.
My need is as No Of occurrence of the Field "First" is 4. I also want to apply one more condition it.
The Conditions:
1.No Of occurrence of the "First" is 4.
2. As Schema 1 has drop down to select values "Heading" and "Title".
I want to restrict that out of 4 occurrence of "First",
Heading can occur Maximum 2 times and Title can occur maximum of 2 two times.
Can any one help how to achieve this.

XSD Make minOccurs depend on containing type

I have a complex type defined which doesn't currently contain any minOccurs restrictions. When I use this comlpex type as an element type I sometimes want the elements to have minOccurs 0, other times 1. E.g.
<xsd:complexType name="Identifier">
<xsd:sequence>
<xsd:element name="Id" type="xsd:string"/>
<xsd:element name="Version" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Wibble">
<xsd:sequence>
<xsd:element name="Id" type="Identifier"/> <!-- I want all elements of Identifier to be mandatory when used as part of a 'Wibble' -->
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Wobble">
<xsd:sequence>
<xsd:element name="Id" type="Identifier"/> <!-- I want all elements of Identifier to be optional when used as part of a 'Wobble' -->
</xsd:sequence>
</xsd:complexType>
Is this possible?
Thanks in advance.
Groups are your friend, e.g.
<xsd:group name="IdentifierGroup">
<xsd:sequence>
<xsd:element name="Id" type="Identifier"/>
</xsd:sequence>
</xsd:group>
<xsd:complexType name="Wibble">
<xsd:sequence>
<xsd:group ref="IdentifierGroup" minOccurs="1"/>
<!-- more elements for Wibble here -->
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="Wobble">
<xsd:sequence>
<xsd:group ref="IdentifierGroup" minOccurs="0"/>
<!-- more elements for Wobble here -->
</xsd:sequence>
</xsd:complexType>

In XSD, how can I define two different elements with the same name?

I have a problem making an xsd file. I need to have an xsd file for xml documents which will look like this:
<message type="login">
<login nick="Ben" gameId="chess" desiredRole="simple "/>
</message>
or like this
<message type="error">
Error message
</message>
Meaning there is always a message tag but it has different attribute type values and depending on type value there are different things inside the message tag. I tried something like this:
<xsd:element name ='message' type='messageType'>
</xsd:element>
<xsd:complexType name='messageType'/>
<xsd:complexType name='error'>
<xsd:complexContent>
<xsd:extension base='messageType'>
<xsd:attribute name ='type' use='required'>
<xsd:simpleType>
<xsd:restriction base='xsd:string'>
<xsd:enumeration value='error'/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name='login'>
<xsd:complexContent>
<xsd:extension base='messageType'>
<xsd:sequence>
<xsd:element name='login'>
<xsd:complexType>
<xsd:attribute name='nick' type='xsd:string' use='required'>
</xsd:attribute>
<xsd:attribute name='gameId' type='xsd:string' use='required'>
</xsd:attribute>
<xsd:attribute name='desiredRole' type='xsd:string' use='required'>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name ='type' use='required'>
<xsd:simpleType>
<xsd:restriction base='xsd:string'>
<xsd:enumeration value='login'/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
but it doesn’t work (I get an error that the attribute type was not defined). Can some one please help me with this?
How about
<message>
<login />
</message>
and
<message>
<error />
</message>
That is, leave off the type attribute, and always have an element inside of message.
XSD doesn't allow you to do precisely what you want to do, which is create an element that has different structures depending on attribute values. However, if you truly need this element to have different structures like this, you can create an element definition that allows all possible structures. You just cannot have it automatically validated by the parser to prove that it conforms to either one definition or the other, exclusively. Not within XSD at any rate.
Try something like this:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="message" type="messageType"/>
<xsd:simpleType name="typeType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="login"/>
<xsd:enumeration value="error"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="login">
<xsd:complexType>
<xsd:attribute name='nick' type='xsd:string' use='required'/>
<xsd:attribute name='gameId' type='xsd:string' use='required'/>
<xsd:attribute name='desiredRole' type='xsd:string' use='required'/>
</xsd:complexType>
</xsd:element>
<xsd:complexType name='messageType'/>
<xsd:sequence>
<xsd:element ref="login" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
<xsd:attribute name="type" type="typeType" use="required"/>
</xsd:complexType>
</xsd:schema>

Resources