how to use select result in another select [modified]
-
<Root> <Products> <Item ItemColor="Red" ItemName="Item1"/> <Item ItemColor="White" ItemName="Item2"/> <Item ItemColor="Blue" ItemName="Item2"/> </Products> <Desciptions> <Color ColorName="Red" ColorId="15"/> <Color ColorName="White" ColorId="14"/> <Color ColorName="Blue" ColorId="13"/> </Desciptions> </Root> <xsl:template match="/"> <xsl:for-each select="Products/Item"> <tr> <td><xsl:value-of select="ItemColor" /></td> <td> ? Right here I want the ColorId of the ColorName that matches ItemColor found by the previous select ?</td> </tr> </xsl:for-each> </xsl:template match="/"> Thank You for reading this post.
modified on Friday, July 10, 2009 6:12 PM
-
<Root> <Products> <Item ItemColor="Red" ItemName="Item1"/> <Item ItemColor="White" ItemName="Item2"/> <Item ItemColor="Blue" ItemName="Item2"/> </Products> <Desciptions> <Color ColorName="Red" ColorId="15"/> <Color ColorName="White" ColorId="14"/> <Color ColorName="Blue" ColorId="13"/> </Desciptions> </Root> <xsl:template match="/"> <xsl:for-each select="Products/Item"> <tr> <td><xsl:value-of select="ItemColor" /></td> <td> ? Right here I want the ColorId of the ColorName that matches ItemColor found by the previous select ?</td> </tr> </xsl:for-each> </xsl:template match="/"> Thank You for reading this post.
modified on Friday, July 10, 2009 6:12 PM
Throw the value into a variable then XPath with a condition on the variable, like this:
<xsl:for-each select="Products/Item">
<xsl:variable name="colorname" select="@ItemColor"/>
<tr>
<td><xsl:value-of select="$colorname"/></td>
<td><xsl:value-of select="//Color[@ColorName=$colorname]/@ColorId"/></td>
</tr>
</xsl:for-each>Jeremy Likness http://csharperimage.jeremylikness.com/
-
Throw the value into a variable then XPath with a condition on the variable, like this:
<xsl:for-each select="Products/Item">
<xsl:variable name="colorname" select="@ItemColor"/>
<tr>
<td><xsl:value-of select="$colorname"/></td>
<td><xsl:value-of select="//Color[@ColorName=$colorname]/@ColorId"/></td>
</tr>
</xsl:for-each>Jeremy Likness http://csharperimage.jeremylikness.com/
Thanks , you have solved my problem and enlightened me on the use of variables . Very much appreciated .
-
<Root> <Products> <Item ItemColor="Red" ItemName="Item1"/> <Item ItemColor="White" ItemName="Item2"/> <Item ItemColor="Blue" ItemName="Item2"/> </Products> <Desciptions> <Color ColorName="Red" ColorId="15"/> <Color ColorName="White" ColorId="14"/> <Color ColorName="Blue" ColorId="13"/> </Desciptions> </Root> <xsl:template match="/"> <xsl:for-each select="Products/Item"> <tr> <td><xsl:value-of select="ItemColor" /></td> <td> ? Right here I want the ColorId of the ColorName that matches ItemColor found by the previous select ?</td> </tr> </xsl:for-each> </xsl:template match="/"> Thank You for reading this post.
modified on Friday, July 10, 2009 6:12 PM
An alternative solution without variables:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/><xsl:template match="/">
<xsl:for-each select="//Products/Item">
<tr>
<td><xsl:value-of select="@ItemColor" /></td>
<td><xsl:value-of select="//Desciptions/Color[@ColorName=current()/@ItemColor]/@ColorId"/></td>
</tr>
</xsl:for-each>
</xsl:template></xsl:stylesheet>
current() retrieves the context node of the scope surrounding the select attribute, i.e. the node currently selected by the for-each element in this case. Here's another solution, using an XSLT key element. This approach is very useful when there's a large number of things to lookup in.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/><xsl:key name="color-name-to-id" match="//Desciptions/Color/@ColorId" use="../@ColorName"/>
<xsl:template match="/">
<xsl:for-each select="//Products/Item">
<tr>
<td><xsl:value-of select="@ItemColor" /></td>
<td><xsl:value-of select="key('color-name-to-id', @ItemColor)"/></td>
</tr>
</xsl:for-each>
</xsl:template></xsl:stylesheet>
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p