xsl-list
[Top] [All Lists]

[xsl] Subject: Counting Path Occurrences

2007-03-29 18:38:44

Hi - I'm trying to write a generic stylesheet to count occurrences of
full paths to elements.  For example, I'd like input like this:

<a>
        <b>
                <x>
                        <w>blah</w>
                </x>
                <y>bleh</y>
        </b>
        <b>
                <x>
                        <w>blih</w>
                </x>
                <x>
                        <w>bloh</w>
                </x>
        </b>
        <c>
                <w>blwh</w>
                <w>blyh</w>
        </c>
</a>

To generate this output:

/a - 1
/a/b - 2
/a/b/x - 3
/a/b/x/w - 3
/a/b/y - 1
/a/c - 1
/a/c/w - 2

So, start with a stylesheet function to generate the paths (/a/b/x)
using the ancestor-or-self axis:

<xsl:function name="gn:path">
        <xsl:param name="node" as="node()"/>
        <xsl:for-each select="$node">
                <xsl:for-each select="ancestor-or-self::*"><xsl:value-of
select="concat('/', name())"/></xsl:for-each>
        </xsl:for-each>
</xsl:function>

My hope was to use it like this in the template that matches "/":

<xsl:for-each-group select="descendant-or-self::*"
group-by="gn:path(.)">
        Path: <xsl:value-of select="gn:path(.)"/> - Count: <xsl:value-of
select="count(current-group())"/>
</xsl:for-each-group>

Unfortunately, stylesheet functions only return item()*, not string, so
this doesn't work - the current-grouping-key() is always something like
"/a" or "/x".  Could I concatenate all the pieces of the sequence with a
template, then return that out of function (since a 1-element sequence
is the same as the single element?)  Or is there a cleaner way?

Thanks,
Scott C.

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

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