Click to See Complete Forum and Search --> : XML Schema Rules / Conditional Attributes


paradoxperfect
10-23-2008, 05:58 PM
Hello all :),

I'm relatively new to creating XML Schemas. I'm trying to achieve the following:

<properties>
<property key="a" name="My String" type="sop:string" default="" />
<property key="b" name="My Integer" type="sop:integer" default="0" />
<property key="c" name="My Ranger" type="sop:range" default="0" min="0" max="1" />
<property key="d" name="My Select List" type="sop:select" default="0">
<option value="1">Hello World</option>
<option value="2">Hello World Second</option>
<option value="3">Hello World Third</option>
</property>
<property key="e" name="My Boolean" type="sop:boolean" default="true" />
<property key="f" name="My Double" type="sop:double" default="0.1" />
</properties>
Here are the conditions:
All 'property' elements must have attributes 'key', 'name', 'type' and 'default'.
When type=sop:string, 'default' must be of type 'xsd:string'.
When type=sop:integer, 'default' must be of type 'xsd:integer'.
When type=sop:range, 'default' must be of type 'xsd:integer' and be an valid index for the options. Also, there has to be childnodes of name 'option'.
When type=sop:boolean, 'default' must be of type 'xsd:boolean'.
When type=sop:double, 'default' must be of type 'xsd:double'.

I have the following schema, but am still having trouble with the restrictions for the 'type' attribute and childnodes etc. I know that Visual Studio shows this kind of behavior, I'm just not how to implement this myself.

<?xml version="1.0" encoding="utf-8" ?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.myns.net/SOP"
xmlns="http://www.myns.net/SOP"
xmlns:sop="http://www.myns.net/SOP"
elementFormDefault="qualified">

<!-- Element template-->
<xsd:element name="template">
<xsd:complexType>
<xsd:all minOccurs="1">
<xsd:element ref="defaultName" minOccurs="1" maxOccurs="1" />
<xsd:element ref="properties" minOccurs="1" maxOccurs="1" />
</xsd:all>
</xsd:complexType>
</xsd:element>

<!-- Element defaultName -->
<xsd:element name="defaultName" type="xsd:string" default="SOP Document" />

<!-- Element properties -->
<xsd:element name="properties">
<xsd:complexType>
<xsd:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element ref="property" />
</xsd:choice>
</xsd:complexType>
<xsd:unique name="unique-property-key">
<xsd:selector xpath="./sop:property" />
<xsd:field xpath="@key" />
</xsd:unique>
</xsd:element>

<!-- Element property -->
<xsd:element name="property">
<xsd:complexType>
<xsd:sequence minOccurs="0">
<xsd:element name="option">
<xsd:complexType>
<xsd:attribute name="value" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="key" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[a-zA-Z]{1,}" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="type" use="required">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="sop:string" />
<xsd:enumeration value="sop:integer" />
<xsd:enumeration value="sop:range" />
<xsd:enumeration value="sop:boolean" />
<xsd:enumeration value="sop:select" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>

</xsd:schema>