Use the Master/Detail Feature with the C++ XML Data Source Object

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Developer's Guide

Use the Master/Detail Feature with the C++ XML Data Source Object

What is the master/detail feature?

The new master/detail feature allows you to bind to the current record of a hierarchical record set. This means that you can now bind the child elements of the current record to a distinct table. For example, consider the following XML.

<orders>
  <order order_number="2233">
    <customer>
      <name>James 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 can allow your user to navigate 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 orders—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 preceding XML has three elements within the root element, <orders>. Based on the heuristic employed by the XML Data Source Object (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 navigate through the orders by "order_number."

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

Now add some buttons to help users 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 retrieve the values within the child elements of the current record, create a table and set that table's DATASRC attribute to "#xmlDoc", exactly as was done in the preceding example. 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.

Click the Show Example button to view the page created above.

James Smith 192883 Fly Swatter 9.99 Candice Calloway 827145 Fly Paper 15.99 Mandy Jones 998022 Mosquito Netting 38.99

ORDER NUMBER:



NAMEID
ITEMPRICE

Try it!

See if you can use the preceding XML to create a Web page that will allow a user to navigate through the orders by customer name.