Use of keys: Consolidating multiple <ul> with intervening <p> elements
===

I have multiple instances of xml similar to the following:

    <refbody>
    <ul>
        <li><uicontrol>FOO</uicontrol>BAR</li>
    </ul>
    <p>Values: 000 - 999</p>
    <ul>
        <li><uicontrol>FOO</uicontrol>BAR</li>
        <li><uicontrol>FOO</uicontrol>BAR</li>
        <li><uicontrol>FOO</uicontrol>BAR</li>
        <li><uicontrol>FOO</uicontrol>BAR</li>
    </ul>
    <p>Values:</p>
    <p>lorem ipsum</p>
    <p>lorem ipsum</p>
    <p>lorem ipsum</p>
    </refbody>

What I would like to have happen is to consolidate all `<ul>` elements into a single `<dl>`.

Each `<li>` should create a child `<dlentry>`.

Each `<uicontrol>` should become a `<dt>` that is a child of its `<dlentry>`.

The remaining text in the `<li>` should be put in a `<dd>` element, which is also a child of the `<dlentry>`.

My style sheet (provided below) accomplishes much this already. Where I am running into trouble is the last requirement:

Any `<p>` needs to be put inside of the `<dlentry>` associated with its `preceding-sibling::li[1]`.  

So the desired result looks like this:

    <refbody>
    <dl>
        <dlentry>
            <dt>FOO</dt>
            <dd>BAR
                <p>Values: 000 - 999</p>
            </dd>
        </dlentry>
        <dlentry>
            <dt>FOO</dt>
            <dd>BAR</dd>
        </dlentry>
        <dlentry>
            <dt>FOO</dt>
            <dd>BAR</dd>
        </dlentry>
        <dlentry>
            <dt>FOO</dt>
            <dd>BAR</dd>
        </dlentry>
        <dlentry>
            <dt>FOO</dt>
            <dd>BAR
                <p>Values:</p>
                <p>lorem ipsum</p>
                <p>lorem ipsum</p>
                <p>lorem ipsum</p>
            </dd>
        </dlentry>
    </dl>
    </refbody>

I think I need to use keys to do this, but I just can't get it to work properly. I would really appreciate it if someone could tell me what I'm doing wrong. 

Here is my stylesheet:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:key name="kValues" match="p" 
        use="generate-id[preceding::li[1]]"/>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
 
    <xsl:template match="ul[ancestor::refbody][1]">
        <dl>
            <xsl:for-each select="child::li|following::li">
                <xsl:variable name="vValues">
                    <xsl:value-of select="key('kValues',generate-id())"/>
                </xsl:variable>
                <dlentry>
                    <dt>
                        <xsl:value-of select="child::uicontrol"/>
                    </dt>
                    <dd>
                         <xsl:value-of select="text()"/>
                        <xsl:if test="$vValues">
                            <xsl:copy-of select="$vValues" />
                        </xsl:if>
                    </dd>
                </dlentry>
            </xsl:for-each>
        </dl>
    </xsl:template>
 
    </xsl:stylesheet>
 