Using Annotated XSD Schemas for Creating XML Views tutorial
Using sql:relationship to Specify Relationship on multiple elements.

## Example. Specifying sql:relationship on multiple elements.

In this example, the annotated XSD schema contains the 'Customer', 'Order', and 'OD' elements. The 'Order' element is a subelement of the 'Customer' element. <sql:relationship> is specified on the 'Order' subelement; therefore, orders that belong to a customer appear as subelements of 'Customer'. The 'Order' element includes the 'OD' subelement. 'sql:relationship' is specified on 'OD' subelement, so the order details that pertain to an order appear as subelements of that 'Order' element.

    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
    <xsd:annotation>
      <xsd:appinfo>
        <sql:relationship name="CustOrders"
            parent="Demo.demo.Customers"
            parent-key="CustomerID"
            child="Demo.demo.Orders"
            child-key="CustomerID" />

        <sql:relationship name="OrderOrderDetail"
            parent="Demo.demo.Orders"
            parent-key="OrderID"
            child="Demo.demo.Order_Details"
            child-key="OrderID" />
      </xsd:appinfo>
    </xsd:annotation>

      <xsd:element name="Customer" sql:relation="Demo.demo.Customers" >
       <xsd:complexType>
         <xsd:sequence>
            <xsd:element name="Order" sql:relation="Demo.demo.Orders"
                  sql:relationship="CustOrders" maxOccurs="unbounded" >
              <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="OrderDetail"
                                 sql:relation="Demo.demo.Order_Details"
                                 sql:relationship="OrderOrderDetail"
                                 maxOccurs="unbounded" >
                      <xsd:complexType>
                        <xsd:attribute name="OrderID" type="xsd:integer" />
                        <xsd:attribute name="ProductID" type="xsd:string" />
                        <xsd:attribute name="Quantity" type="xsd:integer" />
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                  <xsd:attribute name="OrderID" type="xsd:integer" />
                  <xsd:attribute name="CustomerID" type="xsd:string" />
              </xsd:complexType>
            </xsd:element>
          </xsd:sequence>
          <xsd:attribute name="CustomerID" type="xsd:string" />
         </xsd:complexType>
      </xsd:element>
    </xsd:schema>

Let the schema is written to the file 'Cust_Order_OD.xsd', then after loading this file by `xml_load_mapping_schema_decl` function, the first example will produce a result for the XPath query:

    XPATH [__view 'Cust_Order_OD'] /Customer[@CustomerID="QUEEN"];

the second example will produce a result for the XQuery query:

    select xquery_eval('<doc>{for $r in xmlview("Cust_Order_OD")/*[@CustomerID="QUEEN"] return $r}</doc>', xtree_doc('<q/>'))