Re: [xsl] Grouping hierarchy path elements, part 2

Subject: Re: [xsl] Grouping hierarchy path elements, part 2
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 24 Aug 2004 10:33:31 +0100
Hi Daniel,

> It seems as if there is something wrong with my items parameter for
> the transformDocument template called from the template 'otherItem'.
> If you run the above, you will notice that <xsl:value-of select="
> //item[attribute::id]"/> actually does not return the id-Attribute
> element of all items, but instead returns the child elements of the
> first referenced item. Finding a solution to this would get me a big
> step ahead I think.

You're currently using:

  //item[attribute::id = referenceItemId]

This selects those <item> elements whose id attribute is equal to the
value of a <referenceItemId> element *within that <item> element*. You
want to select the <item> elements whose id attribute is equal to the
value of a <referenceItemId> element *within the current <otherItem>
element*. To do this, you can use:

  //item[@id = current()/referenceItemId]

though personally, I would define a key to index the <item> elements
by their id:

<xsl:key name="items" match="item" use="@id" />

and then use the key() function to retrieve the relevant items:

  key('items', referenceItemId)

as this is more efficient.

Note that you should also use the items for the particular <otherItem>
when identifying the name of the root folder. I suggest changing the
template for <otherItem> elements to:

<xsl:template match="otherItem">
  <Chapter id="{@id}">
    <xsl:variable name="items" select="key('items', referenceItemId)" />
    <xsl:variable name="root" select="substring-before($items[1]/path, '\')"/>
    <path name="{$root}">
      <xsl:call-template name="transformDocument">
        <xsl:with-param name="path" select="$root"/>
        <xsl:with-param name="items" select="$items"/>
      </xsl:call-template>
    </path>
  </Chapter>
</xsl:template>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread