Using Annotated XSD Schemas for Creating XML Views tutorial
Using sql:relationship to Specify Relationship on an attribute

## Example.

The schema in this example includes a 'Customer' element with 'CustomerID' and 'ContactName' child elements and an OrderIDList attribute of IDREFS type. The 'Customer' element maps to the Customers table. By default, the scope of this mapping applies to all the child elements or attributes unless sql:relation is specified on the child element or attribute, in which case, the appropriate primary-key/foreign-key relationship must be defined using the 'relationship' element. And the child element or attribute, which specifies the different table using the relation annotation, must also specify the relationship annotation.

    <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" />
         </xsd:appinfo>
    </xsd:annotation>

      <xsd:element name="Customer" sql:relation="Demo.demo.Customers" type="CustomerType" />
       <xsd:complexType name="CustomerType" >
         <xsd:sequence>
            <xsd:element name="ContactName"  type="xsd:string" />
            <xsd:element name="CompanyName"  type="xsd:string" />
            <xsd:element name="City"  type="xsd:string" />
         </xsd:sequence>
         <xsd:attribute name="OrderIDList"
                         type="xsd:IDREFS"
                         sql:relation="Demo.demo.Orders"
                         sql:field="OrderID"
                         sql:relationship="CustOrders" >
            </xsd:attribute>
         <xsd:attribute name="CustomerID"   type="xsd:string" />
        </xsd:complexType>
    </xsd:schema>

Let the schema is written to the file 'Cust_Order_attr.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_attr'] /Customer[@CustomerID="QUEEN"]

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

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