title: Fixed FLAT File to XML
body: <p>Need some help on the XSLT 2.0. </p>
  
  <p>Here is the request of a Fixed flat file that I get using unparsed text and a schema file having offset and length information using a document reference file. I am get all the details but when I start to do the grouping I am not able go further...Any help is greatly appreciated to move my stuck mind.</p>
  
  <h2>Source Flat File</h2>
  
  <pre><code>P01020 230039....
  A30438 009090.....
  A30439 009039.....
  P02390 039438....
  D03049 304830...
  A30493 304030...
  P30439 230300....
  </code></pre>
  
  <h2>Schema File:</h2>
  
  <pre><code>    &lt;Schema&gt;
  &lt;Purchase recordIdentifer="P"&gt;
      &lt;FIELD1 length="5" offset="1" type="xs:string"/&gt;
      &lt;FIELD2 length="6" offset="8" type="xs:string"/&gt;
  &lt;/Purchase&gt;
  &lt;Account recordIdentifer="A"&gt;
      &lt;FIELD1 length="5" offset="1" type="xs:string"/&gt;
      &lt;FIELD2 length="6" offset="8" type="xs:string"/&gt;
  &lt;/Account&gt;
  &lt;Deposit recordIdentifer="D"&gt;
      &lt;FIELD1 length="5" offset="1" type="xs:string"/&gt;
      &lt;FIELD2 length="6" offset="8" type="xs:string"/&gt;
  &lt;/Deposit&gt;
  </code></pre>
  
  <h2>XSLT Code</h2>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"&gt;
      &lt;xsl:param name="text-url" select="'test2014032901.txt'"/&gt;
       &lt;xsl:param name="file1" select="document('Schema.xml')"/&gt;
      &lt;xsl:output indent="yes"/&gt;
      &lt;xsl:template name="main"&gt;
          &lt;xsl:variable name="text" select="unparsed-text($text-url)"/&gt;
          &lt;xsl:variable name="lines" as="element(line)*"&gt;
              &lt;xsl:for-each select="tokenize($text,'\r?\n')[normalize-space()]"&gt;
                  &lt;line&gt;
                      &lt;xsl:value-of select="."/&gt;
                  &lt;/line&gt;
              &lt;/xsl:for-each&gt;
          &lt;/xsl:variable&gt;
          &lt;Root&gt;
              &lt;xsl:for-each-group select="$lines" group-starting-with="line[starts-with(., 'P')]"&gt;
              &lt;Purchases&gt;
                   &lt;Purchase&gt;
                              &lt;xsl:for-each select="$file1/Schema/Purchase"&gt;
                              &lt;xsl:for-each select="./*"&gt;
                                  &lt;xsl:element name="{name(.)}"&gt;
                                      &lt;xsl:value-of select="substring($lines, ./@offset,./@length)"/&gt;
                                  &lt;/xsl:element&gt;
                              &lt;/xsl:for-each&gt;
                          &lt;/xsl:for-each&gt;
                 &lt;/Purchase&gt;
              &lt;/Purchases&gt;
              &lt;/xsl:for-each-group&gt;
              &lt;xsl:for-each-group select="$lines" group-starting-with="line[starts-with(., 'A')]"&gt;
              &lt;Accounts&gt;
                   &lt;Account&gt;
                              &lt;xsl:for-each select="$file1/Schema/Account"&gt;
                              &lt;xsl:for-each select="./*"&gt;
                                  &lt;xsl:element name="{name(.)}"&gt;
                                      &lt;xsl:value-of select="substring($lines, ./@offset,./@length)"/&gt;
                                  &lt;/xsl:element&gt;
                              &lt;/xsl:for-each&gt;
                          &lt;/xsl:for-each&gt;
                 &lt;/Account&gt;
              &lt;/Accounts&gt;
              &lt;/xsl:for-each-group&gt;
              &lt;xsl:for-each-group select="$lines" group-starting-with="line[starts-with(., 'D')]"&gt;
              &lt;Deposits&gt;
                   &lt;Deposit&gt;
                              &lt;xsl:for-each select="$file1/Schema/Deposit"&gt;
                              &lt;xsl:for-each select="./*"&gt;
                                  &lt;xsl:element name="{name(.)}"&gt;
                                      &lt;xsl:value-of select="substring($lines, ./@offset,./@length)"/&gt;
                                  &lt;/xsl:element&gt;
                              &lt;/xsl:for-each&gt;
                          &lt;/xsl:for-each&gt;
                 &lt;/Deposit&gt;
              &lt;/Deposits&gt;
              &lt;/xsl:for-each-group&gt;
          &lt;/Root&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <h2>Output XML is something as below</h2>
  
  <pre><code>&lt;Root&gt;
      &lt;Purchases&gt;
          &lt;Purchase&gt;
              &lt;Field1&gt;01020&lt;/Field1&gt;
              &lt;Field2&gt;230039&lt;/Field2&gt;
          &lt;/Purchase&gt;
          &lt;Purchase&gt;
              &lt;Field1&gt;02390&lt;/Field1&gt;
              &lt;Field2&gt;039438&lt;/Field2&gt;
          &lt;/Purchase&gt;
          &lt;Purchase&gt;
              &lt;Field1&gt;30439&lt;/Field1&gt;
              &lt;Field2&gt;230300&lt;/Field2&gt;
          &lt;/Purchase&gt;
      &lt;/Purchases&gt;
      &lt;Accounts&gt;
          &lt;Account&gt;
              &lt;Field1&gt;30438&lt;/Field1&gt;
              &lt;Field2&gt;009090&lt;/Field2&gt;
          &lt;/Account&gt;
          &lt;Account&gt;
              &lt;Field1&gt;01020&lt;/Field1&gt;
              &lt;Field2&gt;230039&lt;/Field2&gt;
          &lt;/Account&gt;
          &lt;Account&gt;
              &lt;Field1&gt;01020&lt;/Field1&gt;
              &lt;Field2&gt;230039&lt;/Field2&gt;
          &lt;/Account&gt;
      &lt;/Accounts&gt;
      &lt;Deposits&gt;
          &lt;Deposit&gt;
              &lt;Field1&gt;03049&lt;/Field1&gt;
              &lt;Field2&gt;304830&lt;/Field2&gt;
          &lt;/Deposit&gt;
      &lt;/Deposits&gt;
  &lt;/Root&gt;
  </code></pre>
  
  <p>Any help is greatly appreciated!!!.</p>
  
  <p>Thanks</p>