Represent Data in XML

MSXML 5.0 SDK

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

Represent Data in XML

As with any other application, the first thing you must decide when you plan an XML application is how to represent your data. Data design is important because it affects which template rules you will use in the XSLT style sheet, and what kind of performance your XSLT solution can achieve.

In an XSLT solution, data comes in the form of XML documents. You can store data values in an XML element, either as the content of the element or as an attribute value. As an illustration, this example puts company information as the content of some elements, and the sales numbers as attributes of others. All this information is contained in the input XML file, Sales.xml.

To create the Sales.xml file

  1. Create a folder on your computer, such as C:\Documents and Settings\jsmith\My Documents\xsltTut.
  2. Use your HTML or text editor to create a new file. Copy the following code into that file.
    <?xml version="1.0"?>
    <sales>
       <summary>
          <heading>Scootney Publishing</heading> 
          <subhead>Regional Sales Report</subhead> 
          <description>Sales report for the West Coast, Central and East Coast regions</description> 
       </summary>
       <data>
          <region>
             <name>West Coast</name> 
             <quarter number="1" books_sold="24000" /> 
             <quarter number="2" books_sold="38600" /> 
             <quarter number="3" books_sold="44030" /> 
             <quarter number="4" books_sold="21000" /> 
          </region>
          <region>
             <name>Central</name> 
             <quarter number="1" books_sold="11000" /> 
             <quarter number="2" books_sold="16080" /> 
             <quarter number="3" books_sold="25000" /> 
             <quarter number="4" books_sold="29000" /> 
          </region>
          <region>
             <name>East Coast</name> 
             <quarter number="1" books_sold="27000" /> 
             <quarter number="2" books_sold="31400" /> 
             <quarter number="3" books_sold="40100" /> 
             <quarter number="4" books_sold="30000" /> 
          </region>
       </data>
    </sales>
  3. In the folder that you created in Step 1, save the file as Sales.xml. You can leave the file open.
  4. In Windows Explorer, locate and then double-click the Sales.xml file. This allows you to view the file in Internet Explorer.

    Internet Explorer shows the file as an unformatted XML listing. This is the default display for Internet Explorer when viewing an XML file.

Basic XSLT processing involves the following:

  1. Selecting a specified piece or pieces of data.
  2. Inspecting the values of this data.
  3. Doing something with the data. For example, the application might display the company name and calculate the annual total sales of different regions.
  4. The remaining exercises in this section show you how to do all this, one step at time. The next exercise shows how to Reference Data Values—that is, how to select specific data values out of the input XML document.