The next step is to create an ID attribute for each of the
structural (nondata point) elements we have defined so far in our XML
database (with the exception of the root element). This is to
uniquely identify elements that need to be referred to by other
elements.
For the name of the attribute, we use the element name followed by
ID. This might cause name collisions with other attributes that have
already been added to the XML, in which case we need to change the
names of these as appropriate. These should be defined as being of
type ID, and must be declared as #REQUIRED. If we add these ID
attributes to each element for now, we can optionally remove some
from the created XML structures when we come to model all of the
relationships.
When populating these structures, a unique ID will need to be
created for each instance of an element that is generated. We need to
ensure that these IDs are unique not only across all elements of a
specific type, but across all elements in our document. One way to do
this programmatically (assuming that we're using automatically
incremented integers for the primary keys in our database) is to use
the primary key for the row being created, prefixed by the name of
the table in which it appears.
For example, for the customer in our database with the ID number
17, we might use the string Customer17 for the value of the
CustomerID attribute on the Customer element. If we have nonnumeric
keys in our database, or similar table names with numeric suffixes
(like Customer and Customer1), this may cause name collisions - as
always, be on the look out for these.
In our example, then, we have:
<!ELEMENT SalesData EMPTY>
<!ATTLIST SalesData
Status (NewVersion | UpdatedVersion | CourtesyCopy) #REQUIRED>
<!ELEMENT Invoice EMPTY>
<!ATTLIST Invoice
InvoiceID ID #REQUIRED
InvoiceNumber CDATA #REQUIRED
TrackingNumber CDATA #REQUIRED
OrderDate CDATA #REQUIRED
ShipDate CDATA #REQUIRED>
<!ELEMENT Customer EMPTY>
<!ATTLIST Customer
CustomerID ID #REQUIRED
Name CDATA #REQUIRED
Address CDATA #REQUIRED
City CDATA #REQUIRED
State CDATA #REQUIRED
PostalCode CDATA #REQUIRED>
<!ELEMENT Part EMPTY>
<!ATTLIST Part
PartID ID #REQUIRED
PartNumber CDATA #REQUIRED
Name CDATA #REQUIRED
Color CDATA #REQUIRED
Size CDATA #REQUIRED>
<!ELEMENT MonthlyTotal EMPTY>
<!ATTLIST MonthlyTotal
MonthlyTotalID ID #REQUIRED
Month CDATA #REQUIRED
Year CDATA #REQUIRED
VolumeShipped CDATA #REQUIRED
PriceShipped CDATA #REQUIRED>
<!ELEMENT MonthlyCustomerTotal EMPTY>
<!ATTLIST MonthlyCustomerTotal
MonthlyCustomerTotalID ID #REQUIRED
VolumeShipped CDATA #REQUIRED
PriceShipped CDATA #REQUIRED>
<!ELEMENT MonthlyPartTotal EMPTY>
<!ATTLIST MonthlyPartTotal
MonthlyPartTotalID ID #REQUIRED
VolumeShipped CDATA #REQUIRED
PriceShipped CDATA #REQUIRED>
<!ELEMENT LineItem EMPTY>
<!ATTLIST LineItem
LineItemID ID #REQUIRED
Quantity CDATA #REQUIRED
Price CDATA #REQUIRED>
Rule 5: Add ID Attributes to the Elements.
Add an ID attribute to each of the elements we have created in our
XML structure (with the exception of the root element). Use the
element name followed by ID for the name of the new attribute,
watching as always for name collisions. Declare the attribute as type
ID, and #REQUIRED.