Figure

This XSL stylesheet will be used to format the raw XML data shown in Figure 17-11.

The XSL document contains many HTML formatting instructions. (Take a look at Figure 17-13 and notice the following tags: <HEAD>, <TITLE>, <BODY>, <CENTER>, <STRONG>, <TABLE>, <TR>, <TD>, <H6>, <HR>, and <FONT>.) The example stylesheet also uses two special XSL formatting instructions. The first one:

<xsl:for-each select="Courses/Course"/>

tells the XSL processor to loop through all the <Course> elements within the <Courses> root element. The first part of this tag, xsl:for-each, tells the XSL processor to do something every time it finds a pattern, while the second part of the tag, select="Courses/Course", specifies the pattern for which to look. Notice that you must close each loop with a closing loop tag:

The stylesheet can also sort the information by using the order-by attribute on the xsl:for-each element, as shown below:

<xsl:for-each select="Courses/Course" order-by="Title"/>

The second type of special formatting instructions uses the <xsl:value-of> tags to tell the XSL processor to look inside the <Course> element and retrieve the value of the tag specified in the select attribute:

<TD><xsl:value-of select="@ID"/></TD> <TD><xsl:value-of select="Title"/></TD> <TD><xsl:value-of select="Startdate"/></TD> <TD><xsl:value-of select="Sessions"/></TD>

Because the above block of code is located below the <xsl:for-each> tag, the XSL processor will retrieve the specified values for each <Course> element.

The Courses.xsl stylesheet contains formatting instructions that generate an HTML table and use the values of selected XML elements to fill the table with information. You can create as many stylesheets for your XML document as desired. Later in this chapter you will see other examples of XLS stylesheets and enhance your understanding of this subject.

Linking an XML Document to a Stylesheet

To establish a link between the XML and XSL files, you must include in your XML file a reference to the XSL file.

The following exercise demonstrates how to add a reference to a stylesheet in your XML document.

1. Open the Courses.xml document in Notepad.

2. Enter the following instruction below the XML declaration line:

<?xml-stylesheet type="text/xsl" href="Courses.xsl"?>

The modified XML document is shown in Figure 17-14.

3. Save your modified Courses.xml file.

4. Close Notepad.

Viewing XML Documents Formatted with Stylesheets

An XML document formatted with an XSL stylesheet can be viewed in a browser or in Microsoft Excel 2002.

The following exercise demonstrates how to view XML documents formatted with stylesheets.

1. In Windows Explorer, double-click the Courses.xml file. The file should open in the browser. Because this file is now linked to a stylesheet, instead of raw XML data you should see a nicely formatted table.

^ Courses.xml - Notepad

Fie Edt Formai Help

<?xmt-styiesheet type-'texttxsl" href=*Courses.xsr?>

«Courses*

«Title>Beginning VBA în Excel<>Title> «Sla rtdate >2/3/2 00 3«/Sia rtdate > «Se ss io ns > 6«/S ess i ons > «/C ourse >

"Course ID="VBA2EX">

«Tille>lntermecitale VBA In Excel«/TIUe> «Sta rtdate >4/3/2 00 3«fôta rtdate > «Se ss io ns » 8 «/S essi ons » «/Course>

«Course ID=UVBA3EX'>

«Tille3»Advanced VBA in Excel*JTitla> «sta rtdate >6/3/2 00 3«iSta rtdate > «Sesslons>12«/Sessions> «/Coursa > «/Courses >

Figure 17-14: Linking an XML document to an XSL stylesheet

Figure 17-15:

An XML document formatted with an XSL stylesheet opened in the browser

Figure 17-15:

An XML document formatted with an XSL stylesheet opened in the browser

2. Now let's open the Courses.xml file in Excel 2002. When you open an XML document in Excel that is linked to a stylesheet, Excel displays the Import XML dialog box (Figure 17-16) in which you can specify to either open the file without applying a stylesheet or apply a specific stylesheet. If you open the file without applying a stylesheet, the file will be opened, as described earlier in the section titled "XML Flattener."

Figure 17-16:

Excel allows you to open an XML file with or without a stylesheet if the XML file is linked to a stylesheet.

Figure 17-16:

Excel allows you to open an XML file with or without a stylesheet if the XML file is linked to a stylesheet.

^ Microsoft Excel - Courses.unit [Head-Only

BHEäj

Edit

View hrsefl Format tools

Data Wndow

Help

_ a *

Anal Unicode MS

• 10 ■ |B] j h :

3! % (s

A . I

OSHï

m El ^ A 39 IB -

- o

A Ï -Î

>1

lift IHK

-

Al

£ VEA Cguïss Scïieduîe

A

B

c

r

D

z

E

1 FT

1

vea .1.111' ; r mm' ir :-

^

3

Course Id

Course Title

Start Date

rño

of Sesaiei

ris

i

vba1ex

Beginning VBA in Excel

2Í3/2003

6

5

vba2eíí

intermediate vba in Excel

jí3/3003

a

G

VB-aSEX

Advanced VBA in Excel

<03/2003

12

8

A2-

Using Sample XSL Stjlc Sheet 'Courses.isl" piepaicd btp Julitta Koiol

À

H <

► HÀCoun

I -1

D,<m- Ii- mytSkipr,- \ \ OO.IÜ A 0 si Ö

A » = V -:

■ i ?

Readr

EXT

An XML document formatted with an XSL stylesheet opened in Microsoft Excel 2002

% Courses?, xst - Notepad HS B

File Edit Fwmat Help

<?xml version="l.tr?>

<HTM L xmln s ;xsl="http//toww, w3, org/TRAAJD-xs ln>

<HEAD>

<TITLE>VBA Course Schedule<flïTLE>

</HEAD>

*BODY>

<STR0NG>V8A Course Schedule</STRONG><P/>

<xsl :fo r- ea c h ss la ct=*Cou rs es/Co urse">

*DIV Style=" bac kg ro und-c ol or:yell ow. co lo r.wh ite, wl tfth: 30 0 pji"*

<SPAN Style-'font-weightibold; colorblue">

Txsl:vaIu8-of seleci^'Tille" l> (*xsl:yalus-of select="@ID7>)</SPAN>

</DIV>

<SPAN StyJe="fo nt-styl e: ¡tali c*>Sta rf Date" </SPAN>

:valu e-of s e 1 ect="Sta rtdate" !><B R/>

<SPAN Slyle="iortt-style:italrc*>No of Sessions: </SPAN>

*xsl:value-of select="Sessions" /><BRf=

*ixsl:for-eaeh>

<H6 align="lefr>

<FONT face="Tatiorna">

Using Sample XSL Style Stieet'Coursesi.xsl' prepared byJulitta Koro!

«/FONT» «AHB»

</BODY>

*/HTML>

You can apply more than one stylesheet to an XML document. Here's another stylesheet for the XML document shown in Figure 17-11.

VBA Colins - MI iiw i l Internet l luV l f

H

Fib Edit Vbw Favorites Tods Help ^

a

! Dad - iS äSratl [¿¡Fevaries j^Hiïlay ij Mà =J

Addess|s£] C:lJK_B0Dlffi^LL\£Kod_EN_2002\D^pter!NerACfetJlar_17,iCourss!.i!rrt jJ

»

Lrks ff] Cüstornize Linkî Free Hotrnal Liquid M lisic Nelr\çfk |

VBA Coarse Srliediile

-

Begiiiuing VBA in Excil (VB&IEX)

Sion Date: 2^2003 At of Sessions: 6

Hire line ilia re VBA ill Eire! (VBA2EX)

Start Date: 4W2003 No of Sessions: 3

AdVanMd VBA in Etc el (VBA33SQ

Start Date: 6W2QQI No of Sessions: 12

Ç] Done ¡3 Mji templier j

Figure 17-19:

This XML document was formatted with the XSL stylesheet shown in Figure 17-18.

Figure 17-19:

This XML document was formatted with the XSL stylesheet shown in Figure 17-18.

0 0

Post a comment