Stupid Tricks With XSLT
Round Trip transformations of GL Journal Entries in XSLT

Todd Boyle CPA
GLdialtone.com

Contents:    1. Overview: What is XSLT
  2. General Ledger journal entries formatted in XML
  3. Transformation of a hierarchic GL structure into a flat structure
  4. Transformation of flat GL structure into a hierarchic structure
  5. References & Tutorials

1. Overview:  What is XSLT

XSLT is a language for processing XML documents or data.   Here is what small businesses need to understand about XSLT:

  1. An XSLT transformation, once designed, can potentially run on many different ecommerce platforms.
  2. XSLT transformations enable the users of different ecommerce standards and XML formats to interoperate, conducting business in their native XML languages through "translators".
  3. XSLT transformations can be run on the client computer or other locations, thus redistributing processing loads away from central servers.
  4. The speed of transformations doesn't matter much if you're only tranforming a few transactions.  But large transformations such as your whole month's accounting data may be impractical.

2. General Ledger journal entries formatted in XML

Given the following example journal entries...

 General Ledger Journal Listing   example                 July 2000
   
 JournalNo.  Date    LineNo.   Account                       Amount

   8880   22/7/2000    101     10100  Cash in Bank        Cr  -3000
   8880   22/7/2000    102     51200  Rent-Shop           Dr   2000
   8880   22/7/2000    103     51300  Rent-Office         Dr   1000

   8882   23/7/2000    107     10100  Cash in Bank        Cr  -2000
   8882   23/7/2000    108     55200  Maintenance-Truck   Dr   1500
   8882   23/7/2000    109     55700  Maintenance-Auto    Dr    500

   8881   24/7/2000    104     12100  Accts Receivable    Dr   6500
   8881   24/7/2000    105     41100  Commissions income  Cr  -6500

 Total Debits                                                 11500
 Total Credits                                                11500


Within a database or an XML file, Journal Entries (JEs) can always be represented as a single table, i.e. having one row for each debit or credit as you see above.  Alternatively, JEs can be represented in two tables (for example headers and lines).   The above report could be reformatted showing the journal numbers 8880, 8882,  etc. and the transaction dates on one line, and the line numbers 101,102, 103, etc. below.  That is what we are going to do to the data file in this example below.

It is possible to convert any given XML schema having a flatfile structure into a "normallized" structure having headers and rows with an XSLT transformation, and vice versa. 

The significance of these demo XSLT transformations is that they demonstrate it isn't critically important which shape is chosen for an XML Schema standard, for transmitting General Ledger data between different accounting systems, because they can be reformatted by generic XSLT transformations.  (It is critically important, of course that there is some agreement on the names of the data elements within the XML and XSLT files!)

3. Transformation of a hierarchic GL structure into a flat structure

Contents of 1st-Hierarchic2Flat zipfile 

Hierarchic1.xml
Hie2Flat.xsl
Flatfile1.xml
Run1.bat

The file "Hierarchic1.xml" contains three general ledger journal entries, each having a header and two or more rows. i.e. the JEs are structured as hierarchies.

The file "Hie2Flat.xsl" is a generic XSL stylesheet that illustrates how a hierarchic file like a journal entry can be transformed into a flat structure, i.e., it converts the three GL JE's into eight rows, representing all of the debits and credits.

The XML file that results from the XSLT transformation is also included ("Flatfile1.xml"). However the intention of this tutorial is that you will use your own XSLT parser, such as XT or the XSLT-Helper HTML file, to execute the transformation yourself, and view the result. If you save the resulting file it should be the same as the "Flatfile.xml"

Run1.bat is a batchfile which executes the transformation using the command line, using James Taubers' "XT" utility, 

Command syntax:

XT (source XML) (XSL sheet) (result XML)
XT Hierarchic1.xml Hie2Flat.xsl flatfile1.xml


See http://www.oasis-open.org/cover/xslSoftware.html#xt

Hierarchic XML source file

<?xml version="1.0"?>
<JEsWithHeaders>
   <JE>
    <Header>
      <JEID>8880</JEID>
      <JEDate>22/7/2000</JEDate>
    </Header>
    <Rows>
      <JELine>
         <LineID>101</LineID>
         <Account>10100</Account>
         <Amount>-3000</Amount>
      </JELine>
      <JELine>
         <LineID>102</LineID>
         <Account>51200</Account>
         <Amount>2000</Amount>
      </JELine>
      <JELine>
         <LineID>103</LineID>
         <Account>51300</Account>
         <Amount>1000</Amount>
      </JELine>
    </Rows>
   </JE>
   <JE>
    <Header>
      <JEID>8882</JEID>
      <JEDate>23/7/2000</JEDate>
    </Header>
    <Rows>
      <JELine>
         <LineID>107</LineID>
         <Account>10100</Account>
         <Amount>-2000</Amount>
      </JELine>
      <JELine>
         <LineID>108</LineID>
         <Account>55200</Account>
         <Amount>1500</Amount>
      </JELine>
      <JELine>
         <LineID>109</LineID>
         <Account>55700</Account>
         <Amount>500</Amount>
      </JELine>
    </Rows>
   </JE>
   <JE>
    <Header>
      <JEID>8881</JEID>
      <JEDate>24/7/2000</JEDate>
    </Header>
    <Rows>
      <JELine>
         <LineID>104</LineID>
         <Account>12100</Account>
         <Amount>6500</Amount>
      </JELine>
      <JELine>
         <LineID>105</LineID>
         <Account>41100</Account>
         <Amount>-6500</Amount>
      </JELine>
    </Rows>
   </JE>
</JEsWithHeaders>


XSLT conversion into Flat

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="/">
  <JEsAsFlatfile>
      <xsl:apply-templates match="." />
  </JEsAsFlatfile>
</xsl:template>

<xsl:template match="JE">

   <xsl:for-each select="descendant::JELine">
     <JELine>
     <xsl:for-each select="ancestor::JE">

        <JEID> <xsl:value-of select="./Header/JEID[current()]" /> </JEID>
        <JEDate> <xsl:value-of select="./Header/JEDate[current()]" /> </JEDate>
     </xsl:for-each>
     <LineID> <xsl:value-of select="./LineID"/> </LineID>
     <Account> <xsl:value-of select="./Account"/> </Account>
     <Amount> <xsl:value-of select="./Amount"/> </Amount>
     </JELine>
   </xsl:for-each>
</xsl:template>

</xsl:stylesheet>


4. Transformation of flat GL structure into a hierarchic structure

Contents of 2nd-Flat2Hierarchic zipfile:

Flatfile1.xml 
Flat2hier.xsl 
Hierarchic2.xml

The file "Flatfile1.xml" contains three general ledger journal entries, each having two or more rows. Each row of the general ledger listing contains a JEID identifying what journal entry it is associated with, and the JE Date.

The file "Flat2hier.xsl" is a generic XSL stylesheet that illustrates how a journal entry which is formatted in a flat structure can be transformed into a hierarchy consisting of a header, and child rows belonging to the header, i.e., it converts the eight Journal Entry rows representing all of the debits and credits into three GL JE's.

The XML file that results from the XSLT transformation is also included ("Hierarchic2.xml"). However the intention of this tutorial is that you will use your own XSLT parser, such as James Taubers' XT utility, to execute the transformation, and view the result in your own text editor. The resulting file should be the same as the enclosed "Hierarchic2.xml"

Run2.bat is a batchfile which executes the transformation using the command line, using James Taubers' "XT" utility, 

Command syntax:

XT (source XML) (XSL sheet) (result XML)
XT flatfile1.xml Flat2hier.xsl Hierarchic2.xml


See http://www.oasis-open.org/cover/xslSoftware.html#xt

Flat XML source file

<?xml version="1.0" encoding="utf-8"?>
<JEsAsFlatfile>
<JELine><JEID>8880</JEID><JEDate>22/7/2000</JEDate><LineID>101</LineID><Account>10100</Account><Amount>-3000</Amount></JELine>
<JELine><JEID>8880</JEID><JEDate>22/7/2000</JEDate><LineID>102</LineID><Account>51200</Account><Amount>2000</Amount></JELine>
<JELine><JEID>8880</JEID><JEDate>22/7/2000</JEDate><LineID>103</LineID><Account>51300</Account><Amount>1000</Amount></JELine>
<JELine><JEID>8882</JEID><JEDate>23/7/2000</JEDate><LineID>107</LineID><Account>10100</Account><Amount>-2000</Amount></JELine>
<JELine><JEID>8882</JEID><JEDate>23/7/2000</JEDate><LineID>108</LineID><Account>55200</Account><Amount>1500</Amount></JELine>
<JELine><JEID>8882</JEID><JEDate>23/7/2000</JEDate><LineID>109</LineID><Account>55700</Account><Amount>500</Amount></JELine>
<JELine><JEID>8881</JEID><JEDate>24/7/2000</JEDate><LineID>104</LineID><Account>12100</Account><Amount>6500</Amount></JELine>
<JELine><JEID>8881</JEID><JEDate>24/7/2000</JEDate><LineID>105</LineID><Account>41100</Account><Amount>-6500</Amount></JELine>
</JEsAsFlatfile>


XSLT conversion into Hierarchy

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
 <JEsWithHeaders>

   <xsl:for-each select="//JEID[not(. = following::JEID)]">
     <JE>
     <Header>
     <JEID><xsl:value-of select="."/></JEID>
     <JEDate><xsl:value-of select="following-sibling::JEDate"/></JEDate>
     <JEmorestuff><xsl:value-of select="following-sibling::JEmorestuff"/></JEmorestuff>
     </Header>      
     <Rows>
     <xsl:for-each select="//JELine[JEID=current()]">
       <xsl:sort select="LineID"/>
       <JELine>
       <LineID><xsl:value-of select="LineID"/></LineID>
       <Account><xsl:value-of select="Account"/></Account>
       <Amount><xsl:value-of select="Amount"/></Amount>
       </JELine>
     </xsl:for-each>
     </Rows>
     </JE>
      
   </xsl:for-each>
 </JEsWithHeaders>
</xsl:template>

</xsl:stylesheet>


5. References & Tutorials

Here are some references and tutorials. It is unrealistic to try learning XSLT without printing some of these on paper:

1. W3C XSLT http://www.w3.org/TR/xslt
2. W3C XPath language http://www.w3.org/TR/xpath
3. Chapter 14 of the XML Bible: XSL Transformations
        http://metalab.unc.edu/xml/books/bible/updates/14.html
4. Grosso & Walsh tutorial http://www.nwalsh.com/docs/tutorials/xsl/xsl/slides.html
5. XSLT Alleviates. http://msdn.microsoft.com/msdnmag/issues/0800/XSLT/XSLT.asp
6. XSLT.com  http://www.xslt.com