5.1 Specifying UniquenessSpecifying Uniqueness
ref29XML Schema enables us to indicate
that any attribute or element value must be unique within a
certain scope. To indicate that one particular attribute or
element value is unique, we use the unique element first to
"select" a set of elements, and then to identify the
attribute or element "field" relative to each selected
element that has to be unique within the scope of the set
of selected elements. In the case of our report schema,
report.xsd, the
selector
element's xpath
attribute contains an XPath expression,
r:regions/r:zip,
that selects a list of all the
zip elements in a report instance. Likewise, the
field element's
xpath attribute
contains a second XPath expression, @code,
that specifies that the code attribute values
of those elements must be unique. Note that the XPath
expressions limit the scope of what must be unique. The
report might contain another code attribute,
but its value does not have to be unique because it lies
outside the scope defined by the XPath expressions. Also
note that the XPath expressions you can use in the
xpath attribute
are limited to a [subset]
of the full XPath language defined in [XML Path Language 1.0].
We can also indicate combinations of fields that
must be unique. To illustrate, suppose we can relax the
constraint that zip codes may only be listed once, although
we still want to enforce the constraint that any product is
listed only once within a given zip code. We could achieve
such a constraint by specifying that the combination of zip
code and product number must be unique. From the report
document, 4Q99.xml,
the combined values of zip code and
number would be: {95819 872-AA},
{95819 926-AA}, {95819 833-AA},
{95819 455-BX}, and {63143 455-BX}.
Clearly, these combinations do not distinguish between zip
code and number combinations
derived from single or multiple listings of any particular
zip, but the combinations would unambiguously represent a
product listed more than once within a single zip. In other
words, a schema processor could detect violations of the
uniqueness constraint.Going
back to our purchase order example, suppose we want each item to have a
unique combination of part number and product name. We could achieve
such a constraint by specifying that for each item element,
the combined values of its partNum attribute
and its productName child must be unique.
To define combinations of values, we simply add
use multiple field elements to identify
all the values involved. So, to add the part number value
to our existing definition, we add a new field element whose xpath attribute value,
r:part/@number, identifies the
number attribute of part elements that
are children of the zip elements identified by
r:regions/r:zip:
NOTE:
A Unique Composed Value
<unique name="dummy1">
<selector xpath="r:regions/r:zip"/>
<field xpath="@code"/>
<field xpath="r:part/@number"/>
</unique>
<xsd:element name="items" type="Items">
<xsd:unique name="partNumAndName">
<xsd:selector xpath="item"/>
<xsd:field xpath="@partNum"/>
<xsd:field xpath="productName"/>
</xsd:unique>
</xsd:element>
|