xsl-list
[Top] [All Lists]

Re: [xsl] Grouping lists from flat to nested

2011-01-31 09:47:20
Dear Martin,

this looks great, thank you!  - the only downside is that the input to
this grouping attempt is a variable containing the input. So I fear I
can't use <template match="text"> here. I think I would need a new
transformation document in the pipeline with this attempt - which I
would love to avoid. - Or am I mistaken?

Kind regards,
Stefanie

On Mon, Jan 31, 2011 at 3:05 PM, Martin Honnen 
<Martin(_dot_)Honnen(_at_)gmx(_dot_)de> wrote:


Based on your explanation and the required result I think you should rather
use group-adjacent than group-starting-with.
Here is an example stylesheet:

<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";
 version="2.0"
 exclude-result-prefixes="xs mf">

 <xsl:strip-space elements="*"/>
 <xsl:output indent="yes"/>

 <xsl:function name="mf:group" as="node()*">
   <xsl:param name="nodes" as="node()*"/>
   <xsl:param name="level" as="xs:integer"/>
   <xsl:if test="$nodes">
     <list type="ul">
       <xsl:for-each-group select="$nodes"
group-adjacent="boolean(self::*[@level = $level])">
         <xsl:choose>
           <xsl:when test="current-grouping-key()">
             <xsl:apply-templates select="current-group()"/>
           </xsl:when>
           <xsl:otherwise>
             <xsl:sequence select="mf:group(current-group(), $level + 1)"/>
           </xsl:otherwise>
         </xsl:choose>
       </xsl:for-each-group>
     </list>
   </xsl:if>
 </xsl:function>

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

 <xsl:template match="item[@level]">
   <item>
     <xsl:apply-templates/>
   </item>
 </xsl:template>

 <xsl:template match="test">
   <xsl:copy>
     <xsl:for-each-group select="*" group-adjacent="boolean(self::item)">
       <xsl:choose>
         <xsl:when test="current-grouping-key()">
           <xsl:sequence select="mf:group(current-group(), 0)"/>
         </xsl:when>
         <xsl:otherwise>
           <xsl:apply-templates select="current-group()"/>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:for-each-group>
   </xsl:copy>
 </xsl:template>

</xsl:stylesheet>

When applied to the input

<test>
<p level="0">Der als Filmregisseur  <hi rend="bo">Alan Smithee</hi>
ist ein.. </p>
<p level="0">Das Pseudonym.. .</p>
<p level="0">Der Regisseur...</p>
<head2 level="0">Wissenswertes</head2>
<item level="0" type="ul">Alternative Schreibweisen..</item>
<item level="0" type="ul">Eine alternative Theorie...</item>
<item level="0" type="ul">Zu den ... </item>
<item level="1" type="ul">Don  </item>
<item level="1" type="ul">David Lynch </item>
<item level="1" type="ul">Chris Christensen </item>
<item level="1" type="ul">Stuart Rosenberg</item>
<item level="1" type="ul">Richard C. Sarafian</item>
<item level="1" type="ul">Dennis Hopper  </item>
<item level="2" type="ul"> level 3 ulitem</item>
<item level="1" type="ul">Arthur Hiller (für ), </item>
<item level="1" type="ul">Rick Rosenthal (Birds II) und </item>
<item level="1" type="ul">Kevin Yagher </item>
<item level="0" type="ul">Zu den Drehbuchautoren </item>
<item level="1" type="ul">Sam Raimi und Ivan Raimi </item>
<head2 level="0">Weblinks</head2>
</test>


Saxon 9.3 outputs

<test>
  <p level="0">Der als Filmregisseur  <hi rend="bo">Alan Smithee</hi>
ist ein.. </p>
  <p level="0">Das Pseudonym.. .</p>
  <p level="0">Der Regisseur...</p>
  <head2 level="0">Wissenswertes</head2>
  <list type="ul">
     <item>Alternative Schreibweisen..</item>
     <item>Eine alternative Theorie...</item>
     <item>Zu den ... </item>
     <list type="ul">
        <item>Don  </item>
        <item>David Lynch </item>
        <item>Chris Christensen </item>
        <item>Stuart Rosenberg</item>
        <item>Richard C. Sarafian</item>
        <item>Dennis Hopper  </item>
        <list type="ul">
           <item> level 3 ulitem</item>
        </list>
        <item>Arthur Hiller (f├╝r ), </item>
        <item>Rick Rosenthal (Birds II) und </item>
        <item>Kevin Yagher </item>
     </list>
     <item>Zu den Drehbuchautoren </item>
     <list type="ul">
        <item>Sam Raimi und Ivan Raimi </item>
     </list>
  </list>
  <head2 level="0">Weblinks</head2>
</test>




--

       Martin Honnen
       http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
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>
--~--





-- 
Stefanie Haupt, M.A.

--~------------------------------------------------------------------
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>
--~--