|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Creating Nested Relationships in XML Schemas
You create a schema with nested relationships when XML data is represented by parent elements that contain nested child elements. For more information, see Nested Relationships in XML.
Tip If you have access to an XML file (.xml) that contains the structured data that your application needs to validate then creating a schema can be accomplished by following the instructions for Creating XML Schemas from XML Files.
To construct a schema to validate nested data
- Create the individual tables to represent the different hierarchies of data that your XML represents. For more information, see Creating Tables in XML Schemas.
- Select the table that represents the nested child element and drag and drop it onto the table that represents the parent element.
- If you have any tables that represent nested child elements, repeat Step 2.
The following example shows how to construct an XML Schema that validates nested data. In a nested relationship, the OrderDetails table is a nested child of the Orders table, which are both nested children of the Customers table.
<Customers> <CustomerID>ANATR</CustomerID> <CompanyName>Ana Trujillo Emparedados y helados</CompanyName> <Orders> <OrderID>10308</OrderID> <OrderDate>1996-09-18</OrderDate> <OrderDetails> <OrderWeight>20kg</OrderWeight> <ShipVia>International Air</ShipVia> </OrderDetails> </Orders> </Customers>
To create a schema to illustrate a nested relationship
- Following the instructions in the section Creating Tables in XML Schemas and create three tables. For this example there is no need to define columns, just create the tables and name them: Customers, Orders, and OrderDetails.
- Click and drag the header of the Orders table and drop it onto the Customers table.
An Orders column appears in the Customers table named Orders, and a line appears connecting the two tables.
- Click and hold the header of the OrderDetails table and drop it onto the Orders table.
An OrderDetails column appears in the Orders table named OrderDetails, and a line appears connecting the two tables.
You can see the nested relationship illustrated on the designer by the lines that connect all three tables together, as well as the existence of the nested child tables as columns in each parent table throughout the hierarchy.
You have just created a schema that represents a nested relationship. More importantly switch to XML view (click the XML tab on the bottom of the design surface) and you can now see the apparent nested structure of the schema.
Relational Data in XML Schemas | Tables, Columns, Keys, and Constraints in XML Schemas | Nested Relationships in XML | Creating Tables in XML Schemas | Creating Primary and Unique Keys in XML Schemas | XML Schemas and Data | XML Schemas and the XML Designer | Creating XML Schemas and Datasets | XML Files