Re: [xsl] Relationships in for-each statement

Subject: Re: [xsl] Relationships in for-each statement
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 07 Sep 2006 17:29:55 -0400
Bob,

At 04:21 PM 9/7/2006, you wrote:
I have my test to see if it's finding my target person's name (and
that person is acting) anyplace...

<xsl:if test="//member[(role='Actor') or (role='Guest Star') or
(role='Host')][concat(tv:givenname, ' ', tv:surname) = $matcher]">

Which works fine. Once I know the person is in the data, I go looking
in detail, and my current solution for that is...

<!-- Loop on every crew listed -->
<xsl:for-each select="//member[(role='Actor') or (role='Guest Star')
or (role='Host')][concat(tv:givenname, ' ', tv:surname) = $matcher]" >
       <!-- Get //crew @program and do stuff -->
        <xsl:variable name="progID" select="../@program" />
        <!-- And stuff -->

... which works. But I have similar loops elsewhere where I can
successfully get those two operations combined into a single for-each.
The simplest one is...

<xsl:for-each select="//program[tv:title = $matcher]/@id">

How would I go about getting to where my for-each would be operating
from the @program of (the assorted met conditions)?

First, it doesn't look to me like you need that xsl:if test at all. As on other occasions, if no such nodes exist, a for-each instruction won't select any to operate on.


So...

<xsl:for-each select="$monster-path-perhaps-best-bound-to-a-variable">
  <xsl:for-each select="../@program">
    ... stuff on the @program attribute
  </xsl:for-each>
  ... other stuff on the members you've selected
</xsl:for-each>

If you're asking how to process the @program attributes only once each and not once for each member of interest, simply extend your monster path a bit further:

<xsl:for-each
  select="$monster-path-perhaps-best-bound-to-a-variable/../@program">
  ... stuff on the program attributes ...
</xsl:for-each>

... and let set logic take care of the rest.

Although we often deride it for its frequent misuses, for-each is actually handy not only as a switcher of our processing context but also as a conditional -- process a given node if it exists; and if not, merrily continue on.

Cheers,
Wendell

Current Thread