Re: [xsl] Figuring out for-group-by

Subject: Re: [xsl] Figuring out for-group-by
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 9 Jul 2004 22:10:51 +0100
Hi Barry,

> In case you are wondering, I am trying to fix my iTunes library
> which has duplicate entries in it as a result of converting
> everything to Apple Lossless encoding. Looking at the results I can
> see that the attribute labelled 'key' gives me all of the track Ids
> matching the compound key. The fact that there are more than one is
> the issue. What I would like to do is identify the track Id(s)
> associated with the entry that has
> <key>Kind</key><string>Apple Lossless audio file</string>
> within it.

Are you saying that the only <dict> elements that you want to keep are
those with <key>Kind</key><string>Apple Lossless audio file</string>?

If so, you can select just those <dict> elements with:

    [key[. = 'Kind']/following-sibling::string[1] =
     'Apple Lossless audio file']

If selecting just these gives you unique tracks, then you only need to
do a <xsl:for-each> -- no need for any additional grouping. Otherwise,
you can use the above expression in the select attribute of the
<xsl:for-each-group> that you're currently using.
I'm not sure what you mean by "the attribute labelled 'key' gives me
all of the track Ids matching the compound key" but if you can
identify the unique tracks by their track ids, then there's no need to
use the compound key.

Later, you wrote:
> I am getting a little closer. I have managed to pull out
> more-or-less what I am after with
> <xsl:value-of select="current-group()/key[string() = 
> 'Kind']/following-sibling::string[1][string() = 'Apple Lossless audio 
> file']/preceding-sibling::key[string() = 'Track 
> ID']/following-sibling::integer[1]"/>
> but I am concerned that the various keys may not always be in this
> order so the "preceding-sibling" search may not work. I can see that
> I can use for-each with current-group to iterate through the sets of
> keys so that may be more efficient than chasing up and down the
> tree.

This gives you the track ID as long as the Kind of the track is 'Apple
Lossless audio file'. A clearer way of doing this would be to use:

  <xsl:if test="current-group()
                  /key[. = 'Kind']
                    /following-sibling::string[1] =
                'Apple Lossless audio file'">
    <xsl:value-of select="current-group()
                            /key[. = 'Track ID']
                              /following-sibling::integer[1]" />

But I suggest that you only select (and group) the <dict> elements
whose Kind is 'Apple Lossless audio file' rather than filtering the
others out after going to the effort of grouping them.


Jeni Tennison

Current Thread