Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Grouping within a group in XSLT 1.0

$
0
0

Hi,

I have the below XML, from which I need to generate multiple groups within groups.

My sample XML:

<XmlContent>
<SalesInvoices2>
<NewDataSet>
<Table>
  <F1>ID</F1>
  <F2>NumberOfEntries</F2>
  <F3>JournalID</F3>
  <F4>Description</F4>
  <F5>TransactionID</F5>
  <F6>SourceID</F6>
  <F7>RecordID</F7>
  <F8>AccountID</F8>
  <F9>CreditAmount</F9>
  </Table>
<Table>
  <F1>1</F1>
  <F2>4</F2>
  <F3>10</F3>
  <F4>53-IX-90 Volkwagen Golf 1</F4>
  <F5>1</F5>
  <F6>4</F6>
  <F7>1</F7>
  <F8>100</F8>
  <F9>1000.28</F9>
  </Table>
<Table>
  <F1>1</F1>
  <F2>4</F2>
  <F3>10</F3>
  <F4>53-IX-90 Volkwagen Golf 1</F4>
  <F5>1</F5>
  <F6>4</F6>
  <F7>2</F7>
  <F8>101</F8>
  <F9>1089.87</F9>
  </Table>
<Table>
  <F1>1</F1>
  <F2>4</F2>
  <F3>10</F3>
  <F4>53-IX-90 Volkwagen Golf 1</F4>
  <F5>1</F5>
  <F6>4</F6>
  <F7>3</F7>
  <F8>102</F8>
  <F9>1090.87</F9>
  </Table>
<Table>
  <F1>1</F1>
  <F2>4</F2>
  <F3>10</F3>
  <F4>53-IX-90 Volkwagen Golf 1</F4>
  <F5>2</F5>
  <F6>6</F6>
  <F7>4</F7>
  <F8>103</F8>
  <F9>1091.87</F9>
  </Table>
<Table>
  <F1>1</F1>
  <F2>4</F2>
  <F3>10</F3>
  <F4>53-IX-90 Volkwagen Golf 1</F4>
  <F5>2</F5>
  <F6>6</F6>
  <F7>5</F7>
  <F8>104</F8>
  <F9>1092.87</F9>
  </Table>
<Table>
  <F1>3</F1>
  <F2>4</F2>
  <F3>12</F3>
  <F4>53-IX-90 Volkwagen Golf 5</F4>
  <F5>3</F5>
  <F6>5</F6>
  <F7>6</F7>
  <F8>105</F8>
  <F9>1093.87</F9>
  </Table>
<Table>
  <F1>3</F1>
  <F2>4</F2>
  <F3>12</F3>
  <F4>53-IX-90 Volkwagen Golf 5</F4>
  <F5>3</F5>
  <F6>5</F6>
  <F7>7</F7>
  <F8>106</F8>
  <F9>1094.87</F9>
  </Table>
<Table>
  <F1>3</F1>
  <F2>4</F2>
  <F3>12</F3>
  <F4>53-IX-90 Volkwagen Golf 5</F4>
  <F5>3</F5>
  <F6>5</F6>
  <F7>8</F7>
  <F8>107</F8>
  <F9>1095.87</F9>
  </Table>
<Table>
  <F1>3</F1>
  <F2>4</F2>
  <F3>12</F3>
  <F4>53-IX-90 Volkwagen Golf 5</F4>
  <F5>3</F5>
  <F6>5</F6>
  <F7>9</F7>
  <F8>108</F8>
  <F9>1096.87</F9>
  </Table>
<Table>
  <F1>3</F1>
  <F2>4</F2>
  <F3>12</F3>
  <F4>53-IX-90 Volkwagen Golf 5</F4>
  <F5>4</F5>
  <F6>7</F6>
  <F7>10</F7>
  <F8>109</F8>
  <F9>1097.87</F9>
  </Table>
<Table>
  <F1>3</F1>
  <F2>4</F2>
  <F3>12</F3>
  <F4>53-IX-90 Volkwagen Golf 5</F4>
  <F5>4</F5>
  <F6>7</F6>
  <F7>11</F7>
  <F8>110</F8>
  <F9>1098.87</F9>
  </Table>
</NewDataSet>
  </SalesInvoices2>
  </XmlContent>

Expected Output:

<!-- Journal- Repeats for each unique set of journal ID (F3) -->

 <Journal>    
      <JournalId>
        <xsl:value-of select="F3"/>
      </JournalId>
      <Description>
        <xsl:value-of select="F4"/>
      </Description>
     
      <!-- Transaction - Repeats for each unique set of transaction ID (F5) within Journal-->
      <Transaction>
        <TransactionId>
          <xsl:value-of select="F5"/>
        </TransactionId>
        <SourceID>
          <xsl:value-of select="F6"/>
        </SourceID>
        <!-- Line - Repeats within each Transaction for each Record ID(F7)  -->
        <Line>
          <LineNumber>
            <xsl:value-of select="F6"/>
          </LineNumber>
          <OrderDate>
            <xsl:value-of select="F7"/>
          </OrderDate>
          <ProductCode>
            <xsl:value-of select="F8"/>
          </ProductCode>
          <ProductDescription>
            <xsl:value-of select="F9"/>
          </ProductDescription>
        </Line>
      </Transaction>
 </Journal>

I had tried the below XSLT - which is not working as expected, however with slight changes to the approach, I am able to get the one outer loop , i.e., Journal would get repeated twice, but not other elements within that.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:apply-templates select="XmlContent/SalesInvoices2"/>
  </xsl:template>
  <xsl:key name="EntryId" match="Table" use="F3" />
  <xsl:template match="NewDataSet">
    <NewDataSet>
      <xsl:apply-templates select="//Table[position()>1 and generate-id(.) = generate-id(key('EntryId', F3)[1])]" />
    </NewDataSet>
  </xsl:template>

  <xsl:template name="Table" match="Table">
    <NumberOfEntries>
        <xsl:value-of select="F2"/>
      </NumberOfEntries>
    <!-- Repeats - Journal -->
    <Journal>
      <JournalId>
        <xsl:value-of select="F3"/>
      </JournalId>
      <Description>
        <xsl:value-of select="F4"/>
      </Description>

      <!-- Repeats - Transaction -->
      <xsl:for-each select="key('EntryId', concat(F3,'|',F5[1]))">
        <Transaction>
          <TransactionId>
            <xsl:value-of select="F5"/>
          </TransactionId>
          <SourceID>
            <xsl:value-of select="F6"/>
          </SourceID>
          <!-- Repeats - Line -->
          <xsl:for-each select="key('EntryId', F3)">
            <Line>
              <LineNumber>
                <xsl:value-of select="F6"/>
              </LineNumber>
              <OrderDate>
                <xsl:value-of select="F7"/>
              </OrderDate>
              <ProductCode>
                <xsl:value-of select="F8"/>
              </ProductCode>
              <ProductDescription>
                <xsl:value-of select="F9"/>
              </ProductDescription>
            </Line>
       </xsl:for-each>
        </Transaction>
        </xsl:for-each>      
    </Journal>
   </xsl:template>
</xsl:stylesheet>

In the given sample - Journal should get repeated twice and within first Journal set of elements - I should get two transaction set of elements within the first set of transaction there should be 3 Line set of elements and in the second transaction set there should be 2 Line set of elements.

Could you please help me on the solution, how I can have multiple groups and loop though the group and create another group within already constructed groups.


Rpaul



Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>