Re: [xsl] Group Techniques by xslt

Subject: Re: [xsl] Group Techniques by xslt
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Wed, 02 Dec 2009 19:10:31 +0100
Joga Singh Rawat wrote:

If someone have a logic to structure a data of unstructred file, please
help. I am clueless. I am new in xslt 2.0.

Here is a partial solution:

  exclude-result-prefixes="xsd mf">

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

  <xsl:function name="mf:group" as="node()*">
    <xsl:param name="elements" as="element()*"/>
    <xsl:param name="level" as="xsd:integer"/>

<xsl:for-each-group select="$elements" group-starting-with="p[@class eq concat('Head', $level)]">
<xsl:element name="sc{$level}">

        <xsl:variable name="current-head" as="element()?"
          select="self::p[@class eq concat('Head', $level)]"/>

<xsl:apply-templates select="$current-head"/>

<xsl:variable name="max-level"
select="max(for $h in current-group()[self::p[matches(@class, '^Head[0-9]+$')]]
return xsd:integer(substring($h/@class, 5)))"/>

<xsl:variable name="next-head" as="element()?"
select="current-group()[self::p[matches(@class, '^Head[0-9]+$')]][1]"/>

<xsl:variable name="v1" as="element()*"
select="current-group()[. &lt;&lt; $next-head] except $current-head"/>

<xsl:apply-templates select="$v1"/>

<xsl:when test="$level lt $max-level">
<xsl:sequence select="mf:group(current-group() except ($current-head, $v1), $level + 1)"/>
<xsl:apply-templates select="current-group() except $current-head"/>

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

<xsl:template match="div[@class='bdy']">
<xsl:variable name="v1" as="element()*"
select="*[. &lt;&lt; (current()/p[@class eq 'Head1' or @class eq 'Head2'])[1]]"/>
<xsl:apply-templates select="$v1"/>
<xsl:sequence select="mf:group(* except $v1, 1)"/>

  <xsl:template match="p[matches(@class, '^Head[0-9]+$')]">

  <xsl:template match="p[@class eq 'Para_FL']">
    <p t="f1">


It does not quite output what you want as you seem to want to group initially with group-starting-with="p[@class='Head1']|p[@class='Head2'] while the stylesheet above uses a single function that groups level by level (i.e. 'Head1', then 'Head2', then 'Head3' and so on). I am not sure there is an elegant way to fix that, other than implementing a second function for the special case of the initial grouping you want.


	Martin Honnen

Current Thread