home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / XML Utilities / Professional Programmer XSL IDE / Xselerator25.msi / Data.Cab / F23888_FixedLengthTextToXML.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2001-10-04  |  3.8 KB  |  113 lines

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- ===========================================================
  3.   Category:       NonXML
  4.   Sub-category:   FixedLengthText
  5.   Author:         David Silverlight
  6.                   HeadGeek@xmlpitstop.com
  7.   Created:        2001-05-16
  8.   Description:-
  9.     This stylsheet demonstrates transforming a fixed length text
  10.     file as XML.  In this example we are using substring
  11.     functions to  create an XML document from a text file based
  12.     on the start and end positions of the data.  Additionally,
  13.     we are calling a template named TrimTrailing to remove any
  14.     extra traling spaces from our document.
  15. ================================================================ -->
  16. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" ><xsl:output method="xml"/>
  17.  
  18.     <xsl:template match="/">
  19.     <xsl:for-each select="*">
  20.         <xsl:variable name="allContents" select="."/>
  21.  
  22.         <!--In this template, I load the file into a variable
  23.             so that it can be handled as a large string.  Using the string functions
  24.             like substring, it is easy to parse it given the startpositions and lengths
  25.             of the elements
  26.         -->
  27.         <xsl:element name="customers">    
  28.             <xsl:call-template name="SplitFixedLengthToXML">
  29.                 <xsl:with-param name="strInput" select="$allContents"/>
  30.                 <xsl:with-param name="lineLength" select="72"/>
  31.             </xsl:call-template>
  32.         </xsl:element>
  33.     </xsl:for-each>
  34.  
  35.     </xsl:template>
  36.  
  37.  
  38. <xsl:template name="SplitFixedLengthToXML">
  39.        <xsl:param name="strInput" select="''"/>
  40.     <xsl:param name="lineLength" select="120" />
  41.     <!-- This template recursively calls itself with the input string
  42.          each iteration process one line in the text file -->
  43.     <xsl:choose>
  44.      <xsl:when test="string-length($strInput) > $lineLength">    
  45.         <xsl:element name="customer">    
  46.  
  47.             <xsl:element name="CustomerID">
  48.                 <xsl:call-template name="TrimTrailing" >
  49.                     <xsl:with-param name="strInput" select="substring($strInput, 2, 10)"/>
  50.                 </xsl:call-template>
  51.             </xsl:element>        
  52.  
  53.             <xsl:element name="CustomerName">
  54.                 <xsl:call-template name="TrimTrailing" >
  55.                     <xsl:with-param name="strInput" select="substring($strInput, 12, 25)"/>
  56.                 </xsl:call-template>            
  57.             </xsl:element>
  58.  
  59.             <xsl:element name="Title">
  60.                 <xsl:call-template name="TrimTrailing" >
  61.                     <xsl:with-param name="strInput" select="substring($strInput, 37, 21)"/>
  62.                 </xsl:call-template>            
  63.             </xsl:element>
  64.             <xsl:element name="Phone">
  65.                 <xsl:call-template name="TrimTrailing" >
  66.                     <xsl:with-param name="strInput" select="substring($strInput, 58, 14)"/>
  67.                 </xsl:call-template>            
  68.             </xsl:element>
  69.                 </xsl:element>
  70.  
  71.             <xsl:call-template name="SplitFixedLengthToXML">
  72.             <!-- Here, I am calling the template recursively with the same string
  73.                  less one line -->
  74.             <xsl:with-param name="strInput" select="substring($strInput, $lineLength + 1)"/>
  75.             <xsl:with-param name="lineLength" select="$lineLength"/>
  76.         </xsl:call-template>
  77.      </xsl:when>
  78.      <xsl:otherwise>
  79.  
  80.     </xsl:otherwise>
  81. </xsl:choose>
  82. </xsl:template>
  83.  
  84.  
  85. <xsl:template name="TrimTrailing">
  86.     
  87.     <!--This template will recursively trim the trailing spaces from a string-->
  88.        <xsl:param name="strInput" select="''"/>
  89.     <xsl:variable name="strLen" select="string-length($strInput)" />
  90.     <xsl:variable name="strOutput" select="substring($strInput, 1, $strLen - 1 )" />
  91.     <xsl:variable name="strLastChar" select="substring($strInput, $strLen, 1 )" />
  92.  
  93.     <xsl:choose> 
  94.      <xsl:when test="$strLastChar = ' '">    
  95.  
  96.     
  97.         <xsl:call-template name="TrimTrailing" > 
  98.             <xsl:with-param name="strInput" select="$strOutput"/>
  99.         </xsl:call-template>
  100.  
  101.  
  102.     </xsl:when>
  103.      <xsl:otherwise>
  104.             <xsl:value-of select="$strInput" />
  105.     </xsl:otherwise>
  106.     </xsl:choose>
  107.  
  108.  
  109. </xsl:template>
  110.  
  111.  
  112. </xsl:stylesheet>
  113.