Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 24th, 2008, 07:10 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select correct child node

I have the follow XML, it can change but this is how I thought it will be structured.

For example, I have a value stored in a variable $VALUE that contains 200. I want check which <LOWER> and <UPPER> tags it falls in.
Once I find the matching one I want to retrieve <NAME>. So here I want to return Stage3. Can this be done in XSL?
I'm not sure how to do it.

      <Stage>
          <NAME>Stage1</NAME>
          <LOWER>0</LOWER>
          <UPPER>114</UPPER>
      </Stage>
      <Stage>
          <NAME>Stage2</NAME>
          <LOWER>115</LOWER>
          <UPPER>150</UPPER>
      </Stage>
       <Stage>
          <NAME>Stage3</NAME>
          <LOWER>151</LOWER>
          <UPPER>999</UPPER>
      </Stage>


  #2 (permalink)  
Old September 24th, 2008, 07:19 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

One XPath expression suffices:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:param name="VALUE" select="200"/>

  <xsl:template match="/">
    <xsl:value-of select="descendant::Stage[number(LOWER) &lt; $VALUE and number(UPPER) &gt; $VALUE]/NAME"/>
  </xsl:template>

</xsl:stylesheet>
--
  Martin Honnen
  Microsoft MVP - XML
  #3 (permalink)  
Old September 24th, 2008, 07:58 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That's really cool, thanks a lot.

Btw, how can I display <= and >= instead of < (&lt;) and > (&gt;)?


  #4 (permalink)  
Old September 24th, 2008, 08:08 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Code:
<xsl:value-of select="descendant::Stage[number(LOWER) &lt;= $VALUE and number(UPPER) &gt;= $VALUE]/NAME"/>
--
  Martin Honnen
  Microsoft MVP - XML
  #5 (permalink)  
Old September 24th, 2008, 08:53 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Of course! Stupid me! Thanks



Similar Threads
Thread Thread Starter Forum Replies Last Post
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Select first child node that matches criteria priya81 XSLT 4 July 3rd, 2007 10:32 AM
Select node whose child value matches Fantmx XSLT 5 February 16th, 2007 04:54 PM
Select parent node when any one child has key pparadesia XSLT 2 January 7th, 2005 11:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.