Search code examples
xmlxsltxslt-2.0xslt-grouping

How to do nesting grouping in xslt


Below is the xml

<Report>
    <ReportBody>
        <Level type="Assessment-Area" name="Etch">
            <Level type="Issue" Issue-Severity="Critical">
                <Level type="Issue" Issue-Severity="Critical">
                    <Level type="Issue" Issue-Severity="Critical">
                        <Level type="Checklist" name="CEDM - IPC Rules::High-Dense">
                            <Level type="Action" name="Padstack Analysis (High-Dense)" Checklist="CEDM - IPC Rules::High-Dense" Model="High-Dense">
                                <Level type="Category" name="abc" Range="*:via2toep;XX_w = 4.675 4.921 5.167 ">
                                    <Measurement LocationY="5.9547242" Layer="signal_1" Net1="$NONE$" name="CEDM - IPC Rules::High-Dense-11-signal_1-127--1-42" Component2="U56" Overview="some value" image="some_base 64 code">
                                    </Measurement>
                                </Level>
                            </Level>
                        </Level>
                    </Level>
                </Level>
            </Level>
            <Level type="Issue" Issue-Severity="Critical">
                <Level type="Issue" Issue-Severity="Critical">
                    <Level type="Issue" Issue-Severity="Critical">
                        <Level type="Checklist" name="CEDM - IPC Rules::High-Dense">
                            <Level type="Action" name="Padstack Analysis (High-Dense)" Checklist="CEDM - IPC Rules::High-Dense" Model="High-Dense">
                                <Level type="Category" name="xyz" Range="*:via2toep;XX_w = 4.675 4.921 5.167 ">
                                    <Measurement LocationY="5.9547242" Layer="signal_2" Net1="$NONE$" name="CEDM - IPC Rules::High-Dense-11-signal_1-127--1-42" Component2="U56" Overview="some value" image="some_base 64 code">
                                    </Measurement>
                                </Level>
                            </Level>
                        </Level>
                    </Level>
                </Level>
            </Level>
            <Level type="Issue" Issue-Severity="Critical">
                <Level type="Issue" Issue-Severity="Critical">
                    <Level type="Issue" Issue-Severity="Critical">
                        <Level type="Checklist" name="CEDM - IPC Rules::High-Dense">
                            <Level type="Action" name="Padstack Analysis (High-Dense)" Checklist="CEDM - IPC Rules::High-Dense" Model="High-Dense">
                                <Level type="Category" name="abc" Range="*:via2toep;XX_w = 4.675 4.921 5.167 ">
                                    <Measurement LocationY="5.9547242" Layer="signal_2" Net1="$NONE$" name="CEDM - IPC Rules::High-Dense-11-signal_1-127--1-42" Component2="U56" Overview="some value" image="some_base 64 code">
                                    </Measurement>
                                </Level>
                            </Level>
                        </Level>
                    </Level>
                </Level>
            </Level>
            <Level type="Issue" Issue-Severity="Critical">
                <Level type="Issue" Issue-Severity="Critical">
                    <Level type="Issue" Issue-Severity="Critical">
                        <Level type="Checklist" name="CEDM - IPC Rules::High-Dense">
                            <Level type="Action" name="Padstack Analysis (High-Dense)" Checklist="CEDM - IPC Rules::High-Dense" Model="High-Dense">
                                <Level type="Category" name="xyz" Range="*:via2toep;XX_w = 4.675 4.921 5.167 ">
                                    <Measurement LocationY="5.9547242" Layer="signal_2" Net1="$NONE$" name="i" Component2="U56" Overview="some value" image="some_base 64 code">
                                    </Measurement>
                                </Level>
                            </Level>
                        </Level>
                    </Level>
                </Level>
            </Level>
        </Level>
    </ReportBody>
</Report>

I am new in xslt . so i tried couple of method which i found on google. but its not working.

I want to group all the base 64 image which present in "Measurement" Tag then subgroup those images which have same "name" property inside the "Level" tag which present just above the "Measurement" Tag.


Solution

  • Your required output is not too clear, but here's an example where you would get the Measurements grouped by the name of their parent Level.

    Then I just copied the Measurement node, but you could choose to keep only your required attributes.

    <xsl:template match="/">
        <xsl:for-each-group select="//Measurement" group-by="../@name">
            <xsl:element name="Level">
                <xsl:attribute name="name" select="current-grouping-key()"/>
                <xsl:for-each select="current-group()">
                    <xsl:copy-of select="current()"/>
                </xsl:for-each>
            </xsl:element>
        </xsl:for-each-group>
    </xsl:template>