spacer
Yehuda Shiran January 5, 2002
Instantiating An Additional XML File
Tips: January 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

The XSL format allows you to add an external XML file and insert it wherever you want in the original XML file. The external XML file should begin with the <document> tag and end with the </document> tag. Here is an example (020105b.xml):

<document>
    <summary>
      <heading>MyDVD Rental Store</heading>
      <subhead>Periodical Sales Report</subhead>
      <description>Sales Report for January, February, and March of 2001</description>
    </summary>
</document>
In the XSL file, you need to define the external XML file as a variable, like this:

<xsl:variable name="mydvd_header" select="document('020105b.xml')"/>
And then you place it somewhere, like here:

<H1><xsl:value-of select="$mydvd_header"/></H1>
The full XSL file is shown below. Let's assume that the original XML file is as follows (try it):

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="020104a.xsl"?>
  <sales>
	<data>
      <month>
        <name>January 2001</name>
        <week number="1" dvds_rented="12000" />
        <week number="2" dvds_rented="15000" />
        <week number="3" dvds_rented="18000" />
        <week number="4" dvds_rented="11800" />		  
      </month>
      <month>
        <name>February 2001</name>
        <week number="1" dvds_rented="11500" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="19050" />
        <week number="4" dvds_rented="11200" />		  
      </month>
      <month>
        <name>March 2001</name>
        <week number="1" dvds_rented="15300" />
        <week number="2" dvds_rented="12390" />
        <week number="3" dvds_rented="10050" />
        <week number="4" dvds_rented="11230" />		  
      </month>
    </data>
  </sales>
The outcome of displaying this file in a browser is a combination of the external file (020105b.xml) with the original XML file (020105a.xml):

Here is the XSLT file:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:param name="forecast" select="50000"/>
<xsl:variable name="mydvd_header" select="document('020105b.xml')"/>
<xsl:param name="weekly_quota" select="12000"/>
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE><xsl:value-of select="//summary/heading"/></TITLE>
</HEAD>
<BODY>
<H1><xsl:value-of select="$mydvd_header"/></H1>
<TABLE>
<TR>
<TH>Month\Week</TH>
<xsl:for-each select="//data/month[1]/week">
<TH>W<xsl:value-of select="@number"/></TH>
</xsl:for-each>
<TH>Total</TH>
<TH>Forecast</TH>
</TR>
<xsl:for-each select="//data/month">
  <tr>
    <th style="text-align:left"><xsl:value-of select="name"/></th>
    <xsl:for-each select="week">
       <td>
         <xsl:attribute name="style">
           <xsl:choose>
             <xsl:when test="number(@dvds_rented <= $weekly_quota)">color:red;</xsl:when>
             <xsl:otherwise>color:green;</xsl:otherwise>
           </xsl:choose>
           text-align:right;
         </xsl:attribute>
         <xsl:value-of select="format-number(@dvds_rented, '###,###')"/> 
      </td>
    </xsl:for-each>
    <td style="text-align:right;font-weight:bold">
      <xsl:value-of select="format-number(sum(week/@dvds_rented), '###,###')"/>
    </td>
    <td style="text-align:right;font-weight:bold">
      <xsl:value-of select="format-number($forecast, '###,###')"/>
    </td>
  </tr>
</xsl:for-each>
</TABLE>    
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint