January 6, 2002 - Adding a Signature to an XML File | WebReference

January 6, 2002 - Adding a Signature to an XML File

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

Yehuda Shiran, Ph.D.
Doc JavaScript

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. Suppose you want to add the accountant signature at the bottom of a financial table. Your accountant signature is needed in many other documents, so you keep it in a separate XML file:

<document>
   <preparedby>Thomson Accountants, LLC</preparedby>
   <accountantaddress>300 5th Ave., New York, NY 10003, USA</accountantaddress>
</document>
In the main XML file, you need to define a variable that will point to the new XML file:

<xsl:variable name="accountantinfo" select="document('020106b.xml')"/>
And at the end of the file, you need to include the new XML file:
<div>
  <xsl:value-of select="$accountantinfo//preparedby"/><BR></BR>
  <xsl:value-of select="$accountantinfo//accountantaddress"/>
</div>    
Notice how you can select particular data tags from the external XML file. Try it now. Here is the main XML file:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="020106a.xsl"?>
  <sales>
    <summary>
      <heading>MyDVD Rental Store</heading>
      <subhead>Periodical Sales Report</subhead>
      <description>Sales Report for January, February, and March of 2001</description>
    </summary>
	<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>
And here is the complete XSL file:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:param name="forecast" select="50000"/>
<xsl:variable name="accountantinfo" select="document('020106b.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="//summary/heading"/></H1>
<H2><xsl:value-of select="//summary/subhead"/></H2>
<P><xsl:value-of select="//summary/description"/></P>
<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>
<div>
  <xsl:value-of select="$accountantinfo//preparedby"/><BR></BR>
  <xsl:value-of select="$accountantinfo//accountantaddress"/>
</div>    
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>