DanielCSD
DanielCSD

Reputation: 73

How to nest multiple elements in xslt

I'm having the following scenario and i've never done a nesting transformation and i don't know how to start. I've have the following sample. In case the value are having two digits this became the ancestor for all of it's descendants.

Input:

<Items Title="Title" Icon="Icon" Description="Description">
    <Item Value="01" Name="Agriculture"/>
    <Item Value="011" Name="Horticulture and Fruit Growing"/>
    <Item Value="0111" Name="Plant Nurseries"/>
    <Item Value="011101" Name="Bulb Propagating"/>
    <Item Value="0112" Name="Cut Flower and Flower Seed Growing"/>
    <Item Value="011201" Name="Display Foliage Growing"/>

... ... the values are continuing

The desire output:

<Items Title="Title" Icon="Icon" Description="Description">
    <Item Name="Agriculture">
        <Item Value="011" Name="Horticulture and Fruit Growing">
            <Item Value="0111" Name="Plant Nurseries">
                <Item Value="011101" Name="Bulb Propagating" />
            </Item>
            <Item Value="0112" Name="Cut Flower and Flower Seed Growing">
                <Item Value="011201" Name="Display Foliage Growing" />
            </Item>
        </Item>
    </Item>

...

I've used the following xslt:

<xsl:template match="Items">
       <Items Title="{@Title}" Icon="{@Icon}" Description="{@Description}">

           <xsl:for-each select="Item">
               <xsl:if test="string-length(@Value) = 2">
                   <Item Name="{@Name}">
                       <xsl:for-each select="ancestor::Items/Item">
                           <xsl:if test="string-length(@Value) = 3">
                               <Item Value="{@Value}" Name="{@Name}">

                               </Item>
                           </xsl:if>
                       </xsl:for-each>
                   </Item>
               </xsl:if>
           </xsl:for-each> 

       </Items>
   </xsl:template>

But this is not working for the 4 and 6 level.

The result for now is:

<Items Title="Title" Icon="Icon" Description="Description">
<Item Name="Agriculture">
    <Item Value="011" Name="Horticulture and Fruit Growing"/>

...

Upvotes: 0

Views: 527

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167781

You can solve that using xsl:for-each-group group-starting-with, as you want to process several levels it is best to write a recursive function:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    exclude-result-prefixes="xs mf"
    version="2.0">

    <xsl:output indent="yes"/>

    <xsl:function name="mf:group" as="element(Item)*">
        <xsl:param name="items" as="element(Item)*"/>
        <xsl:param name="level" as="xs:integer"/>
        <xsl:for-each-group select="$items" group-starting-with="Item[string-length(@Value) eq $level]">
            <xsl:choose>
                <xsl:when test="self::Item[string-length(@Value) eq $level]">
                    <xsl:copy>
                        <xsl:copy-of select="@*"/>
                        <xsl:sequence select="mf:group(current-group() except ., min((current-group() except .)/string-length(@Value)))"/>
                    </xsl:copy>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="mf:group(current-group(), min(current-group()/@Value))"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:function>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Items">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:sequence select="mf:group(Item, 2)"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions