Re: [xsl] Compound filter in for-each

Subject: Re: [xsl] Compound filter in for-each
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Fri, 27 Mar 2009 12:39:42 -0400
Alan,

At 05:22 AM 3/27/2009, you wrote:
        <xsl:template match="Folder">
                        <xsl:for-each
select="Placemark[ExtendedData/Data/attribute::name='rdb_status' and
ExtendedData/Data/value='Endangered']">
                        <xsl:value-of select="@id" />,
                        <xsl:value-of select="name" />,
                        <xsl:value-of
select="ExtendedData/Data[@name='location_name']/value"/>,
                        <xsl:value-of
select="ExtendedData/Data[@name='grid_ref']/value"/>,
                        <xsl:value-of
select="ExtendedData/Data[@name='rdb_status']/value"/>,
                        <xsl:value-of
select="ExtendedData/Data[@name='last_seen']/value"/>,
                        <br/>
                        </xsl:for-each>
        </xsl:template>


the <xsl:for-each> element being the most significant in the context of this problem.

Right.


From here, it's a short step to use a template (with Mike's correction):

<xsl:template match="Folder">
  <xsl:apply-templates select="Placemark[ExtendedData/Data
    [attribute::name='rdb_status' and value='Endangered']]"/>
  </xsl:template>

<xsl:template match="Placemark">
  <xsl:value-of select="@id" />,
  <xsl:value-of select="name" />,
  <xsl:value-of select="ExtendedData/Data[@name='location_name']/value"/>,
  <xsl:value-of select="ExtendedData/Data[@name='grid_ref']/value"/>,
  <xsl:value-of select="ExtendedData/Data[@name='rdb_status']/value"/>,
  <xsl:value-of select="ExtendedData/Data[@name='last_seen']/value"/>,
  <br/>
</xsl:template>

Now, this is an improvement only if:

(a) you consider it a cosmetic improvement, or
(b) your problem becomes more complex and you discover you have to do the same thing with other Placemark elements in other situations.


More refactoring is also possible; for example you could have a template matching 'Data' elements that would write their 'value' elements, if you did a lot of that. But again, for a simple case (especially if it's a single-purpose, one-off stylesheet) that might be overkill, and you get the idea.

Cheers,
Wendell



======================================================================
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