[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] key using a number of siblings


Subject: Re: [xsl] key using a number of siblings
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Thu, 14 Nov 2002 01:10:30 +0100

Isn't this a simple grouping problem?

XSLT:

<xsl:key name="words" match="w" use="generate-id((self::w|preceding-sibling::w) [not(f/@type='completion')][last()])"/>

<xsl:template match="root">
    <root>
        <xsl:apply-templates select="w[not(f/@type='completion')]"
mode="word"/>
    </root>
</xsl:template>

<xsl:template match="w" mode="word">
    <word>
        <xsl:apply-templates select="key('words', generate-id())"/>
    </word>
</xsl:template>

<xsl:template match="w">
    <xsl:value-of select="."/>
    <xsl:if test="position() != last()">+</xsl:if>
</xsl:template>

Input:

<root>
    <w>a</w>
    <w><f type="completion">b</f></w>
    <w><f type="completion">c</f></w>
    <w>d</w>
    <w><f type="completion">e</f></w>
    <w><f type="completion">f</f></w>
    <w>g</w>
    <w><f type="completion">h</f></w>
    <w><f type="completion">i</f></w>
    <w>j</w>
    <w><f type="completion">k</f></w>
    <w><f type="completion">l</f></w>
</root>

Output:

<root>
<word>a+b+c</word>
<word>d+e+f</word>
<word>g+h+i</word>
<word>j+k+l</word>
</root>

Or did I miss something important?

Regards,

Joerg

Wendell Piez wrote:
> Franklin,
>
> At 05:13 PM 11/13/2002, you wrote:
>
>> I have data that looks like
>>
>> ... <w>a</w> <w><f type="completion">b</f></w> <w><f
>> type="completion">c</f></w> ...
>>
>> I am doing processing for each "word", and am attempting to do this
>>  using a key.  The key
>>
>> <xsl:key name="words" match="w[not(f/@type='completion')]"
>> use="normalize-space(text())"/>
>>
>> manages to match what I want, I believe, but instead of
>>
>> text(), I really want to concatenate text() with all following
>> siblings that are completions, such that for the example above, I
>> get the string
>>
>> "a+b+c"
>
>
> Oo.
>
>> Is there an elegant way to do this?
>
>
> The cleanest approach to this I can think of is to pass twice, first
> to process your <w> combinations into their full "words", the second
> to key them and do whatever you're doing. This could be done in a way
> that wouldn't mess too much with the data if the first pass worked to
>  introduce a wrapper, so
>
> <w>a</w> <w><f type="completion">b</f></w> <w><f
> type="completion">c</f></w>
>
> would appear as
>
> <word key="a+b+c"> <w>a</w> <w><f type="completion">b</f></w> <w><f
> type="completion">c</f></w> </word>
>
> This would require a "levitation" algorithm (i.e. a grouping to
> unflatten the hierarchy) to collect the <w> elements properly.
>
> Your problem as stated must be the Puzzler of the Week; I wonder if
> someone can come up with something (but methinks it can't be done).
>
> I hope this helps, Wendell


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list




Current Thread