Re: [xsl] Tricky inclusion match

Subject: Re: [xsl] Tricky inclusion match
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 29 Mar 2005 18:26:06 -0500
Karl,

It turned out that keys weren't actually necessary: as posed (as I understand it) the colors problem could be solved with a simple (if not obvious) test. But using a key does make it slightly more efficient:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

<xsl:key name="pictures-by-color" match="picture" use="color"/>

<xsl:variable name="colors" select="/data/colors/color"/>

  <xsl:template match="data">
    <xsl:apply-templates select="colors"/>
    <!-- if we weren't going to select using the key, we could go straight to
         the pictures; but then we'd be testing each one -->
  </xsl:template>

  <xsl:template match="colors">
    <xsl:apply-templates select="key('pictures-by-color',color)"/>
    <!-- by using the key we select just the pictures that have at least
         one color matching a color in your set -->
  </xsl:template>

<xsl:template match="picture">
<xsl:if test="count(color[not(.=preceding-sibling::color)][.=$colors]) &gt;= 2">
<xsl:text>&#xA;picture sample #</xsl:text>
<xsl:value-of select="@sample"/>
</xsl:if>
</xsl:template>


</xsl:stylesheet>

results (over your original data):

picture sample #2
picture sample #4
picture sample #5

-- which I think you said is what you want.

The action is in that count of the colors inside each picture:

color[not(.=preceding-sibling::color)][.=$colors]

The first predicate deduplicates each color against its siblings; the second sees whether it's listed among the colors you want. This yields the set of unique colors (within each parent) listed among the colors of interest, which you can count.

Note that it's a brute-force deduplication; if you have lots and lots of siblings you could optimize that first predicate (using another key).

I hope that's what you were after --

Cheers,
Wendell

At 06:12 PM 3/29/2005, you wrote:
Wendell:
I am much insterested in your XSLT 1.0 solution to the original colors
problem.  Indeed, it is the "simple" version of the problem which I am
facing.
Thanks.  Karl



====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================

Current Thread