title: XSLT output JSON instead of XML
body: <p>I have a XSLT that works the way I want when it outputs XML, however I would like to change the XSLT it to output JSON instead. The problem appears to me with the inner most for-each loop, but I'm not sure. If there is way to make this more efficient I'm also interested in your suggestions. </p>
  
  <p>Sample input XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
     &lt;Metric measType="1526727075"
             measResult="0"
             endTime="2016-08-25T04:30:00-07:00"
             measObjLdn="LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7"
             Element_Type="ENODEB"
             Key1="LTHBC0126858"
             TableName="HH_ENODEB"
             ColumnName="H1526727075"
             H1526727075="0"/&gt;
     &lt;Metric measType="1526727076"
             measResult="0"
             endTime="2016-08-25T04:30:00-07:00"
             measObjLdn="LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7"
             Element_Type="ENODEB"
             Key1="LTHBC0126858"
             TableName="HH_ENODEB"
             ColumnName="H1526727076"
             H1526727076="0"/&gt;
     &lt;Metric measType="1526727077"
             measResult="0"
             endTime="2016-08-25T04:30:00-07:00"
             measObjLdn="LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7"
             Element_Type="ENODEB"
             Key1="LTHBC0126858"
             TableName="HH_ENODEB"
             ColumnName="H1526727077"
             H1526727077="0"/&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>This is the XSLT that outputs XML and works the way I expect</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"&gt;
      &lt;xsl:output method="xml" indent="yes"/&gt;
      &lt;xsl:strip-space elements="*"/&gt;
      &lt;xsl:template match="root"&gt;
          &lt;root&gt;
              &lt;xsl:for-each-group select="Metric" group-by="@measObjLdn"&gt;
                  &lt;xsl:sort select="current-grouping-key()"/&gt;
                  &lt;xsl:variable name="curr_key" select="current-grouping-key()"/&gt;
                  &lt;xsl:for-each-group select="current-group()" group-by="@TableName"&gt;
                      &lt;xsl:sort select="current-grouping-key()"/&gt;
                      &lt;xsl:if test="current-grouping-key() != ''"&gt;
                          &lt;Table TableName="{current-grouping-key()}"&gt;
                              &lt;xsl:for-each select="current-group()"&gt;
                                  &lt;xsl:attribute name="Stamp"&gt;
                                      &lt;xsl:value-of select="@endTime"/&gt;
                                  &lt;/xsl:attribute&gt;
                                  &lt;xsl:attribute name="measObjLdn"&gt;
                                      &lt;xsl:value-of select="$curr_key"/&gt;
                                  &lt;/xsl:attribute&gt;
                                  &lt;xsl:attribute name="Element_Type"&gt;
                                      &lt;xsl:value-of select="@Element_Type"/&gt;
                                  &lt;/xsl:attribute&gt;
                                  &lt;xsl:attribute name="Key1"&gt;
                                      &lt;xsl:value-of select="@Key1"/&gt;
                                  &lt;/xsl:attribute&gt;
                                  &lt;xsl:for-each select="@*"&gt;
                                      &lt;xsl:if test="starts-with(name(), 'H')"&gt;
                                          &lt;xsl:attribute name="{name()}"&gt;
                                              &lt;xsl:value-of select="number(.)"/&gt;
                                          &lt;/xsl:attribute&gt;
                                      &lt;/xsl:if&gt;
                                  &lt;/xsl:for-each&gt;
                              &lt;/xsl:for-each&gt;
                          &lt;/Table&gt;
                      &lt;/xsl:if&gt;
                  &lt;/xsl:for-each-group&gt;
              &lt;/xsl:for-each-group&gt;
          &lt;/root&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>This is my JSON output code, but not working as expected.</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"&gt;
      &lt;xsl:output method="text" encoding="utf-8"/&gt;
      &lt;xsl:strip-space elements="*"/&gt;
      &lt;xsl:template match="root"&gt;
          &lt;xsl:text&gt;{"root":{&lt;/xsl:text&gt;
          &lt;xsl:for-each-group select="Metric" group-by="@TableName"&gt;
              &lt;xsl:sort select="current-grouping-key()"/&gt;
              &lt;xsl:if test="current-grouping-key() != ''"&gt;
                  &lt;xsl:text&gt;"Table":[&lt;/xsl:text&gt;
                  &lt;xsl:text&gt;{"TableName":"&lt;/xsl:text&gt;
                  &lt;xsl:value-of select="current-grouping-key()"/&gt;
                  &lt;!--&lt;Table TableName="{current-grouping-key()}"&gt;--&gt;
                  &lt;xsl:text&gt;",&lt;/xsl:text&gt;
                  &lt;xsl:for-each-group select="current-group()" group-by="@measObjLdn"&gt;
                      &lt;xsl:sort select="current-grouping-key()"/&gt;
                      &lt;xsl:for-each select="current-group()"&gt;
                          &lt;xsl:text&gt;"Stamp":"&lt;/xsl:text&gt;
                          &lt;xsl:value-of select="@endTime"/&gt;
                          &lt;xsl:text&gt;",&lt;/xsl:text&gt;
                          &lt;xsl:text&gt;"measObjLdn":"&lt;/xsl:text&gt;
                          &lt;xsl:value-of select="current-grouping-key()"/&gt;
                          &lt;xsl:text&gt;",&lt;/xsl:text&gt;
                          &lt;xsl:text&gt;"Element_Type":"&lt;/xsl:text&gt;
                          &lt;xsl:value-of select="@Element_Type"/&gt;
                          &lt;xsl:text&gt;",&lt;/xsl:text&gt;
                          &lt;xsl:text&gt;"Key1":"&lt;/xsl:text&gt;
                          &lt;xsl:value-of select="@Key1"/&gt;
                          &lt;xsl:text&gt;",&lt;/xsl:text&gt;
                          &lt;xsl:for-each select="@attribute()"&gt;
                              &lt;xsl:if test="starts-with(name(), 'H')"&gt;
                                  &lt;xsl:text&gt;"&lt;/xsl:text&gt;
                                  &lt;xsl:value-of select="name()"/&gt;
                                  &lt;xsl:text&gt;":"&lt;/xsl:text&gt;
                                  &lt;xsl:value-of select="number(.)"/&gt;
                                  &lt;xsl:text&gt;"&lt;/xsl:text&gt;
                                  &lt;xsl:if test="position() != last()"&gt;
                                      &lt;xsl:text&gt;,&lt;/xsl:text&gt;
                                  &lt;/xsl:if&gt;
                              &lt;/xsl:if&gt;
                          &lt;/xsl:for-each&gt;
                      &lt;/xsl:for-each&gt;
                  &lt;/xsl:for-each-group&gt;
                  &lt;!--&lt;/Table&gt;--&gt;
                  &lt;xsl:text&gt;}&lt;/xsl:text&gt;
                  &lt;xsl:if test="position() != last()"&gt;
                      &lt;xsl:text&gt;,&lt;/xsl:text&gt;
                  &lt;/xsl:if&gt;
              &lt;/xsl:if&gt;
          &lt;/xsl:for-each-group&gt;
          &lt;xsl:text&gt;}&lt;/xsl:text&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Good point, so the XML version groups all the attributes for the same table together; Given the sample above the XML output is as follows</p>
  
  <pre><code>&lt;root xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;Table TableName="HH_ENODEB" Stamp="2016-08-25T04:30:00-07:00" measObjLdn="LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7" Element_Type="ENODEB" Key1="LTHBC0126858" H1526727075="0" H1526727076="0" H1526727077="0"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>JSON example output is not a JSON version of the XML version which I expected.</p>
  
  <pre><code>{"root":{"Table":[{"TableName":"HH_ENODEB","Stamp":"2016-08-25T04:30:00-07:00","measObjLdn":"LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7","Element_Type":"ENODEB","Key1":"LTHBC0126858","H1526727075":"0""Stamp":"2016-08-25T04:30:00-07:00","measObjLdn":"LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7","Element_Type":"ENODEB","Key1":"LTHBC0126858","H1526727076":"0""Stamp":"2016-08-25T04:30:00-07:00","measObjLdn":"LTHBC0126858/GTPU:Board Type=MPT, Cabinet No.=0, Subrack No.=1, Slot No.=7","Element_Type":"ENODEB","Key1":"LTHBC0126858","H1526727077":"0"}}
  </code></pre>