[Solved] Query SQL database using XML schema

Richard Asks: Query SQL database using XML schema
I have xml files that describe data I want to retrieve out of an MS SQL database. e.g.

<Customer Type="Entity" ID="12345">
  <Territory Type="Entity" Table="Territory" Join="Territory.TerritoryID = Customer.TerritoryID"></Territory>  
  <Addresses Type="Collection" Table="Address" Join="Address.AddressID = Customer.AddressID"></Addresses>
  <Orders Type="Collection" Table="Order" Join="Order.CustomerID = Customer.CustomerID">
    <BillToAdress Type="Entity" Table="Address" Join="Address.AddressID = Order.BillToAddressID"></BillToAdress>
    <ShipToAddress Type="Entity" Table="Address" Join="Address.AddressID = Order.ShipToAddressID"></ShipToAddress>
    <OrderLines Type="Collection" Table="OrderLine" Join="OrderLine.OrderID = Order.OrderID">
      <Product Type="Entity" Table="Product" Join="Product.ProductID = OrderLine.ProductID">
        <ProductParts Type="Collection" Table="ProductPart" Join="ProductPart.ProductID = Product.ProductID"></ProductParts>

The output I want is a data set with multiple record sets like this;


CustomerID Name TerritoryID
12345 Acme Ltd 67


TerritoryID Name
67 California


AddressID CustomerID Address1 Address2 City Zip
890123 12345 12 Some Street Beverly Hills 90210