Share this Page URL

Chapter 16. Working with XML Data in Acc... > Presenting XML Data with Access - Pg. 581

Working with XML Data in Access 581 These are the opening and closing lines of the routine that populates the table in the HTML. The for-each statement tells the XSL to iterate through each record in the XML until it gets to the last record. The code will operate in exactly the same way for each record because the code between the opening and closing for-each statements is executed on each record. This is called a program loop, or just a loop. See Also For more information about using loops and other programming structures, see Chapter 5 "Using Visual Basic to Automate Your Database." You'll see one segment of code that looks similar to the following for each field in our XML table: <td> <xsl:value-of select="ExpenseID"/> </td> These are table definition statements, and they populate the table with XML data. This code will pull out the value of the ExpenseID field and place in the appropriate cell in the table. Note that the order and placement of each table definition statement for each field are important. If the statements are written out of order, we'll get the wrong value in the cells! The reason is that the code does not reference the column in the creation of the HTML. It relies on the structure to get the placement right. This type of table definition isn't the only kind you'll see in the XSL. You will also see a construct that looks like this: <td> <xsl:value- of select="fx:FormatFromXSL('AmountSpent', 'Currency', 'auto', '1033', 6)"/> </td> Notice that this construct contains more formatting information. To understand what's going on here, we have to refer to the XSD we looked at earlier. Remember that the XML data is raw, meaning that it has no properties associated with it. We relied on the XSD to apply a structure to the data. Because we want the presentation to reflect both the data and, in some cases, the structural elements, Access used the XSD when it created the XSL to bring out some of the important structural elements (in this case, the data type) of the XML when it creates the HTML. Here, the XSL is formatting the HTML for the AmountSpent field as currency; it will place monetary symbols and decimal points in the output. Instead of treating the field as text and writing out the symbols explicitly, it uses the data type gleaned from the XSD to do the work. The XSL then uses program code to interpret the currency property and write out the symbols in the HTML. Making Sense of the Extra Code in the XSL File If you look below the main body of the XSL, you'll notice a lot of code that looks like a program. This is script that Access produces to do specific formatting work in the XSL. Currency fields, for example, need to have a dollar symbol placed before the field value and a decimal point put in the correct place. This script helps accomplish that. Let's take another look at the final HTML by double-clicking on CampaignExpenses.htm again. You'll notice that the column headers are boldface and centered, you'll notice that the cells are right- justified, you'll notice that the AmountSpent field has a dollar sign before each value, and you'll notice that the entire XML data set is placed in a table that looks similar to an Access table. All this corresponds to the XSL instructions we just looked at. In this chapter, we've looked only at the XSL that Access creates. The great thing about using a stylesheet is that you can create your own XSL and transform the data in any way you want.