Enhanced Grid View


In the preceding section, you have been working with XML documents exclusively using XMLSPY’s Text view, which is ideal for manually editing any XML document at the code level. Although it is important to understand the basics and be able to work with XML documents at code level, as XML documents become bigger and increasingly more complex in their structure, working with XML can quickly become a laborious task. As previously mentioned, XMLSPY can greatly simplify your work with XML documents by providing various powerful graphical views of a document. In this chapter, you learn to use the Enhanced Grid view, which I henceforth refer to as simply Grid view.

Viewing XML documents in Grid view

After you have opened an XML document within the XMLSPY editing environment, you can easily switch back and forth between Text view and Grid view (or any view for that matter) at any time, simply by choosing View ? Enhanced Grid View (as shown in Figure 2-14). Alternatively, you can click the Enhanced Grid View button on the main toolbar. Note that you cannot switch from one view to another unless the focus is on the main editing window. For example, if the focus is on the Project window, the various view menu options and toolbar buttons will be grayed out. Simply clicking the main editing window returns the focus to that window and enables you to switch views.


Figure 2-14: Switching from Text view to Enhanced Grid view using the View menu.

How XML documents are rendered in Grid view

For this section, you need to use a slightly more advanced XML sample file than the basic XML file you built previously. Open the OrgChart.xml sample file located in the C:\Program Files\Altova\XMLSPY\Examples directory. Choose either File ? Open or click the Open File button on the main toolbar and then switch to Grid view. You should see something similar to the screenshot in Figure 2-15. The OrgChart.xml file is one of the default sample files included with XMLSPY.

click to expand
Figure 2-15: A collapsed XML document shown in Grid view.

Figure 2-15 shows the OrgChart.xml file collapsed into two rows (a processing instruction, and the root element). To further expand the contents of the XML document, toggle the two gray arrow buttons (CompanyLogo and Office) that appear to the right of the OrgChart element. After expanding the OrgChart element, you will find more nested elements that you also must expand. Practice expanding and collapsing the entire document tree by toggling the arrow buttons.

The Grid view, as its name suggests, is a tabular (or grid-like) view of an XML document, which in some respects is similar to a spreadsheet or relational database view of data. The main difference between Grid view and a Microsoft Excel spreadsheet or Microsoft Access database table is that databases and spreadsheets both have fairly rigid, tabular data sources. An XML document, on the other hand, has a very extensible underlying tree-like structure. Grid view displays repeating patterns of elements and/or attributes within an XML document as rows in a table; the columns of the table are determined by the sequence of repeated elements and/or attributes. This graphical rendering methodology is applied recursively throughout an entire XML document, nesting tables within tables, all of which can be expanded or collapsed by toggling the arrow button. This nested table structure is referred to as a semantic table. Semantic tables represent a very important principle that you need to understand in order to effectively work with XML documents in Grid view.

Note

In Grid view, for each repeating pattern of XML elements and/or attributes, Grid view builds a table. Each time the repeated pattern is encountered, Grid view builds a new row in the table. The columns of the table are determined by the sequence of elements and attributes within one instance of the repeating pattern. Repeating patterns within a repeating pattern are represented as a nested table within a table. Each table structure in Grid view is referred to as a semantic table.

Figure 2-16 shows an XML document fragment displayed as a semantic table in Grid view; it is the result of expanding the OrgChart, Office, and Department elements.

click to expand
Figure 2-16: A section of the OrgChart.xml document displayed in Grid view.

The Department table of Figure 2-16 has two columns (the sequence of repeated elements Name and Person), and four rows (Administration, Marketing, Engineering, IT & Technical Support). Note that the number in parentheses, (4), corresponds to the number of rows in the current table. (In general, a number in parentheses to the right of any element name indicates the number of times a pattern is encountered, also corresponding to the number of rows in the semantic table.) The <> indicates that both the Name and Person columns are XML elements. Name is a single-valued element, so the content of the Name column corresponds to the content of the <Name> element. The content of the <> Person column is actually a complex element, which is simply an element that contains one or more child elements. The complex element Person appears in the Department table as an unexpanded, nested-table structure (denoted by an unexpanded arrow button). Take a look at the OrgChart.xml file listing in Figure 2-17 to see what is happening here.

Figure 2-17 is a decomposition of a section of the OrgChart.xml file, which was graphically displayed as a semantic table in Grid view in Figure 2-16. Notice that some <Person> elements have been omitted for the sake of brevity. Figure 2-17 has several repeating patterns; the outermost pattern is the four-time repeating sequence of <Department> elements. The semantic table of Figure 2-16 has exactly four rows, corresponding to each of the four repeating <Department> elements. The two columns of Figure 2-16 correspond to <Department>’s two child elements, <Name> and <Person>, respectively.

click to expand
Figure 2-17: The mapping of an XML document to a semantic table in Grid view.

To continue the analysis of the Grid view, expand the nested <Person> elements of Figure 2-16 (represented as nested semantic tables) by toggling the arrow buttons adjacent to the Person nodes, as shown in Figure 2-18. Notice that the highlighted Person (3) table has exactly three rows (Vernon, Frank, and Loby). So again, the (3) represents the number of rows in the current table.

Let’s analyze the highlighted semantic table of Figure 2-18 in more detail by looking at its corresponding XML document fragment from OrgChart.xml file—the XML source is listed in Figure 2-19.

click to expand
Figure 2-18: Expanding a nested Person table of the Department table, from the OrgChart.xml document, using Grid view.

click to expand
Figure 2-19: Mappings from the XML source listing to the semantic table highlighted in Figure 2-18.

This three-time repeating sequence of <Person> elements in the OrgChart.xml file is represented in Grid view as a Person table with columns: First, Last, Title, PhoneExt, Email. These columns are child elements of <Person>. The Person table has exactly three rows, corresponding to each of the three repeating <Person> elements. In summary, XMLSPY’s Grid view, through the use of semantic tables, enables you to easily visually analyze the contents of an entire XML document. For example, the OrgChart.xml file is over 200 lines in length, yet by toggling just a few arrow buttons, you can quickly zoom in or out of any section of the XML document.

Working with Grid view

Grid view’s semantic tables enable you to perform editing operations on an XML document as a whole, as opposed to editing only one line at a time using Text view. This will make more sense in the next few subsections, as you cover some of the most common XML editing operations: inserting and deleting elements or attributes, moving elements and attributes, switching elements and attributes, and importing and exporting data. You can accomplish all of this easily by using Grid view.

Note

The XMLSPY Grid view (as well as all other views) supports unlimited Undo (Ctrl+Z) and Redo (Ctrl+Y). So if you make a mistake at any time when you’re in Grid view, you can easily recover from the error by using these features.

Inserting and Deleting Elements and Attributes

Grid view is ideal for modifying the structure of an existing XML document. You can, for example, change every <Person> element to include an additional attribute, emp-id, which contains an employee identification number. You can also add an additional sub-element called <middle> that corresponds to a person’s middle name. The desired output would look similar to the XML document shown in Listing 2-1.

Listing 2-1: Modifying the Person Element

start example
<Person emp->   <First>Vernon</First>   <Middle>James</Middle>   <Last>Callaby</Last>   <Title>Office Manager</Title>   <PhoneExt>582</PhoneExt>   <EMail>v.callaby@nanonull.com</EMail> </Person>
end example

Of course you could simply type this additional information manually for each <Person> element throughout the document; however, using Grid view, it is possible to insert attributes for all the <Person> elements simultaneously. Simply select a column header within the <Person> semantic table (such as the <> First column), right-click the column, and choose Insert ? Attribute, as shown in Figure 2-20.

click to expand
Figure 2-20: Adding a new attribute to every <Person> element within the current semantic table.

In Grid view, you can easily add additional elements and attributes to any semantic table simply by right-clicking a table column header adjacent to where you would like the new element to be inserted and choosing Insert ? Element. XMLSPY auto-inserts the new element. Be sure to name the new columns emp-id and Middle, respectively. Next, type some sample values into the new emp-id attribute and <Middle> element fields so that you get a result similar to the one shown in Figure 2-21.

click to expand
Figure 2-21: Inserting an attribute and an element into a table. Columns starting with = correspond to attributes, and columns starting with greater< > correspond to elements.

It is important to remember that any change you make to an XML document in Grid view is automatically synchronized with any other view in XMLSPY. This behavior can be verified by switching to Text view by choosing View ? Text View and examining the XML source document for the section of the OrgChart.xml file just edited. The XML document fragment is shown in Listing 2-2 with the newly inserted attributes and elements shown in bold.

Listing 2-2: Modified Semantic Table

start example
<Person emp->    <First>Vernon</First>    <Middle>James</Middle>    <Last>Callaby</Last>    <Title>Office Manager</Title>    <PhoneExt>582</PhoneExt>    <EMail>v.callaby@nanonull.com</EMail> </Person> <Person emp->    <First>Frank</First>    <Middle>Edward</Middle>    <Last>Further</Last>    <Title>Accounts Receivable</Title>    <PhoneExt>471</PhoneExt>    <EMail>f.further@nanonull.com</EMail> </Person> <Person emp->    <First>Loby</First>    <Middle>Sarah</Middle>    <Last>Matise</Last>    <Title>Accounting Manager</Title>    <PhoneExt>963</PhoneExt>    <EMail>l.matise@nanonull.com</EMail> </Person>
end example

In the following subsections, you make changes to an XML document in Grid view. After making the changes, try switching back to Text view to verify that XMLSPY has updated the underlying XML document source.

Moving Elements and Attributes

Using Grid view, it is easy to change the ordering of attributes or elements in an XML document. Practice selecting any column of a semantic table by clicking the column header. Drag over the silhouette of the column to its new location and release the mouse button. Figure 2-22 shows the dragging and dropping of a <Title> column so that it appears as the first sub-element of its parent, the <Person> element.

click to expand
Figure 2-22: Changing the ordering of child elements in Grid view.

After you switch back to Text view, you see that the <Title> element appears before the <First> element for all six <Person> elements in this particular XML document fragment.

Switching Elements and Attributes

Changing an XML element into an XML attribute, or the reverse—changing an XML attribute into an XML element—is a very common XML editing task. This situation arises frequently because there isn’t a universally accepted guideline dictating exactly if information should be represented as an XML element or XML attribute. (In practice, you generally try out a few different possibilities!) As an example, try changing the <EMail> element so that it appears as an attribute of <Person>. In Grid view, you can switch content represented as XML elements to XML attributes by first selecting a column of elements to operate on. Clicking a semantic table’s column header highlights the entire column, as shown in Figure 2-23. Then, right-click the selected column and choose Convert to ? Attribute from the submenu.

click to expand
Figure 2-23: Changing the <Email> Element to an attribute using Grid view.

Similarly, you convert an XML attribute to an XML element by selecting a column of attributes in Grid view, right-clicking the selected column, and then choosing Convert to ? Element from the submenu.

Renaming Elements and Attributes

Using Grid view, it is easy to simultaneously rename multiple occurrences of elements or attributes throughout an XML document. Double-click the column header of any semantic table, which places an active cursor in the column header. (Note: You can also simply select a column header and press the F2 key.) Editing the header field renames all elements or attribute names in the selected column. Figure 2-24 shows an XML element being renamed from <Title> to <Job-Title>.

click to expand
Figure 2-24: Renaming an element from <Title> to <Job-Title>.

Sorting XML Documents

XMLSPY’s Grid view supports the sorting of any semantic table by any XML element or attribute field. For example, suppose that you want to sort a sequence of six <Person> elements, alphabetically by last name. In Grid view, you do this by selecting the sort column (in this example, the <Last> element), right-clicking the selected column, and then choosing Ascending Table Sort from the menu (see Figure 2-25). Conversely, to reverse-alphabetize the sequence of <Person> elements by last name, right-click the <Last> column and select Descending Table Sort.

click to expand
Figure 2-25: Sorting a sequence of <Person> elements by last name.

Importing and Exporting Data with Microsoft Excel

Grid view is ideally suited to copy and accept content from Microsoft Office applications such as Microsoft Excel, Microsoft Access, Microsoft Word, or Microsoft PowerPoint. You export content from an XML document to Microsoft Excel by selecting a region of a semantic table in Grid view, right-clicking the selected region, and choosing Copy as Structured Text from the menu (see Figure 2-26). This copies the selected data to the Clipboard as tab-delimited data.

click to expand
Figure 2-26: Exporting data from Grid view for use with any external Windows application.

Next, launch Microsoft Excel (or any other Windows program) and place the cursor in the top-left corner of where you want to insert the previously selected information. Choose Edit → Paste or press Ctrl+V (the keyboard shortcut for the Paste command). The result is shown in Figure 2-27.

click to expand
Figure 2-27: Exporting XML data from XMLSPY’s Grid view to Microsoft Excel.

Similarly, you can import data from a Microsoft Office application, such as Excel, into XMLSPY by reversing what you just did to import data. Select the fields in Excel that you want to copy and place them on the Clipboard by choosing Edit → Copy or by pressing Ctrl+C. Open an XML document in Grid view and place the cursor in the top-left corner of a semantic table where the data is to be inserted. Choose Edit → Paste or press Ctrl+V. The Grid view automatically converts the tabular data into an XML format.




The XMLSPY Handbook
The Official XMLSPY Handbook
ISBN: 764549642
EAN: 2147483647
Year: 2001
Pages: 121
Authors: Larry Kim

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