Sunfile
Sunfile

Reputation: 125

Muenchian grouping XSLT 1.0 multiple grouping

I've read similar posts, but can't figure out how to apply Muenchian grouping in XSLT 1.0 based on multiple columns.

I'm stuck with the worst XML-file there is, can't change the layout. This is a sample:

<DataSet>
    <Row>
        <Cells>
            <Cell>COMPANY-A</Cell>
            <Cell>VG-ALG-TAX</Cell>
            <Cell>2021000009</Cell>
            <Cell>F29888</Cell>
        </Cells>
    </Row>
    <Row>
        <Cells>
            <Cell>COMPANY-A</Cell>
            <Cell>VG-ALG-TAX</Cell>
            <Cell>2021000010</Cell>
            <Cell>F12350</Cell>
        </Cells>
    </Row>
    <Row>
        <Cells>
            <Cell>COMPANY-A</Cell>
            <Cell>VG-ALG-TAX</Cell>
            <Cell>2021000010</Cell>
            <Cell>F12135</Cell>
        </Cells>
    </Row>
    <Row>
        <Cells>
            <Cell>COMPANY-B</Cell>
            <Cell>VG-ALG-TAX</Cell>
            <Cell>2021000010</Cell>
            <Cell>F12350</Cell>
        </Cells>
    </Row>
</DataSet>

I want to use Muenchian grouping in XSLT1.0 to group by the first, second and third cell. The fourth cell needs to be linked to that key. Expected result:

<DataSet>
    <Invoice>
        <Key>
            <Company>COMPANY-A</Company>
            <Type>VG-ALG-TAX</Type>
            <Num>2021000009</Num>
        </Key>
        <Customers>
            <Customer>F29888</Customer>
        </Customers>
    </Invoice>
    <Invoice>
        <Key>
            <Company>COMPANY-A</Company>
            <Type>VG-ALG-TAX</Type>
            <Num>2021000010</Num>
        </Key>
        <Customers>
            <Customer>F12350</Customer>
            <Customer>F12135</Customer>
        </Customers>
    </Invoice>
    <Invoice>
        <Key>
            <Company>COMPANY-B</Company>
            <Type>VG-ALG-TAX</Type>
            <Num>2021000010</Num>
        </Key>
        <Customers>
            <Customer>F12350</Customer>
        </Customers>
    </Invoice>
</DataSet>

I've tried this, but there is no result:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:output method="xml" indent="yes"/>

<xsl:key name="document-by-number" match="GenericBrowseResponse/Select/Response/Selection/DataSet/Row" use="Cells/Cell[2]"></xsl:key>

<xsl:template match="GenericBrowseResponse/Select/Response/Selection/DataSet/Row">
    <Invoices>
        <xsl:apply-templates select="Cells[generate-id(.)=generate-id(key('document-by-number',Cells/Cell[2])[1])]"/>
    </Invoices>
</xsl:template>

<xsl:template match="Cells">
    <Invoice>
        <xsl:for-each select="key('document-by-number', Cell[2])">
            <Document><xsl:value-of select="Cell[3]"/></Document>
        </xsl:for-each> 
    </Invoice>
</xsl:template>

<xsl:template match="text()"></xsl:template>

</xsl:stylesheet>

Upvotes: 0

Views: 208

Answers (1)

Sunfile
Sunfile

Reputation: 125

Tried some possibilities with the way the key is defined, Solved the issue with the following code:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="xml" indent="yes"/>

<xsl:key name="k1" match="Cells" use="concat(Cell[1], '|', Cell[2], '|', Cell[3])" />
<!--<xsl:key name="k1" match="Cells" use="Cell[3]"></xsl:key>-->

<xsl:template match="/DataSet">
  <DataSet>
  <xsl:apply-templates select="Row/Cells[generate-id()=generate-id(key('k1',concat(Cell[1], '|', Cell[2], '|', Cell[3]))[1])]"/>
  </DataSet>
</xsl:template>

<xsl:template match="Row/Cells">
    <Invoice>
      <Key>
        <Company><xsl:value-of select="Cell[1]"/></Company>
        <Type><xsl:value-of select="Cell[2]"/></Type>
        <Num><xsl:value-of select="Cell[3]"/></Num>
      </Key>
      <Customer>
        <xsl:for-each select="key('k1', concat(Cell[1], '|', Cell[2], '|', Cell[3]))">
            <Customers><xsl:value-of select="Cell[4]"/></Customers>
        </xsl:for-each> 
      </Customer>
    </Invoice>
</xsl:template>

<xsl:template match="text()"></xsl:template>

</xsl:stylesheet>

Upvotes: 0

Related Questions