Microsoft XML 2.5 SDK


 

Lesson 10: Using the Master/Detail Feature with the C++ XML DSO

[This is preliminary documentation and subject to change.]

What is the master/detail feature?

The new master/detail feature allows you to bind to the current record of a hierarchical record set. In layman's terms, this means that you can now bind the child elements of the current record to a distinct table. For example, let's say you had the following XML:

<orders>
  <order order_number="2233">
    <customer>
      <name>John Smith</name>
      <custID>192883</custID>
    </customer>
    <item>      
      <name>Fly Swatter</name>    
      <price>9.99</price>    
    </item>  
  </order> 
  <order order_number="2234">  
    <customer>   
      <name>Marea Angela Castaneda</name>      
      <custID>827145</custID>  
    </customer>    
    <item>    
      <name>Fly Paper</name>    
      <price>15.99</price>    
    </item>  
  </order>  
  <order order_number="2235"> 
    <customer>   
      <name>Amy Jones</name>      
      <custID>998022</custID>   
    </customer>   
    <item>      
      <name>Mosquito Netting</name>
      <price>38.99</price>    
    </item>
  </order>
</orders>

You could allow your user to surf through the orders by ID, displaying only the customer and item information for the current order. Your user would not have to view the information for all of the orders, but only for the one in which he or she is interested.

How do I bind to the details?

The key to binding to lower levels in the hierarchy (the details) is to understand the structure of your data. The above XML has three elements within the root (the "orders") element. Based on the heuristic employed by the XML DSO, each order will be mapped to a rowset containing an "order_number", "customer", and "item" field. The "order_number" column will contain the value of the "order_number" attribute.  The "customer" and "item" columns will contain pointers to respective "customer" and "item" recordsets. The "customer" recordset will then contain a "name" and "custID" field with the values of those elements within. The "item" recordset will contain a "name" and "price" field with the values of those elements within.

So, with this in mind, note that within the top-level (the "orders") recordset, you can get to the value of the "order_number." You will then allow your user to surf through the orders by "order_number."

<P>ORDER NUMBER: <SPAN DATASRC="#xmlDoc" DATAFLD="order_number"></SPAN></P>

Now put in a couple of buttons to help them move throughout the "orders" recordset.

<INPUT TYPE=BUTTON VALUE="Previous Order" onclick="xmlDoc.recordset.movePrevious()">
<INPUT TYPE=BUTTON VALUE="Next Order" onclick="xmlDoc.recordset.moveNext()">

To get at the values within the subelements of the current record, create a table and set that table's DATASRC attribute to "#xmlDoc" exactly as was done above. However, this time also set its DATAFLD attribute to "customer." This tells the table to bind to data within the "customer" recordset pointed at within the "customer" field of the "orders" recordset.

<TABLE DATASRC="#xmlDoc" DATAFLD="customer" BORDER>
  <THEAD><TH>NAME</TH><TH>ID</TH></THEAD>
  <TR>
    <TD><SPAN DATAFLD="name"></SPAN></TD>
    <TD><SPAN DATAFLD="custID"></SPAN></TD>
  </TR>
</TABLE>

Then do the same for the data within the "item" element.

<TABLE DATASRC="#xmlDoc" DATAFLD="item" BORDER=1>
  <THEAD><TR><TH>ITEM</TH><TH>PRICE</TH></TR></THEAD>
  <TR>
    <TD><SPAN DATAFLD="name"></SPAN></TD>
    <TD><SPAN DATAFLD="price"></SPAN></TD>
  </TR>
</TABLE>

Now, as the user clicks the buttons and moves to the next and previous records in the recordset, the data in the tables will change to correspond to the current record.

If you have Internet Explorer 5, click the Show Example button to view the page created above.