xsl-list
[Top] [All Lists]

Re: [xsl] Grouping lists from flat to nested

2011-01-31 08:06:16
Stefanie Haupt wrote:

I've chunked my grouping attempts and indeed there is another grouping
happening later (dividing the headers with the following elements into
nested sections) but here I only wanted to group the lists properly. I
found it rather confusing to group too much at a time. To answer your
question: I would not want to nest the p elements. I've just added the
level attribute here to prevent these elements from being nested
within the lists (because that's what happening if i remove the
attribute).

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