xsl-list
[Top] [All Lists]

Re: Tricky inclusion match

2005-03-29 16:26:06
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(_at_)mulberrytech(_dot_)com
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
======================================================================


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>