By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

How to allow arbitrary content but make one requirement

P: n/a
Hi,

I have a schema which is fine except for one part giving me trouble:

(...)

<xs:element name="DOC">
<xs:complexType mixed="true">
<xs:sequence>
<xs:any processContents="lax" minOccurs="0"
maxOccurs="unbounded" />
<xs:element ref="IEOutput" minOccurs="1" maxOccurs="1" />
</xs:sequence>
<xs:attribute name="ADEPTID" type="xs:string" use="required" />
<xs:anyAttribute processContents="lax" />
</xs:complexType>
</xs:element>

(...)
I get the error: "The content model must be deterministic. Wildcard
declaration along with a local element declaration causes the content
model to become ambiguous."

I can see how at validation-time it is ambiguous whether the <xs:any>
or <xs:element> particle should be responsible for covering a given
<IEOutput> element (which I guess means XML processors aren't
responsible for things like backtracking).

My question is this: how do I say in a schema that "you can have any
number of arbitrary children so long as one of them conforms to the
(elsewhere defined) <IEOutput> element particle"? What I really mean
by "one of them" is "exactly one" but I'd also settle for "at least
one". And I also have a requirement that the <IEOutput> element should
come last, but I could drop that req'ment too.
Thanks
Mike
Nov 11 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

Hi,

<<My question is this: how do I say in a schema that "you can have any
<<number of arbitrary children so long as one of them conforms to the
<<(elsewhere defined) <IEOutput> element particle"?

You cannot without being non-deterministic if the required element and the
other elements are in the same schema namespace, and the .NET and MSXML
schema processors do not permit non-deterministic schemas.

What you can do is define the required element in a disctinct schema
namespace...

reqElem.xsd:

<?xml version="1.0" standalone="yes"?>
<xs:schema xmlns="urn:test" targetNamespace="urn:test"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="RequiredElement" type="xs:string"/>
</xs:schema>

... and in the main schema define the complexType sequence content model as
comprising of 2 xs:any definitions. For the second any element set the
minOccurs and maxOccurs to 1 and the namespace attribute to the namespace
of the schema that defines the required element. Since this namespace
defines only one element, the instance documents must contain it dues to
the min and max occurs contraints defined on the any element. For the first
any element set minOccurs = 0, maxOccurs = "unbounded" and the namespace
attribute to a list of namespaces excluding the namespace in which you
defined the required element that must appear only once. If you ommit the
namespace attribute... the default will be ##any which implies that
multiple occurences of the "once only" required element can then occur.
Sample schema based on the above schema that defines the required element
is provided below. Note that this will also address your requirement of the
requirement element being the last element in the sequence:

data.xsd:

<?xml version="1.0" standalone="yes"?>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:r="urn:test">

<xs:import namespace="urn:test" schemaLocation="reqElem.xsd"/>

<xs:element name="elem1" type="xs:string"/>
<xs:element name="elem2" type="xs:string"/>

<xs:element name="MyElement">
<xs:complexType>
<xs:sequence>
<xs:any processContents="lax" minOccurs="0" maxOccurs="unbounded"
namespace="##local"/>
<xs:any processContents="lax" minOccurs="1" maxOccurs="1"
namespace="urn:test"/>

</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

The following is a sample instance document that will validate successfully
against the above schema:

<MyElement xmlns="" xmlns:r="urn:test">
<elem1>data</elem1>
<elem2>data</elem2>
<r:RequiredElement>data</r:RequiredElement>
</MyElement>

The following is a sample instance that will fail validation as the
required element is not the last element in the sequence:

<MyElement xmlns="" xmlns:r="urn:test">
<elem1>data</elem1>
<r:RequiredElement>data</r:RequiredElement>
<elem2>data</elem2>
</MyElement>

Hope this helps.

Thanks
Karthik

This posting is provided "AS IS" with no warranties and confers no rights.

Nov 11 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.