Newbie XML Schema Constraint Question

N

Neil Whitlow

I understand constraints on elements for the most part, but haven't
been able to find an explanation of a constraint that depends on other
elements within the document.

Assume I have something along the lines of the following in a
document:

<DataSet>

<DataTable name="InvoiceTable">
<DataColumn>InvoiceID</DataColumn>
<DataColumn>VendorID</DataColumn>
etc, etc, etc.
</DataTable>
<DataTable name="VendorTable">
<DataColumn>VendorID</DataColumn>
<DataColumn>VendorName</DataColumn>
etc, etc, etc.
</DataTable>

<Relation>
<ParentTable name="VendorTable" relatedcolumn="VendorID">
</ParentTable>
<ChildTable name="InvoiceTable" relatedcolumn="VendorID">
</ChildTable>
</Relation>

</DataSet>

I have minoccurs = 1/max occurs = unbounded on the DataTable element.
I may have as many or as few as I want as long as I have 1. The
Relation element is purely optional, minoccurs = 0.

What I cannot figure out is how to require "at least 2" DataTable
elements **IF** there is a Relation element in the document. I still
want to keep the general rule for DataTables to be (1 to unbounded),
UNLESS there is a Relation element that means two are required.

How do can you get the schema to look for the presence of other
elements in the document in order to validate a constraint like that?
Is there some special constraint I'm missing where you can specify
some XPath query as an attribute (an Xpath query that looks for the
presence of a Relation element)?

I'd also like it to enforce that the name attribute of the Parent and
Child tables in the Relation are actually the names of DataTable
elements that appear earlier in the document.

And as overkill, what about making sure that the relatedcolumn
attributes are the names of actual DataColumns that appear in the
given DataTable nodes.

Any help or direction anyone could provide on this would be most
appreciated.
 
C

C. M. Sperberg-McQueen

I understand constraints on elements for the most part, but haven't
been able to find an explanation of a constraint that depends on other
elements within the document.

Assume I have something along the lines of the following in a
document: [example trimmed]
What I cannot figure out is how to require "at least 2" DataTable
elements **IF** there is a Relation element in the document. I still
want to keep the general rule for DataTables to be (1 to unbounded),
UNLESS there is a Relation element that means two are required.

How do can you get the schema to look for the presence of other
elements in the document in order to validate a constraint like that?
Is there some special constraint I'm missing where you can specify
some XPath query as an attribute (an Xpath query that looks for the
presence of a Relation element)?

There is not such a construct in XML Schema 1.0. In this case,
however, if I have understood your structure correctly you may
not need it. Think of it as an exercise in regular-expression
construction. If I've understood correctly, you now have a content
model which, in XML DTD content-model notation, reads:

(DataTable+, Relation*)

and want to accept anything in that language EXCEPT the string

DataTable, Relation

i.e. Relation is legal only after at least two DataTables. This
is straightforward as a regular expression, and straightforward
to express in DTDs or in XML Schema:

(DataTable, (DataTable+, Relation*)?)
I'd also like it to enforce that the name attribute of the Parent and
Child tables in the Relation are actually the names of DataTable
elements that appear earlier in the document.

You should be able to do this with identity constraints.
And as overkill, what about making sure that the relatedcolumn
attributes are the names of actual DataColumns that appear in the
given DataTable nodes.

Again, look at identity constraints, this time with both the
DataTable and DataColumn names in the key.
Any help or direction anyone could provide on this would be most
appreciated.

I hope this helps.

-C. M. Sperberg-McQueen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top