XML and SQL Server 2000
As a last coding illustration in this chapter, I want to present a real-world example. This example was taken from a corporate intranet site, and it generates a quarterly fiscal report for the company in an HTML tabular format. The table we want to create is shown in Figure 2.4. The XML data file and stylesheet are available for download at http://www.newriders.com. Figure 2.4. Quarterly fiscal report in HTML format.
Listing 2.28 shows the XML data file Channels.xml . Listing 2.28 Channels.xml Document
<CHANNELS> <CHANNEL TYPE="Aftermarket"> <AREA LOCATION="Americas"> <BOOKINGS> <DAY>1837183</DAY> <MTD>55839513</MTD> </BOOKINGS> <RETURNS> <DAY>-502987</DAY> <MTD>-4203783</MTD> </RETURNS> <BACKLOG> <GT_7DAYS>-4203783</GT_7DAYS> <LT_7DAYS>5622736</LT_7DAYS> </BACKLOG> <FUTURE_ORDERS> <CURRWK>2861866</CURRWK> <WK7-31-00>423176</WK7-31-00> <WK8-7-00>42549</WK8-7-00> <WK8-14-00>22950</WK8-14-00> <WK_GT_8-21-00>55515</WK_GT_8-21-00> <CURR_QTR>3406056</CURR_QTR> </FUTURE_ORDERS> </AREA> <AREA LOCATION="Asia/Pacific"> <BOOKINGS> <DAY>246073</DAY> <MTD>7743758</MTD> </BOOKINGS> <RETURNS> <DAY>-42723</DAY> <MTD>125894</MTD> </RETURNS> <BACKLOG> <GT_7DAYS>845567</GT_7DAYS> <LT_7DAYS>228438</LT_7DAYS> </BACKLOG> <FUTURE_ORDERS> <CURRWK>404541</CURRWK> <WK7-31-00>1394951</WK7-31-00> <WK8-7-00>1204360</WK8-7-00> <WK8-14-00>1096666</WK8-14-00> <WK_GT_8-21-00>2241186</WK_GT_8-21-00> <CURR_QTR>6336203</CURR_QTR> </FUTURE_ORDERS> </AREA> <AREA LOCATION="Europe"> <BOOKINGS> <DAY>3068642</DAY> <MTD>19753369</MTD> </BOOKINGS> <RETURNS> <DAY>-25060</DAY> <MTD>-1671616</MTD> </RETURNS> <BACKLOG> <GT_7DAYS>343785</GT_7DAYS> <LT_7DAYS>2138534</LT_7DAYS> </BACKLOG> <FUTURE_ORDERS> <CURRWK>364145</CURRWK> <WK7-31-00>1014554</WK7-31-00> <WK8-7-00>563798</WK8-7-00> <WK8-14-00>1365700</WK8-14-00> <WK_GT_8-21-00>3464202</WK_GT_8-21-00> <CURR_QTR>6683236</CURR_QTR> </FUTURE_ORDERS> </AREA> <AREA LOCATION="World Wide"> <BOOKINGS> <DAY>5151898</DAY> <MTD>83336640</MTD> </BOOKINGS> <RETURNS> <DAY>-570771</DAY> <MTD>-6001293</MTD> </RETURNS> <BACKLOG> <GT_7DAYS>8442583</GT_7DAYS> <LT_7DAYS>7989708</LT_7DAYS> </BACKLOG> <FUTURE_ORDERS> <CURRWK>3630552</CURRWK> <WK7-31-00>2832681</WK7-31-00> <WK8-7-00>1810706</WK8-7-00> <WK8-14-00>2485316</WK8-14-00> <WK_GT_8-21-00>5760903</WK_GT_8-21-00> <CURR_QTR>16425494</CURR_QTR> </FUTURE_ORDERS> </AREA> </CHANNEL> <CHANNEL TYPE="OEM"> <AREA LOCATION="Americas"> <BOOKINGS> <DAY>285052</DAY> <MTD>12129683</MTD> </BOOKINGS> <RETURNS> <DAY/> <MTD>-161641</MTD> </RETURNS> <BACKLOG> <GT_7DAYS>401291</GT_7DAYS> <LT_7DAYS>252617</LT_7DAYS> </BACKLOG> <FUTURE_ORDERS> <CURRWK>15400</CURRWK> <WK7-31-00>231960</WK7-31-00> <WK8-7-00>118668</WK8-7-00> <WK8-14-00/> <WK_GT_8-21-00>231960</WK_GT_8-21-00> <CURR_QTR>597988</CURR_QTR> </FUTURE_ORDERS> </AREA> <AREA LOCATION="Asia/Pacific"> <BOOKINGS> <DAY>24769</DAY> <MTD>2156458</MTD> </BOOKINGS> <RETURNS> <DAY/> <MTD>-147435</MTD> </RETURNS> <BACKLOG> <GT_7DAYS>771100</GT_7DAYS> <LT_7DAYS>240155</LT_7DAYS> </BACKLOG> <FUTURE_ORDERS> <CURRWK>270800</CURRWK> <WK7-31-00>278991</WK7-31-00> <WK8-7-00>406481</WK8-7-00> <WK8-14-00>668844</WK8-14-00> <WK_GT_8-21-00>653683</WK_GT_8-21-00> <CURR_QTR>2194810</CURR_QTR> </FUTURE_ORDERS> </AREA> <AREA LOCATION="Europe"> <BOOKINGS> <DAY>243435</DAY> <MTD>2238751</MTD> </BOOKINGS> <RETURNS> <DAY>-4718</DAY> <MTD>-29353</MTD> </RETURNS> <BACKLOG> <GT_7DAYS/> <LT_7DAYS/> </BACKLOG> <FUTURE_ORDERS> <CURRWK>154300</CURRWK> <WK7-31-00>165354</WK7-31-00> <WK8-7-00>63620</WK8-7-00> <WK8-14-00>104620</WK8-14-00> <WK_GT_8-21-00>185220</WK_GT_8-21-00> <CURR_QTR>673114</CURR_QTR> </FUTURE_ORDERS> </AREA> <AREA LOCATION="World Wide"> <BOOKINGS> <DAY>553256</DAY> <MTD>16524892</MTD> </BOOKINGS> <RETURNS> <DAY>-4718</DAY> <MTD>-338429</MTD> </RETURNS> <BACKLOG> <GT_7DAYS>1172391</GT_7DAYS> <LT_7DAYS>492772</LT_7DAYS> </BACKLOG> <FUTURE_ORDERS> <CURRWK>440500</CURRWK> <WK7-31-00>676305</WK7-31-00> <WK8-7-00>588769</WK8-7-00> <WK8-14-00>773464</WK8-14-00> <WK_GT_8-21-00>1070863</WK_GT_8-21-00> <CURR_QTR>3465912</CURR_QTR> </FUTURE_ORDERS> </AREA> </CHANNEL> </CHANNELS> Figure 2.4 shows the HTML table we want to end up with. XSLT Stylesheet
Listing 2.29 shows the XSLT stylesheet used to generate the table. This is a very long example, but it is much more attune to the real world. I've attempted to use as many XSLT elements as possible to give you good examples of their use. And remember one thing: There is no one way to write a stylesheet! Some ways are more efficient than others, but first get the correct answer. One thing in this stylesheet that I did not cover explicitly in the book is the use of variables. Just as I gave you some exercises in Chapter 1 to work out on your own, I purposely left variables out of the discussion to give you some practice in looking up definitions. I again refer you to Figure 2.4 for the table that this stylesheet generates. Listing 2.29 The Stylesheet That Generates the HTML Corporate Financial Report
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl='http://www.w3.org/XSL/Transform/1.0'> <xsl:output method="html" indent="yes"/> <xsl:decimal-format/> <!-- declares the default number format --> <xsl:template match="/"> <HTML> <BODY> <TABLE border='0' cellPadding='0' cellSpacing='0' width='100%'> <TR> <TD colspan='14' align='middle' bgColor='#ffffff' height='20'><B> <FONT color='black' face='Arial' size='3'>Bookings and Orders Report</FONT></B></TD> </TR> <TR> <TD colspan='14' align='middle' bgColor='#ffffff' height='20'><I><B> <FONT color='black' face='Arial' size='2'>Report as of - 2000-Jul-27</FONT></B></I></TD> </TR> </TABLE> <TABLE border='1' cellPadding='0' cellSpacing='0' width='100%'> <TR> <TD bgColor='#000000' height='18'> <xsl:text> </xsl:text> </TD> <TD bgColor='#000000'> <xsl:text> </xsl:text> </TD> <TD align='middle' bgColor='#000000' colSpan='2'><B> <FONT color='white' face='Arial' size='1'>Bookings</FONT> </B></TD> <TD align='middle' bgColor='#000000' colSpan='2'><B> <FONT color='white' face='Arial' size='1'>Returns</FONT> </B></TD> <TD align='middle' bgColor='#000000' colSpan='2'><B> <FONT color='white' face='Arial' size='1'>Backlog</FONT> </B></TD> <TD align='middle' bgColor='#000000' colSpan='6'><B> <FONT color='white' face='Arial' size='1'>Future Orders</FONT></B></TD> </TR> <TR> <TD align='right' bgColor='#000000' height='15'><B><FONT color='white' face='Arial' size='1'> Channel </FONT></B> </TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> Region</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> Day</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> MTD</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> Day</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> MTD</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> > 7 Days</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> 1-7 Days</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> Curr Wk</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> 7/31/00</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> 8/7/00</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> 8/14/00</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> >08/21/00</FONT></B></TD> <TD align='middle' bgColor='#d1d1d1'><B><FONT face='Arial' size='1'> Curr Qtr</FONT></B></TD> </TR> <xsl:apply-templates/> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="CHANNEL"> <TR> <TD align='right' bgColor='#000000' height='15'><B><FONT color='white' face='Arial' size='1'> <xsl:value-of select='@TYPE'/> </FONT></B> </TD> <xsl:apply-templates select='AREA'/> </TR> </xsl:template> <xsl:template match="AREA"> <xsl:choose> <xsl:when test="'Americas'=@LOCATION"> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> Americas </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="BOOKINGS/DAY"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="BOOKINGS/MTD"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="RETURNS/DAY"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="RETURNS/MTD"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="BACKLOG/GT_7DAYS"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="BACKLOG/LT_7DAYS"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right" bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/CURRWK"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/WK7-31-00"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/WK8-7-00"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/WK8-14-00"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/WK_GT_8-21-00"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/CURR_QTR"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> </xsl:when> <xsl:otherwise> <TR> <TD bgColor='#000000'></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="location" select="@LOCATION"/> <xsl:value-of select="@LOCATION"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'> <B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="BOOKINGS/DAY"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="BOOKINGS/MTD"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="RETURNS/DAY"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="RETURNS/MTD"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="BACKLOG/GT_7DAYS"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="BACKLOG/LT_7DAYS"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name='amount' select="FUTURE_ORDERS/CURRWK"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name='amount' select="FUTURE_ORDERS/CURRWK"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name='amount' select="FUTURE_ORDERS/WK8-7-00"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/WK8-14-00"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/WK_GT_8-21-00"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> <TD align='right' bgColor='#ffffff'><B><FONT face='Arial' size='1'> <xsl:variable name="amount" select="FUTURE_ORDERS/CURR_QTR"/> <xsl:value-of select="format-number($amount, '#,#00;(#,#00)')"/> </FONT></B></TD> </TR> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> These examples should give you plenty to study and should firm up a lot of what we've talked about in this chapter. Most importantly, have fun. |