Subject: Re: [xsl] Grouping lists from flat to nested From: Stefanie Haupt <st.haupt@xxxxxxxxx> Date: Mon, 31 Jan 2011 16:47:07 +0100 |
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.Honnen@xxxxxx> 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 > B xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > B xmlns:xs="http://www.w3.org/2001/XMLSchema" > B xmlns:mf="http://example.com/mf" > B version="2.0" > B exclude-result-prefixes="xs mf"> > > B <xsl:strip-space elements="*"/> > B <xsl:output indent="yes"/> > > B <xsl:function name="mf:group" as="node()*"> > B B <xsl:param name="nodes" as="node()*"/> > B B <xsl:param name="level" as="xs:integer"/> > B B <xsl:if test="$nodes"> > B B B <list type="ul"> > B B B B <xsl:for-each-group select="$nodes" > group-adjacent="boolean(self::*[@level = $level])"> > B B B B B <xsl:choose> > B B B B B B <xsl:when test="current-grouping-key()"> > B B B B B B B <xsl:apply-templates select="current-group()"/> > B B B B B B </xsl:when> > B B B B B B <xsl:otherwise> > B B B B B B B <xsl:sequence select="mf:group(current-group(), $level + 1)"/> > B B B B B B </xsl:otherwise> > B B B B B </xsl:choose> > B B B B </xsl:for-each-group> > B B B </list> > B B </xsl:if> > B </xsl:function> > > B <xsl:template match="@* | node()"> > B B <xsl:copy> > B B B <xsl:apply-templates select="@*, node()"/> > B B </xsl:copy> > B </xsl:template> > > B <xsl:template match="item[@level]"> > B B <item> > B B B <xsl:apply-templates/> > B B </item> > B </xsl:template> > > B <xsl:template match="test"> > B B <xsl:copy> > B B B <xsl:for-each-group select="*" group-adjacent="boolean(self::item)"> > B B B B <xsl:choose> > B B B B B <xsl:when test="current-grouping-key()"> > B B B B B B <xsl:sequence select="mf:group(current-group(), 0)"/> > B B B B B </xsl:when> > B B B B B <xsl:otherwise> > B B B B B B <xsl:apply-templates select="current-group()"/> > B B B B B </xsl:otherwise> > B B B B </xsl:choose> > B B B </xsl:for-each-group> > B B </xsl:copy> > B </xsl:template> > > </xsl:stylesheet> > > When applied to the input > > <test> > <p level="0">Der als Filmregisseur B <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 B </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 B </item> > <item level="2" type="ul"> level 3 ulitem</item> > <item level="1" type="ul">Arthur Hiller (fC<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> > B <p level="0">Der als Filmregisseur B <hi rend="bo">Alan Smithee</hi> > ist ein.. </p> > B <p level="0">Das Pseudonym.. .</p> > B <p level="0">Der Regisseur...</p> > B <head2 level="0">Wissenswertes</head2> > B <list type="ul"> > B B B <item>Alternative Schreibweisen..</item> > B B B <item>Eine alternative Theorie...</item> > B B B <item>Zu den ... </item> > B B B <list type="ul"> > B B B B <item>Don B </item> > B B B B <item>David Lynch </item> > B B B B <item>Chris Christensen </item> > B B B B <item>Stuart Rosenberg</item> > B B B B <item>Richard C. Sarafian</item> > B B B B <item>Dennis Hopper B </item> > B B B B <list type="ul"> > B B B B B B <item> level 3 ulitem</item> > B B B B </list> > B B B B <item>Arthur Hiller (fbbr ), </item> > B B B B <item>Rick Rosenthal (Birds II) und </item> > B B B B <item>Kevin Yagher </item> > B B B </list> > B B B <item>Zu den Drehbuchautoren </item> > B B B <list type="ul"> > B B B B <item>Sam Raimi und Ivan Raimi </item> > B B B </list> > B </list> > B <head2 level="0">Weblinks</head2> > </test> > > > > > -- > > B B B B Martin Honnen > B B B B http://msmvps.com/blogs/martin_honnen/ > > -- Stefanie Haupt, M.A.
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Grouping lists from flat , Martin Honnen | Thread | Re: [xsl] Grouping lists from flat , Stefanie Haupt |
[xsl] EMF, Rolf Schumacher | Date | Re: [xsl] Grouping lists from flat , Stefanie Haupt |
Month |