|
3.7. Exporting Through XML MapsOnce you have created lists containing XML data, you can export that data to a new XML file from Excel two ways:
In either case, you can only export data using one XML map at a time. If a workbook contains more than one XML map, you are prompted to choose the map to use (Figure 3-13). Figure 3-13. Exporting XML uses only one XML map at a time3.7.1. How it worksWhen Excel exports a list as XML, it uses the schema stored in the workbook to generate XML that matches the XML source file that the list was created from. However, Excel omits the following items:
For example, if you create a list from SimpleOrder.xml containing only names and totals, only those elements are saved when you export the list as XML: <Orders> <Order> <BillTo> <Address> <Name>Joe Magnus</Name> </Address> </BillTo> <Total> <Due>572.4</Due> </Total> </Order> <Order>...</Order </Orders> In the preceding XML, the original address and order information is omitted because it wasn't included in the list. From Excel's point of view, the data doesn't exist if it doesn't reside on a worksheet somewhere. 3.7.2. What this meansThe limitations that come with XML maps imply a set of approaches when using them with XML. You can't just assume that you will be able to successfully import, edit, and export arbitrary XML data using Excel. XML maps are best suited for XML structured a certain way. For example, the preceding SimpleOrder.xml sample requires some changes if you want to be able to view and edit orders via XML maps. Specifically:
These changes and other recommendations are explained in the following sections. 3.7.2.1 Avoid lists of listsExcel can import XML that contains lists more than one level deep, but it can't export those lists. In XML schema terminology, a list is an element with a maxOccurs attribute greater than one. Therefore, XML using the following schema can't be exported from an XML map: <xsd:element minOccurs="0" maxOccurs="unbounded" nillable="true" name="Order" form="qualified"> <xsd:complexType> <xsd:sequence minOccurs="0"> ... </xsd:sequence> <xsd:element minOccurs="0" maxOccurs="unbounded" nillable="true" name="Line" form="qualified"> <xsd:complexType> <xsd:sequence minOccurs="0"> ...</xsd:sequence> </xsd:complexType> </xsd:element> </xsd:complexType> </xsd:element> You can solve this problem by breaking the source XML into smaller pieces. In the case of SimpleOrder.xml , this means creating a separate file for each Order node. The XML map's root node then becomes Order (Figure 3-14). Figure 3-14. Break XML into smaller files to avoid lists of listsYou can organize the new, smaller files into a separate folder or by using a unique file extension, such as ".ord". For example, the following code allows the user to select an order file to open in Excel: Sub cmdOpenOrder_Click( ) ' Get a file name to open. Use ".ord" extension for orders. Dim fname As String fname = Application.GetOpenFilename("Orders (*.ord),*.ord", 1, _ "Open an Order", "Open", False) If fname <> "" Then ThisWorkbook.XmlMaps("Order_Map").Import (fname) End If End Sub Using the unique .ord file extension organizes orders (Figure 3-15). Excel (and XML) don't care what file extension you use when importing or exporting files. Figure 3-15. Organize XML files using a unique file extension3.7.2.2 Avoid denormalized dataIf you drag the Order node shown in Figure 3-14 onto a worksheet, you get a list containing denormalized data, as shown in Figure 3-16. Denormalized means that non-repeating data elements appear multiple times on the worksheet. A user could change one of the non-repeating items, such as Name, on one row, making that item inconsistent with other rows that are supposed to show the same data. There is no way for Excel to reconcile this inconsistency, so the list can't be exported. To avoid this, create non-repeating and repeating nodes in separate lists (Figure 3-17). Figure 3-16. A list with denormalized dataFigure 3-17. Put repeating and non-repeating data items in separate lists to avoid denormalized data3.7.2.3 Create an XML schemaAllowing Excel to infer a schema for an XML map is fine if the nodes don't contain optional items or if the first occurrence of each node contains all of its possible children. Otherwise, Excel may omit items from the schema it creates and some nodes won't appear in the XML map. You can solve this problem by creating an XML schema and referencing that schema in the XML file you import. Excel copies the referenced XML schema into the XML map when the XML map is created. Having an external XML schema is also useful for making changes to the XML map. As mentioned earlier, you can't update an XML map inside of Excel; you can, however, modify the XML schema stored in the workbook by editing it outside of Excel. To edit an XML map schema:
The XML spreadsheet nodes for the schema appear as follows. The nodes to edit or replace are highlighted in bold . <x2:MapInfo x2:HideInactiveListBorder="false" x2:SelectionNamespaces="xmlns:ns1='http://www.mstrainingkits.com'"> <x2:Schema x2: x2:Namespace="http://www.mstrainingkits.com"> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.mstrainingkits.com" xmlns:ns0="http://www.mstrainingkits.com"> <xsd:element nillable="true" name="Order"> ... </xsd:element> </xsd:schema> </x2:Schema> </x2:MapInfo> 3.7.2.4 Include all nodes if exportingWhen you export XML, Excel takes the data found in mapped items on worksheets, applies the XML map, and generates XML nodes defined in the XML map's schema. If some of the XML map's data nodes are not mapped, that data is omitted from the exported XML. In some cases, this is what you want. But if you are trying to read and write an XML file without losing content, you need to make sure that all elements from the XML map appear somewhere on the worksheet (even if they are hidden). In cases where a node contains a calculated value, you will need to perform the calculation in a non-mapped cell, then copy that value to the mapped cell before exporting (see Figure 3-17). The Save Order button in the sample copies the calculated subtotal, tax, and total values to cells created from the XML map before exporting the XML using the following code: Sub cmdSaveOrder_Click( ) ' Update mapped cells with calculated values. Range("XmlSubTotal") = Range("SubTotal") Range("XmlTax") = Range("Tax") Range("XmlTotal") = Range("Total") ' Create file name to save. Dim fname As String fname = ThisWorkbook.path & "\" & Range("OrderID") & ".ord" ' Save the order. ThisWorkbook.XmlMaps("Order_Map").Export fname, True End Sub 3.7.2.5 Other things to avoidThere are a number of other XML schema constructs that Excel does not support when importing XML, and a number of schema constructs that are not supported when exporting XML. These constructs are listed in Table 3-1 and Table 3-2.
|
|