title: Creating a JSON from xml with date and time logic templates
body: <p>I'm trying to create a JSON from a XML which has messages and each message has its date/time.</p>
  
  <p>Below is the XML</p>
  
  <pre><code>  &lt;message&gt;
       &lt;messageText heading="Temporary Maintenance Message 1"&gt;test message1&lt;/messageText&gt;
  
       &lt;displayScheduleContainer&gt;
          &lt;startDate&gt;22/05/2019&lt;/startDate&gt;
          &lt;startTimeHrs&gt;12&lt;/startTimeHrs&gt;
          &lt;startTimeMins&gt;45&lt;/startTimeMins&gt;
          &lt;noEndDate&gt;true&lt;/noEndDate&gt;
       &lt;/displayScheduleContainer&gt;
    &lt;/message&gt;
  
     &lt;message&gt;
       &lt;messageText heading="Temporary Maintenance Message 1"&gt;test message2&lt;/messageText&gt;
  
       &lt;displayScheduleContainer&gt;
          &lt;startDate&gt;22/06/2019&lt;/startDate&gt;
          &lt;startTimeHrs&gt;12&lt;/startTimeHrs&gt;
          &lt;startTimeMins&gt;45&lt;/startTimeMins&gt;
          &lt;noEndDate&gt;true&lt;/noEndDate&gt;
       &lt;/displayScheduleContainer&gt;
    &lt;/message&gt;
  </code></pre>
  
  <p>The logic inside XSLT reads the date and time to activate the message</p>
  
  <pre><code>   &lt;xsl:for-each select="xalan:nodeset($messageData)/activeMessage/message"&gt;
  
      &lt;xsl:variable name="variableN"&gt;
          &lt;xsl:call-template name="jsonMsg" /&gt;
      &lt;/xsl:variable&gt;
  
  
      &lt;xsl:choose&gt;
          &lt;xsl:when test="$variableN = 'true'"&gt;
              &lt;xsl:copy-of select="messageText/text()" /&gt;
              &lt;xsl:if test="position() &amp;lt; last()"&gt;,&lt;/xsl:if&gt;
          &lt;/xsl:when&gt;
  
      &lt;/xsl:choose&gt;
  
  &lt;/xsl:for-each&gt;
  
  &lt;xsl:template name="jsonMsg"&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when test="displayScheduleContainer/noEndDate = 'true'"&gt;
              &lt;xsl:variable name="messageInDateTime"&gt;
                  &lt;xsl:call-template name="noEndDateTemplate"&gt;
                      &lt;xsl:with-param name="startDateTime"
                          select="concat(displayScheduleContainer/startDate, ' ', displayScheduleContainer/startTimeHrs, ':', displayScheduleContainer/startTimeMins)" /&gt;
  
                  &lt;/xsl:call-template&gt;
              &lt;/xsl:variable&gt;
              &lt;xsl:value-of select="$messageInDateTime" /&gt;
          &lt;/xsl:when&gt;
  
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template name="noEndDateTemplate"&gt;
      &lt;xsl:param name="startDateTime" /&gt;
  
      &lt;xsl:variable name="sdf"
          select="java:text.SimpleDateFormat.new('dd/MM/yyyy hh:mm')" /&gt;
      &lt;xsl:variable name="currentDateTime" select="java:util.Date.new()" /&gt;
      &lt;xsl:choose&gt;
          &lt;xsl:when
              test="java:compareTo(java:parse($sdf, $startDateTime), $currentDateTime) &amp;lt; 0"&gt;
              &lt;xsl:text&gt;true&lt;/xsl:text&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:text&gt;false&lt;/xsl:text&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  
  
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>The problem i'm facing here is if the last value is false, I end up getting the comma at the end. As i'm checking for the last position and adding the comma. Due to this the whole JSON is broken. In this case it adds the comma because i'm displaying the text only if it is true.</p>
  
  <pre><code> "message": ["test message1", ]  
  </code></pre>
  
  <p>I'm using XSLT 1.0</p>