[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: XSLT: Selecting nodes based on a group of other nodes


Subject: Re: XSLT: Selecting nodes based on a group of other nodes
From: Miloslav Nic <nicmila@xxxxxxxx>
Date: Thu, 09 Sep 1999 08:58:29 +0200

Look at http://zvon.vscht.cz/HTMLonly/XSLTutorial/Books/Book1/index.html
example 75. It can help.

Mark Sztainbok wrote:
> 
> Is there a way to select nodes out of a document if they have a value which
> matches a value in another group of nodes?
> 
> For example,
> 
> if I have a document which looks like this:
> 
> <RESPONSE>
>     <PRODUCT>
>         <ID>ABC</ID>
>         <NAME>Brown dog</NAME>
>     </PRODUCT>
>     <PRODUCT>
>         <ID>DEF</ID>
>         <NAME>Black cat</NAME>
>     </PRODUCT>
>     <PRODUCT>
>         <ID>GHI</ID>
>         <NAME>Yellow giraffe</NAME>
>     </PRODUCT>
>     <PRODUCT>
>         <ID>JKL</ID>
>         <NAME>Orange monkey</NAME>
>     </PRODUCT>
>     <SELECTEDPRODUCT>ABC</SELECTEDPRODUCT>
>     <SELECTEDPRODUCT>GHI</SELECTEDPRODUCT>
> </RESPONSE>
> 
> I would like to get a node list of the products which have an ID which is in
> a SELECTEDPRODUCT element i.e. products ABC and GHI (Brown dog and Yellow
> giraffe) and store it in a variable so I can use it later with for-each
> 
> I have tried the following XSLT code but it doesn't work as it only gets the
> first product:
> <xsl:variable name="IDS" select="/RESPONSE/SELECTEDPRODUCT"/>
> <xsl:variable name="products" select="/RESPONSE/PRODUCT[ID = $IDS]/>
> 
> and have also tried this, which also did not work:
> <xsl:variable name="products"/>
> <xsl:for-each select="RESPONSE/SELECTEDPRODUCT">
>     <xsl:variable name="product_id" select="text()"/>
>     <xsl:choose>
>         <xsl:when test="$products">
>             <xsl:variable name="products"
> select="$products|/RESPONSE/PRODUCT[ID=$product_id])"/>
>         </xsl:when>
>         <xsl:otherwise>
>             <xsl:variable name="products"
> select="/RESPONSE/PRODUCT[ID=$product_id]"/>
>         </xsl:otherwise>
>    </xsl:choose>
> </xsl:for-each>
> 
> What is the correct way to do this?
> 
> Thanks,
> 
> Mark

-- 
***************************************************************
Dr. Miloslav Nic                        e-mail: nicmila@xxxxxxxx
Department of Organic Chemistry         TEL: +420 2 2435 5012  
ICT Prague (VSCHT Praha)                     +420 2 2435 4118
    				        FAX: +420 2 2435 4288  
****************************************************************


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



Current Thread
Keywords