RE: [xsl] matching on multiple attributes contained in a node-set

Subject: RE: [xsl] matching on multiple attributes contained in a node-set
From: Carl Yu <carl.yu@xxxxxxxxxxxx>
Date: Fri, 28 Mar 2003 08:37:49 -0800 (PST)
Thanks for cleaning up my implementation, but -- unfortunately, I'm looking 
something that outputs 1 line for each and every student, not just the ones in 
the list.  I've altered your implementation to do this... but it suffers from 
the same problem as my initial implementation.

[input]
<?xml version="1.0" encoding="UTF-8"?>
<dftml version="0.2">
  <student>
     <name value="bob"/>
     <grade value="1"/>
     <type value="lunch"/>
  </student>
  <student>
     <name value="joe"/>
     <grade value="1"/>
     <type value="science"/>
  </student>
  <student>
     <name value="peter"/>
     <grade value="2"/>
     <type value="math"/>
  </student>
  <student>
     <name value="larry"/>
     <grade value="1"/>
     <type value="math"/>
  </student>
</dftml>

[modified xsl]
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:xalan="http://xml.apache.org/xalan"; exclude-result-prefixes="xalan" 
version="1.0">
<xsl:output method="text"/>

<xsl:variable name="list">
<item grade="1" type="math"/>
<item grade="2" type="science"/>
</xsl:variable>

<xsl:template match="dftml">
  <xsl:for-each select="student">
    <xsl:variable name="s" select="." />
    <xsl:for-each select="xalan:nodeset($list)/item">
      <xsl:choose>
        <xsl:when test="$s/grade/@value = @grade and $s/type/@value = @type">
         <xsl:value-of select="$s/name/@value" />
         <xsl:text> is in the list.&#xA;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
         <xsl:value-of select="$s/name/@value" />
         <xsl:text> is not in the list.&#xA;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

</xsl:transform>

[output]
bob is not in the list.
bob is not in the list.
joe is not in the list.
joe is not in the list.
peter is not in the list.
peter is not in the list.
larry is in the list.
larry is not in the list.

[desired output]
bob is not in the list.
joe is not in the list.
peter is not in the list.
larry is in the list.

Thanks,
Carl

>
>Date: Fri, 28 Mar 2003 07:11:00 +0200
>From: Jarno.Elovirta@xxxxxxxxx
>Subject: RE: [xsl] matching on multiple attributes contained in a node-set
>
>Hi,
>
>> The desired output would list all the students, but only 
>> once.  That is, larry 
>> is listed as being on the list, all others are not.
>> 
>> The variable $list can be variable in length.. that is, I may 
>> add or remove 
>> items from this list (in fact, in the real-deal, this is a 
>> passed-in parameter 
>> to the Transformer.)
>
><xsl:template match="dftml">
>  <xsl:for-each select="student">
>    <xsl:variable name="s" select="." />
>    <xsl:for-each select="xalan:nodeset($list)/item[$s/grade/@value = @grade 
and $s/type/@value = @type]">
>      <xsl:value-of select="$s/name/@value" />
>      <xsl:text> is in the list.&#xA;</xsl:text>
>    </xsl:for-each>
>  </xsl:for-each>
></xsl:template>
>
>You should consider switching from xalan:nodeset to exslt:node-set. Also, if 
the $list is passed as a parameter, you shouldn't need to use node-set 
extensions. Hope this helps,
>
>Cheers,
>
>Jarno - Linnunradan kdsikirja liftareille: Osa 4
>
> XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread