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

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.

Related

Is there a way to handle/ignore inline xsd in received xml file in biztalk?

Client sends a file containing customer data in xml. The problem is that it also contains the xsd they use. Is there a way when setting up the source schema in biztalk to make it validate while ignoring the xsd?
I've tried including nodes for it, but it does not validate due to some namespace error, and it keeps looking for the first element node containing customer data
<Root>
<xsd:schema id="Root" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="Root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="Data" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="number" type="xsd:int"/>
<xsd:element name="name">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="60"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="address">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="30"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Zip">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="5"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="telephone">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="60"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="custNumber">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="11"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="branchnumber" type="xsd:int"/>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:choice>
<xsd:anyAttribute namespace="http://www.w3.org/XML/1998/namespace" processContents="lax"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<Data>
<number>12345</number>
<name>Scrooge McDuck</name>
<address>Address 1</adresse>
<Zip>0000</Zip>
<telephone>1234563290</telephone>
<custNumber>123324</custNumber>
<branchnumber>0</branchnumber>
</Data>
<Data>
<number>23456</number>
<name>Donald Duck</name>
<address>Address 4</adresse>
<Zip>4625</Zip>
<telephone>1234567890</telephone>
<custNumber>123321</custNumber>
<branchnumber>0</branchnumber>
</Data>
</Root>
I don't think this would actually cause a problem.
First, don't bother validating at runtime, no one ever does this and it's never mattered.
The schema should only contain the content nodes, this will work just fine in the Mapper, Properties etc.
Since you're not validating, provided the Schema is valid xml, it'll just go along for the ride and will be ignored.
To Validate in VS, just take the Schema nodes out.

FpML 5.3 JAXB Bindings

How do I use jaxb bindings to fix multiple group references in a schema?
Part of fpml 5.3 has the following in the schema
<xsd:complexType name="Price">
<xsd:annotation>
<xsd:documentation xml:lang="en">A type describing the strike price.</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="commission" type="Commission" minOccurs="0">
<xsd:annotation>
<xsd:documentation xml:lang="en">This optional component specifies the commission to be charged for executing the hedge transactions.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:choice minOccurs="0">
<xsd:sequence>
<xsd:element name="determinationMethod" type="DeterminationMethod">
<xsd:annotation>
<xsd:documentation xml:lang="en">Specifies the method according to which an amount or a date is determined.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:group ref="EquityPrice.model" minOccurs="0"></xsd:group>
</xsd:sequence>
<xsd:element name="amountRelativeTo" type="AmountReference">
<xsd:annotation>
<xsd:documentation xml:lang="en">The href attribute value will be a pointer style reference to the element or component elsewhere in the document where the anchor amount is defined.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:group ref="EquityPrice.model"></xsd:group>
</xsd:choice>
<xsd:element name="cleanNetPrice" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation xml:lang="en">The net price excluding accrued interest. The "Dirty Price" for bonds is put in the "netPrice" element, which includes accrued interest. Thus netPrice - cleanNetPrice = accruedInterest. The currency and price expression for this field are the same as those for the (dirty) netPrice.</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="quotationCharacteristics" type="QuotationCharacteristics" minOccurs="0">
<xsd:annotation>
<xsd:documentation>Allows information about how the price was quoted to be provided.</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
This results in
* <p>
* You are getting this "catch-all" property because of the following reason:
* The field name "GrossPrice" is used by two different parts of a schema. See:
* line 1361 of file:/C:/fpml-jaxb/src/main/xsd/fpml-asset-5-3.xsd
* line 1361 of file:/C:/fpml-jaxb/src/main/xsd/fpml-asset-5-3.xsd
* <p>
The error is misleading. EquityPrice.model starts with this and line 1361 is the grossPrice. The issue stems from above where there are 2 group references to EquityPrice.model in the Price complex type.
<xsd:group name="EquityPrice.model">
<xsd:sequence>
<xsd:element name="grossPrice" type="ActualPrice" minOccurs="0">
<xsd:annotation>
<xsd:documentation xml:lang="en">Specifies the price of the underlyer, before commissions.</xsd:documentation>
</xsd:annotation>
</xsd:element>
The same thing applies for TradeNovationContent where it has multiple references to NewTrade.model.
I also had this same problem, the solution was to enable simple binding mode.
Add the following into your external bindings file.
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
...
<jxb:globalBindings>
<xjc:simple />
</jxb:globalBindings>
Error is caused by the way JAXB is handling groups, as you've seen yourself.

Limit value of element to value in other element XSD

Given the following type:
<xsd:complexType name="Options">
<xsd:sequence>
<xsd:element name="option" maxOccurs="unbounded">
<xsd:complexType>
<xsd:all>
<xsd:element name="id" type="xsd:integer" />
<xsd:element name="label" type="xsd:string" />
<xsd:element name="value" type="xsd:string" minOccurs="0" />
<xsd:element name="dependency" type="xsd:integer" minOccurs="0" />
</xsd:all>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
How can I express that a dependency of one option must exist as an id of a different option?
I think I'm supposed to be using xsd:key and xsd:keyref, but having trouble understanding how to use them.
XSD 1.0 doesn't support this. XSD 1.1 supports Conditional Types and assertions which might allow you to do this. For xsd 1.0 you can use Schematron to achieve this.
Constraints can only be expressed in the context of an element.
<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="Options">
<xsd:sequence>
<xsd:element name="option" maxOccurs="unbounded">
<xsd:complexType>
<xsd:all>
<xsd:element name="id" type="xsd:integer"/>
<xsd:element name="label" type="xsd:string"/>
<xsd:element name="value" type="xsd:string" minOccurs="0"/>
<xsd:element name="dependency" type="xsd:integer" minOccurs="0"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Options" type="Options">
<xsd:key name="PK">
<xsd:selector xpath="option"/>
<xsd:field xpath="id"/>
</xsd:key>
<xsd:keyref name="FK" refer="PK">
<xsd:selector xpath="option"/>
<xsd:field xpath="dependency"/>
</xsd:keyref>
</xsd:element>
</xsd:schema>
If you wish to have this type referenced in multiple instances, along with the constraints you've indicated, then you will have to always define a global element, and then ref that element in your specific contexts.
I would mention that it is very important to understand of your use of the "different" word in your question. If you really mean it, then the above doesn't give you that.

XML Schema Choice - choose at lease one, but no repeat

How can I enforce to chose at lease one, but no repeat?
The following syntax allows any c element to repeat up to 3 times.
<choice minOccurs="1" maxOccurs="3">
<element name="c1" type="string" />
<element name="c2" type="string" />
<element name="c3" type="string" />
</choice>
thnx
steve
Loose the maxOccurs="3" and what you got is "choose at least one", no repeat.
For particles, the default is minOccurs="1"; a mandatory choice, where each option particle is itself mandatory, is your answer.
UPDATE: Based on your comment, if what you're looking is for any ordered combination of the particles you've described, this is the best you can get with XSD spec.
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:choice>
<xsd:sequence>
<xsd:element ref="c1"/>
<xsd:element ref="c2" minOccurs="0"/>
<xsd:element ref="c3" minOccurs="0"/>
</xsd:sequence>
<xsd:sequence>
<xsd:element ref="c2"/>
<xsd:element ref="c3" minOccurs="0"/>
</xsd:sequence>
<xsd:element ref="c3"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<xsd:element name="c1" type="xsd:string"/>
<xsd:element name="c2" type="xsd:string"/>
<xsd:element name="c3" type="xsd:string"/>
</xsd:schema>
This is messy already; if you're looking for a greater number of particles or any unordered combination, then I would change the model to something like this (these are XSD 1.0 limitations in action - it all has to do with limitations in the XPath syntax you can use for selectors/fields).
<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="c" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="pk">
<xsd:selector xpath="*"/>
<xsd:field xpath="#code"/>
</xsd:key>
</xsd:element>
<xsd:element name="c" type="TC" abstract="true"/>
<xsd:element name="c1" type="TC1" substitutionGroup="c"/>
<xsd:element name="c2" type="TC2" substitutionGroup="c"/>
<xsd:element name="c3" type="TC3" substitutionGroup="c"/>
<xsd:complexType name="TC">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="code" type="xsd:string"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="TC1">
<xsd:simpleContent>
<xsd:restriction base="TC">
<xsd:attribute name="code" type="xsd:string" fixed="c1"/>
</xsd:restriction>
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="TC2">
<xsd:simpleContent>
<xsd:restriction base="TC">
<xsd:attribute name="code" type="xsd:string" fixed="c2"/>
</xsd:restriction>
</xsd:simpleContent>
</xsd:complexType>
<xsd:complexType name="TC3">
<xsd:simpleContent>
<xsd:restriction base="TC">
<xsd:attribute name="code" type="xsd:string" fixed="c3"/>
</xsd:restriction>
</xsd:simpleContent>
</xsd:complexType>
</xsd:schema>
A sample XML would like this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<c1 code="c1">c11</c1>
<c2 code="c2">c21</c2>
<c3 code="c3">c21</c3>
</root>
or this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<c2 code="c2">c21</c2>
<c1 code="c1">c11</c1>
</root>
Basically you're keying in on some component that makes your element unique, that is part of the data as opposed to a tag name. Again, messy, but as an exercise, it may give you an idea.

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