xsl-list
[Top] [All Lists]

XSLT2: Clustering, or Grouping the groups

2004-05-24 15:52:24
Hi all,

I am using XSLT2 as implemented by Saxon 7.9.1 to group a flat
structure. What I start out with is something like

    <foo>
        <bar baz="1" />
        <bar baz="2" />
        <bar baz="2" />
        <bar />
        <bar baz="1" />
        <bar baz="1" />
        <bar />
    </foo>

Now I need to put these bars in a list. The first step is easy
enough (I'm only paraphrasing; excuse any syntax errors please):

    <xsl:for-each-group select="bar" group-adjacent="@baz">
        <xsl:choose>
            <xsl:when test="@baz">
                <list-item>
                    <xsl:copy-of select="current-group()" />
                </list-item>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="current-group()" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each-group>

Now what I get looks like this:

    <foo>
        <list-item>
            <bar baz="1" />
        </list-item>
        <list-item>
            <bar baz="2" />
            <bar baz="2" />
        </list-item>
        <bar />
        <list-item>
            <bar baz="1" />
            <bar baz="1" />
        </list-item>
        <bar />
    </foo>

This is all well and good, but now I need to get to

    <foo>
        <list>
            <list-item>
                <bar baz="1" />
            </list-item>
            <list-item>
                <bar baz="2" />
                <bar baz="2" />
            </list-item>
        </list>
        <bar />
        <list>
            <list-item>
                <bar baz="1" />
                <bar baz="1" />
            </list-item>
        </list>
        <bar />
    </foo>

The only way I've succeeded in doing this was by two separate
stylesheets (where the second is pretty much a copy of the first,
except for different conditions and element names), which is
suboptimal.

I'd like to do this in a single step. I tried assigning the
output to a variable and processing it afterwards using something
like

    <xsl:variable name="list">
        <xsl:for-each-group select="bar" group-adjacent="@baz">
            <xsl:choose>
                <xsl:when test="@baz">
                    <list-item>
                        <xsl:copy-of select="current-group()" />
                    </list-item>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="current-group()" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </xsl:variable>
    <xsl:for-each-group select="$list" 
group-adjacent="boolean(self::list-item)">
        <xsl:choose>
            <xsl:when test="   SOME-TEST-HERE()   "> <!-- KEY BIT -->
                <list-item>
                    <xsl:copy-of select="current-group()" />
                </list-item>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="current-group()" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each-group>

Unfortunately, both what the context node is as well as the value
of current-group() at the point of SOME-TEST-HERE() seem to defy
all reason or reasonable expectation in such a way that I can't
figure out how to get the desired result.

Uh.. help?

-- 
Thanks muchly in advance,
Aristotle
 
"If you can't laugh at yourself, you don't take life seriously enough."


<Prev in Thread] Current Thread [Next in Thread>