The key
element is not supposed to go inside the sequence
element. Instead, you put it as a child of the xs:element
that defines ReferralCollection
. The selector
and field
are child elements of the key
element that define what is supposed to be unique. Both are XPath expressions. The selector
value sets up the XPath context, and the field
elements (there can be more than one) define the combination of values that are supposed to be unique. So in your case, the key
element might look something like this:
<xs:key name="URL">
<xs:selector xpath="."/>
<xs:field xpath="rd:keyword"/>
</xs:key>
The selector
here selects the element itself, that is, ReferralCollection
. Then, in that context, the field
element denotes the values of the keyword
elements, which are thus constrained to be unique. The value of an element in XPath is its text content.
I haven't done much with key
elements (or XML Schema in general), so some details above might not be correct, but the basic idea is what I explained, so it should be easy to fix any mistakes I made. Oh, also note that I used the rd
prefix for your READOO
namespace; in my experience using explicit prefixes is always best when dealing with XPath, so you need the appropriate xmlns:rd
attribute in your schema to make this work.
EDIT: So it seems you want something like this:
<xs:element name="ReferralCollection">
<xs:complexType>
<xs:sequence>
<xs:element name="URL" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="stem" type="xsd:string"/>
<xs:element name="keyword" maxOccurs="unbounded">
...
</xs:element>
...
</xs:complexType>
<xs:key name="URL">
<xs:selector xpath="./URL"/>
<xs:field xpath="stem"/>
</xs:key>
</xs:element>
Is this closer to what you meant? It has URL
elements, one or more, each containing a stem
element followed by one or more keyword
elements. The key
constraint should make the contents of the stem
elements unique.
You're getting the error because xsd:restriction
is not allowed as a child of xsd:element
. Try adding your xsd:restriction
to an xsd:simpleType
and then specifying that type in your xsd:element
.
You could add the xsd:simpleType
directly to the xsd:element
, but since you're using the same restriction 3 times, it makes more sense to put it in a simpleType outside of the elements.
Here's an example. I named the simpleType "stackOverflowTest":
<xsd:schema elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="company_donation_request" type="company_donation_requestType" />
<xsd:complexType name="company_donation_requestType">
<xsd:sequence>
<xsd:element name="order" type="orderType"></xsd:element>
<xsd:element name="donation" type="donationType"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="donationType">
<xsd:sequence>
<xsd:element name="campaign_key" type="stackOverflowTest" minOccurs="1" maxOccurs="1"/>
<xsd:element name="amount" type="xsd:decimal" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="orderType">
<xsd:sequence>
<xsd:element name="id" type="stackOverflowTest" minOccurs="1" maxOccurs="1"/>
<xsd:element name="fulfillment_date" type="stackOverflowTest" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="stackOverflowTest">
<xsd:restriction base="xsd:string">
<xsd:minLength value="2"/>
<xsd:maxLength value="255"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Hope this helps.
Best Answer
I'd say you are missing a
xsd:sequence
, furthermorescreenname
cannot bexsd:string
AND acomplexType
at the same time - you have to chose one or the other.Probably this is what you need: