tags:

views:

40

answers:

1

I have an XML document of store locations which have a Type node indicating the type of store and an existing set of templates for adding a key to each Location based on it's state which then adds these to an HTML in order of their state.

What I need to do is exclude all of the states from display that do not have a Type of 1. It's easy enough in the for-each loop to skip display of those Locations but I end up with states displaying that have no values in them.

Here's the code inside the <select/> HTML tag:

<xsl:apply-templates select="$locations/descendant::Locations" mode="locations.list"/>

That template looks like this:

<xsl:template match="Locations" mode="locations.list">
    <xsl:apply-templates select="Location[generate-id(.) = generate-id(key('state', State)[1])]"  mode="locations.list.location"/>
</xsl:template>

The template it calls looks like:

<xsl:template match="Location" mode="locations.list.location">
    <option value="state"><xsl:value-of select="normalize-space(State)"/></option>
    <xsl:for-each select="key('state', State)">
        <option value="{normalize-space(SiteKey)}"><xsl:value-of select="normalize-space(Location)/></option>
    </xsl:for-each>
    <option value=""/>
</xsl:template>

Ordinarily in the <xsl:apply-templates select="Location[generate-id(.) = generate-id(key('state', State)[1])]" mode="locations.list.location"/> I'd just indicate in the select that I was only interested in Locations that have a Type of '1' but I can't seem to find a way to insert that condition along with the key generation code.

+1  A: 

Without seeing your entire input XML, this is a bit of guesswork:

<xsl:apply-templates 
  mode   = "locations.list.location"
  select = "Location[
    generate-id(.) 
    = 
    generate-id(key('state', State)[Type = '1'][1])
  ]" 
 />

Selects Locations nodes where the unique id equals the first Location with the current State.

Tomalak
Very close to what I ultimately did. I neglected to include this piece of code which I entirely overlooked;`<xsl:key name="state" match="Location[Type = '0' or Type='1']" use="State"/>`So the change I needed to make was pretty simply in retrospect. /sigh
Otis
Such is life. ;-)
Tomalak