Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

please check if my xsd is correct 1

Status
Not open for further replies.

bluesky999

Programmer
Aug 3, 2010
4
CA
This is my first time writing xsd file. Could someone please tell me if everything is correct? Thanks.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="
<xs:simpleType name="booltype">
<xs:restriction base="xs:string">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
<xs:enumeration value="yes"/>
<xs:enumeration value="no"/>
<xs:enumeration value="1"/>
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="marketdatatype">
<xs:all>
<xs:element name="instument" type="xs:string"/>
<xs:element name="currency" type="xs:string" minOccurs="0"/>
<xs:element name="quoteType" type="xs:string" minOccurs="0"/>
<xs:element name="fact" type="xs:string" minOccurs="0"/>
<xs:choice minOccurs="0">
<xs:element name="symbol" type="xs:string"/>
<xs:element name="term" type="xs:string"/>
<xs:element name="deliveryDate" type="xs:string"/>
</xs:choice>
<xs:element name="underlyingTerm" type="xs:string" minOccurs="0"/>
<xs:choice minOccurs="0">
<xs:element name="rating" type="xs:string"/>
<xs:element name="crossCurrency" type="xs:string"/>
</xs:choice>
</xs:all>
</xs:complexType>

<xs:complexType name="datetype">
<xs:choice use="required">
<xs:attribute name="start" type="xs:date"/>
<xs:attribute name="obs" type="xs:positiveInteger"/>
</xs:choice>
<xs:attribute name="end" type="xs:date" use="required"/>
</xs:complexType>

<xs:complexType name="requesttype">
<xs:attribute name="header" type="booltype"/>
<xs:attribute name="showProxy" type="booltype"/>
<xs:all>
<xs:element name="date" type="datetype" use="required"/>
<xs:element name="marketdata" type="marketdatatype" minOccurs="1" maxOccurs="unbounded"/>
</xs:all>
</xs:complexType>

<xs:element name="request" type="requesttype"/>

</xs:schema>
 
[1] >
<xs:all>
<xs:element name="date" type="datetype" [red]use="required"[/red]/>
<xs:element name="marketdata" type="marketdatatype" [blue]minOccurs="1"[/blue] [red]maxOccurs="unbounded"[/red]/>
</xs:all>

[1.1] use attribute is not applicable to xs:element element. If you mean it is necessarily appeared, it is minOccurs="1" (by default). That means you have to take out that use attribute.
[1.2] In xs:all element, its child xs:element is not allowed to have maxOccurs other than 0 or 1 (default). Hence, in order it be a valid schema, that is not allowed.

[2] >
<xs:complexType name="requesttype">
[red]<xs:attribute name="header" type="booltype"/>
<xs:attribute name="showProxy" type="booltype"/>[/red]
<xs:all>
<!-- etc etc... ->
</xs:all>
</xs:complexType>


[2.1] xs:attribute cannot appear before xs:element etc... you should put it after.
[tt]
<xs:complexType name=&quot;requesttype&quot;>
<xs:all>
<!-- etc etc... ->
</xs:all>
<xs:attribute name="header" type="booltype"/>
<xs:attribute name="showProxy" type="booltype"/>
</xs:complexType>[/tt]

[3] >
<xs:choice [red]use="required"[/red]>
<xs:attribute name="start" type="xs:date"/>
<xs:attribute name="obs" type="xs:positiveInteger"/>
</xs:choice>


[3.1] use attribute is not allowed for xs:choice element.
[3.2] xs:choose cannot have xs:attribute as child element. Hence, content model like that is illegal. Effectively you're meant for a co-contraint fo the type, and it cannot be expressed in w3c schema 1.0
 
amendment
[3.2] xs:cho[red]ice[/red] cannot have xs:attribute as child element. Hence, content model like that is illegal. Effectively you're meant for a co-contraint fo the type, and it cannot be expressed in w3c schema 1.0
 
Thanks. If I change xs:all to xs:sequence as below, everything is correct?

<xs:complexType name="requesttype">
<xs:sequence>
<xs:element name="date" type="datetype"/>
<xs:element name="marketdata" type="marketdatatype" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="header" type="booltype"/>
<xs:attribute name="showProxy" type="booltype"/>
</xs:complexType>
 
[4] That is a good cleanup, but it is still not quite enough. But you still have not change the datetype which is completely off.

[4.1] Since datetype is in fact intended to describe an empty element with attributes start or obs attribute and end attribute, as I mentioned in [3.2], that kind of co-constraint is not expressible within w3c schema 1.0. (Thing has changed in v1.1, but not broadly supported yet.) I would suggest you let the first two "optional" and last "required", and then use application level validating routine to make sure they don't both appear - that is all you can do at the moment if you stick to w3c schema 1.0. Another open-lead will be to introduce kind of tandem validation with relex-ng or schematron. But that is leading quite far afield for the moment.

[4.2] I will make it an attributeGroup to group the attributes. (see below)

[5] The marketdata type is still problematic.

[5.1] The model group of all has a severe restriction in its content. Apart from annotation, it can hold only elements. Hence, xs:choice cannot appear as the child of xs:all. Hence, the marketdata type is still not quite correct.

[5.2] I would suggest you change xs:all to xs:sequence and respect the sequence order of the elements in it. That also makes much sense too.

[6] Here is a suggestion with full listing of the xsd.
[tt]
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="[ignore][/ignore]">

<xs:simpleType name="booltype">
<xs:restriction base="xs:string">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
<xs:enumeration value="yes"/>
<xs:enumeration value="no"/>
<xs:enumeration value="1"/>
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>

<xs:complexType name="marketdatatype">
<xs:sequence>
<xs:element name="inst[red]r[/red]ument" type="xs:string"/>
<xs:element name="currency" type="xs:string" minOccurs="0"/>
<xs:element name="quoteType" type="xs:string" minOccurs="0"/>
<xs:element name="fact" type="xs:string" minOccurs="0"/>
<xs:choice minOccurs="0">
<xs:element name="symbol" type="xs:string"/>
<xs:element name="term" type="xs:string"/>
<xs:element name="deliveryDate" type="xs:string"/>
</xs:choice>
<xs:element name="underlyingTerm" type="xs:string" minOccurs="0"/>
<xs:choice minOccurs="0">
<xs:element name="rating" type="xs:string"/>
<xs:element name="crossCurrency" type="xs:string"/>
</xs:choice>
</xs:sequence>
</xs:complexType>

<xs:attributeGroup name="attgrp">
<xs:attribute name="start" type="xs:date" use="optional" />
<xs:attribute name="obs" type="xs:positiveInteger" use="optional" />
<xs:attribute name="end" type="xs:date" use="required"/>
</xs:attributeGroup>

<xs:complexType name="datetype">
<xs:complexContent>
<xs:restriction base="xs:anyType">
<xs:attributeGroup ref="attgrp" />
</xs:restriction>
</xs:complexContent>
</xs:complexType>

<xs:complexType name="requesttype">
<xs:sequence>
<xs:element name="date" type="datetype"/>
<xs:element name="marketdata" type="marketdatatype" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="header" type="booltype"/>
<xs:attribute name="showProxy" type="booltype"/>
</xs:complexType>

<xs:element name="request" type="requesttype"/>

</xs:schema>
[/tt]
[6.1] Here is a sample of xml that would be validated.
[tt]
<request header="true" showProxy="false">
<date start="2010-08-01" end="2010-08-04" />
<marketdata>
<instrument>commercial paper</instrument>
<term>30-day</term>
<underlyingTerm>none</underlyingTerm>
<crossCurrency>eu-jpy</crossCurrency>
</marketdata>
</request>
[/tt]
[7] In any case, it is not as easy as it looks to build a good schema. I would say, build it up successively. All in one go? unless you become proficient with it.

ps: I've got to go.
 
I'm a little uncomfortable with attributeGroup and anyType with restriction. Is the following simple version correct?

<xs:complexType name="datetype">
<xs:attribute name="start" type="xs:date"/>
<xs:attribute name="obs" type="xs:positiveInteger"/>
<xs:attribute name="end" type="xs:date" use="required"/>
</xs:complexType>

 
You can do that. The version I presented is an "official" approach which can be modified more readily if one changes their mind at any later stage; and, as far as I can assure myself, should pass all validators which claimed to be w3c compliant. But, there are short-cuts. What you prefer can do. Whether it is an undertaking of being accepted by _all_ compliant validator, I cannot be sure, but it may well be.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top