Newbie XML Schema Constraint Question

Discussion in 'XML' started by Neil Whitlow, Sep 18, 2003.

  1. Neil Whitlow

    Neil Whitlow Guest

    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.
    Neil Whitlow, Sep 18, 2003
    #1
    1. Advertising

  2. (Neil Whitlow) writes:

    > 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
    C. M. Sperberg-McQueen, Sep 23, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stanimir Stamenkov
    Replies:
    1
    Views:
    490
    Stanimir Stamenkov
    Jan 5, 2006
  2. Replies:
    5
    Views:
    754
    Priscilla Walmsley
    Jan 16, 2006
  3. Replies:
    0
    Views:
    439
  4. puvit82
    Replies:
    4
    Views:
    746
    puvit82
    Feb 1, 2008
  5. Replies:
    1
    Views:
    1,061
    Martin Honnen
    Sep 26, 2008
Loading...

Share This Page