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

[xsl] Applying templates for unique children


Subject: [xsl] Applying templates for unique children
From: Elizabeth Barham <lizzy@xxxxxxxxxxxxxxxxx>
Date: 04 Aug 2003 15:06:12 -0500

Hi,

   Consider this:

  <root>
    <sequence>carcin</sequence>
    <combining-vowel>o</combining-vowel>
    <definition xml:lang="en">cancer</definition>
  </root>
  <root>
    <sequence>carcin</sequence>
    <combining-vowel>o</combining-vowel>
    <definition xml:lang="en">cancer</definition>
    <definition xml:lang="en">cancerous</definition>
  </root>
  <root>
    <sequence>carcin</sequence>
    <combining-vowel>o</combining-vowel>
    <definition xml:lang="en">cancerous, cancer</definition>
  </root>

  With the goal of turning it into:

  <root>
    <sequence>carcin</sequence>
    <combining-vowel>o</combining-vowel>
    <definition xml:lang="en">cancer</definition>
    <definition xml:lang="en">cancerous</definition>
    <definition xml:lang="en">cancerous, cancer</definition>
  </root>

I'm using the Muenchian grouping method to select the first <root> element
with the same <sequence>:

   <xsl:key name="root-key" match="root" use="sequence"/>

and

      <xsl:for-each select="root[generate-id() = generate-id(key('root-key', sequence)[1])]">
	<xsl:apply-templates select="."/>
      </xsl:for-each>

Which is great because the matching template is called once for each
unique <sequence>, but the kicker is the group of <definition>'s that
much be unique as well. So far, this is what I have:

  <xsl:template match="root">
    <xsl:copy>
      <xsl:apply-templates select="sequence | combining-vowel"/>
      <xsl:for-each select="key('root-key', sequence)/definition">
	<xsl:sort />
	<xsl:apply-templates select="."/>
      </xsl:for-each>
    </xsl:copy>
  </xsl:template>

Which applies the template on *all* definitions for a the matching
root/sequence, not simply the unique one.

   Why don't I just use the Muenchian method again? Because there are
<definition> elements that have the same value under a different
root/sequence so the first element in the key may not be the same as
the one in the current node, e.g.:

  <xsl:key name="definition-key" match="definition" use="."/>

   If I filter on the first element of definition-key, as in:

      <xsl:for-each select="key('root-key', sequence)/definition[generate-id() = generate-id(key('definition-key', .)[1])]">
	<xsl:sort />
	<xsl:apply-templates select="."/>
      </xsl:for-each>

There may be *no* match because, as an example, there may have been
this before the "carcin" elements used in the first example:

  <root>
    <sequence>anything</sequence>
    <combining-vowel>a</combining-vowel>
    <definition xml:lang="en">cancer</definition>
  </root>

Any ideas on how to solve this?

Thank you,
Elizabeth

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



Current Thread