xsl-list
[Top] [All Lists]

Re: [xsl] Create xml file with recursive childnodes

2008-08-10 12:41:23
Here is a slightly better solution.

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

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

<xsl:template match="Objs">
  <xsl:variable name="start" select="obj[not(@name = ../obj/@child)]" />
  <Obj name="{$start/@name}">
    <xsl:choose>
      <xsl:when test="not(obj[(_at_)name = $start/@child])">
        <Obj name="{$start/@child}" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="performGrouping">
          <xsl:with-param name="list" select="obj[(_at_)name = $start/@child]" 
/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </Obj>
</xsl:template>

<xsl:template name="performGrouping">
  <xsl:param name="list" />

  <xsl:for-each-group select="$list" group-by="@name">
    <Obj name="{current-grouping-key()}">
      <xsl:for-each select="current-group()">
        <xsl:variable name="child" select="@child" />
        <xsl:choose>
          <xsl:when test="not(../obj[(_at_)name = $child])">
            <Obj name="{$child}" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="performGrouping">
        <xsl:with-param name="list" select="../obj[(_at_)name = $child]" />
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </Obj>
  </xsl:for-each-group>
</xsl:template>

</xsl:stylesheet>

The stylesheet I earlier posted did execution in two pass. This one
does the whole processing in a single pass.

On Sat, Aug 9, 2008 at 10:44 AM, Mukul Gandhi 
<gandhi(_dot_)mukul(_at_)gmail(_dot_)com> wrote:
The following stylesheet works ...

-- 
Regards,
Mukul Gandhi

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--