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:

   <preparedby>Thomson Accountants, LLC</preparedby>
   <accountantaddress>300 5th Ave., New York, NY 10003, USA</accountantaddress>
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:
  <xsl:value-of select="$accountantinfo//preparedby"/><BR></BR>
  <xsl:value-of select="$accountantinfo//accountantaddress"/>
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"?>
      <heading>MyDVD Rental Store</heading>
      <subhead>Periodical Sales Report</subhead>
      <description>Sales Report for January, February, and March of 2001</description>
        <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" />		  
        <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" />		  
        <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" />		  
And here is the complete XSL 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="accountantinfo" select="document('020106b.xml')"/>
<xsl:param name="weekly_quota" select="12000"/>
<xsl:template match="/">
<TITLE><xsl:value-of select="//summary/heading"/></TITLE>
<H1><xsl:value-of select="//summary/heading"/></H1>
<H2><xsl:value-of select="//summary/subhead"/></H2>
<P><xsl:value-of select="//summary/description"/></P>
<xsl:for-each select="//data/month[1]/week">
<TH>W<xsl:value-of select="@number"/></TH>
<xsl:for-each select="//data/month">
    <th style="text-align:left"><xsl:value-of select="name"/></th>
    <xsl:for-each select="week">
         <xsl:attribute name="style">
             <xsl:when test="number(@dvds_rented <= $weekly_quota)">color:red;</xsl:when>
         <xsl:value-of select="format-number(@dvds_rented, '###,###')"/> 
    <td style="text-align:right;font-weight:bold">
      <xsl:value-of select="format-number(sum(week/@dvds_rented), '###,###')"/>
    <td style="text-align:right;font-weight:bold">
      <xsl:value-of select="format-number($forecast, '###,###')"/>
  <xsl:value-of select="$accountantinfo//preparedby"/><BR></BR>
  <xsl:value-of select="$accountantinfo//accountantaddress"/>