Section 11.2.  Secondary XML documents

Prev don't be afraid of buying books Next

11.2. Secondary XML documents

The Widget Wear product catalog is stored in an XML document. It looks something like Example 11-1.

Example 11-1. A product catalog in XML (catalog.xml)
 <catalog>   <product >     <name>Ten-Gallon Hat</name>     <dept>ACC</dept>   </product>   <product >     <name>White Linen Blouse</name>     <dept>WMN</dept>   </product>   <product >     <name>Deluxe Golf Umbrella</name>     <dept>ACC</dept>   </product>   <product >     <name>Miscellaneous</name>     <dept>NA</dept>   </product> </catalog> 

11.2.1 For validation

When a user enters a product number, you want it to be checked against the product catalog to determine whether it is valid. The script functions shown in Example 11-2 will accomplish this. This script can be seen in action using the order_xml.xsn example file. The OnValidate function for the item number field calls another function, lookupCatalog, to determine whether the product number exists in the catalog. The line if (!desc) determines whether the desc variable contains a null value. If it does, it was not found in the catalog and an error is reported.

Example 11-2. Script to validate product number against catalog
 function msoxd__item_number_attr::OnValidate(eventObj) {   var desc = lookupCatalog(eventObj.Site.nodeTypedValue)   if (!desc)     eventObj.ReportError(eventObj.Site,       "Invalid product number.", false) } function lookupCatalog(prodNum) {   var catDom = XDocument.GetDOM("catalog");   var nameNode = catDom.selectSingleNode(         "//product[@num='" + prodNum + "']/name");   if (nameNode)     return nameNode.nodeTypedValue; } 

The lookupCatalog function first assigns the DOM of the catalog document to the variable catDom using the GetDOM method and referring to the document by the name specified for the secondary data source (catalog). It then selects from the catalog the name node that has that product number. It does this by stringing together an XPath expression that will look, for example, like: //product[@num='334']/name

The brackets in the XPath expression surround what is known as a predicate, which limits results to nodes that meet a certain criterion; in this case, those whose num attributes are equal to 334.[1]

[1] There is more on predicates in 24.2.1.2, "Predicates", on page 507.

If the function finds an appropriate node, it returns the node's typed value. If it does not find such a node, it returns a null value, signaling to the OnValidate function that the product was not found in the catalog.

11.2.2 To complete fields

Suppose that in addition to validating the product number, you want to fill in the product description automatically with the name data found in the catalog. The script function shown in Example 11-3 will accomplish this.

Example 11-3. Script to fill in the product name
 function msoxd__item_number_attr::OnAfterChange(eventObj) {   if (eventObj.IsUndoRedo) return;   var descNode = eventObj.Site.selectSingleNode("../ns1:desc")   var desc = lookupCatalog(eventObj.Site.nodeTypedValue)   if (desc)     descNode.nodeTypedValue = desc   else     descNode.nodeTypedValue = "" } 

The function is triggered by the OnAfterChange event, which occurs after the OnValidate event. It is very similar to the OnValidate function, in that it calls the lookupCatalog to retrieve the product description. This function, however, updates the description node with the name retrieved from the catalog. Note that it uses the relative XPath expression ../ns1:desc to retrieve the description. The .. step accesses the parent, while the ns1:desc step accesses the desc child of the parent.

11.2.3 For drop-down lists

Rather than validating a product number after it is entered, you could forestall errors by including the valid values in a drop-down list. This technique does not require a script; it is simply a matter of setting up the list control.

To test this out on our order form example:

1. Select the product number field and right-click.

2. On the resulting context menu, point to Change To, then click Drop-Down List Box.

3. Double-click the field to bring up its properties dialog, shown in Figure 11-2.

Figure 11-2. The Drop-Down List Box Properties dialog




4. Under List box entries, click Look up in a database, Web service, or file.

5. Select the catalog data source from the drop-down list.

6. Use the button to the right of the Entries box to bring up the data source structure and select the num node.

7. Click OK. The Entries field should now say /catalog/product/@num.

8. Click OK again to return to the form.

Amazon


XML in Office 2003. Information Sharing with Desktop XML
XML in Office 2003: Information Sharing with Desktop XML
ISBN: 013142193X
EAN: 2147483647
Year: 2003
Pages: 176

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net