title: Transform an XML by grouping the fields
body: <p>My knowledge to XSLT is limited but always eager to learn. I am  currently working on a template that requires to transform the XML input. I've been trying to group the InvoiceNum fields and not getting anywhere. 
  I am getting an error: Envision.Utilities.XsltEngine-Object reference not set to an instance of an object. </p>
  
  <p>Here's the input XML for reference:</p>
  
  <pre><code>&lt;?xml version='1.0' ?&gt;
    &lt;Request&gt;
    &lt;Information&gt;
      &lt;ImageID&gt;987456321&lt;/ImageID&gt;
      &lt;Contract&gt;123456789&lt;/Contract&gt;
      &lt;Lastname&gt;MICKEYMOUSE&lt;/Lastname&gt;
    &lt;/Information&gt;
  &lt;Document&gt;
      &lt;InvoiceNum&gt;123456823&lt;/InvoiceNum&gt;
      &lt;Reference&gt;AD20985224&lt;/Reference&gt;
      &lt;InvoiceNum&gt;100000123&lt;/InvoiceNum&gt;
      &lt;Reference&gt;AS20101387&lt;/Reference&gt;
      &lt;InvoiceNum&gt;858511825&lt;/InvoiceNum&gt;
      &lt;Reference&gt;GF96844&lt;/Reference&gt;
      &lt;InvoiceNum&gt;885154145&lt;/InvoiceNum&gt;
      &lt;Reference&gt;FGFD2018&lt;/Reference&gt;
      &lt;InvoiceNum&gt;25241111&lt;/InvoiceNum&gt;
      &lt;Reference&gt;SD88888&lt;/Reference&gt;
      &lt;InvoiceNum&gt;8571414&lt;/InvoiceNum&gt;
      &lt;Reference&gt;DF864841254&lt;/Reference&gt;
      &lt;/Document&gt;
   &lt;/Request&gt;
  </code></pre>
  
  <p>Here's my XSLT format for reference:
  What am I missing? Is there better way to format the XSLT template I have currently below? Any help is greatly appreciated.</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
  &lt;xsl:strip-space elements="*"/&gt;
  &lt;xsl:param name="cols" select="3" /&gt;
  
  &lt;xsl:template match="Request"&gt;
   &lt;table border="1"&gt;
   &lt;xsl:apply-templates select="InvoiceNum[position() mod $cols = 1]"/&gt;
   &lt;/table&gt;
   &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Document"&gt;
  &lt;xsl:variable name="group" select=". | following-sibling::InvoiceNum
  [position() &amp;lt; $cols]" /&gt;
  &lt;xsl:for-each select="*"&gt;
  &lt;xsl:variable name="i" select="position()" /&gt;
  &lt;Invoice&gt;
  &lt;InvoiceNumber&gt;
  &lt;xsl:value-of select="InvoiceNum()"/&gt;
  &lt;/InvoiceNumber&gt;
   &lt;xsl:for-each select="$group"&gt;
   &lt;xsl:value-of select="*[$i]"/&gt;
   &lt;/xsl:for-each&gt;
   &lt;/Invoice&gt;
   &lt;/xsl:for-each&gt;
   &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Here's the XML output I'd like to have:</p>
  
  <pre><code>&lt;InvCase&gt;
    &lt;Invoices&gt;
      &lt;InvoicemNumber&gt;InvoiceNum1&lt;/InvoicemNumber&gt;
      &lt;InvoicemNumber&gt;InvoiceNum2&lt;/InvoicemNumber&gt;
      &lt;InvoicemNumber&gt;InvoiceNum3&lt;/InvoicemNumber&gt;
   &lt;/Invoices&gt;
  &lt;/InvCase&gt;
  </code></pre>