Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. how to use select result in another select [modified]

how to use select result in another select [modified]

Scheduled Pinned Locked Moved XML / XSL
regexxmltutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    daveyerwin
    wrote on last edited by
    #1

    <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

    J S 2 Replies Last reply
    0
    • D daveyerwin

      <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

      J Offline
      J Offline
      Jeremy Likness
      wrote on last edited by
      #2

      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/

      D 1 Reply Last reply
      0
      • J Jeremy Likness

        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/

        D Offline
        D Offline
        daveyerwin
        wrote on last edited by
        #3

        Thanks , you have solved my problem and enlightened me on the use of variables . Very much appreciated .

        1 Reply Last reply
        0
        • D daveyerwin

          <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

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups