Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old February 15th, 2007, 11:47 AM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select node whose child value matches

I am trying to select a parent node, that has a child node id that matches a specific value. How would I go about doing this? Right now style sheet looks like this:
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <xsl:for-each select="//node">
         <xsl:if test='string(node/guid)=string(9012)'>
            <xsl:value-of select="."/>
         </xsl:if>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>
The data is as follows:
Code:
<node>
   <display>Some Text</display>
   <guid>1234</guid>
   <node>
      <display>More text</display>
      <guid>5678</guid>
   </node>
   <node>
      <display>Another node</display>
      <guid>9012</guid>
   </node>
</node>
Thank you!

 
Old February 15th, 2007, 12:21 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:for-each select="//node[guid=9012]">
   <xsl:value-of select="."/>
</xsl:for-each>


When the current node is a <node> element, doing node/guid means child::node/child::guid, and your <node> element doesn't have a child called <node>.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 15th, 2007, 12:23 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I was too quick. Your node does have a child::node. So your code looks OK, except it can be shortened to

<xsl:for-each select="//node[node/guid=9012]">...

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 15th, 2007, 01:11 PM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That doesn't seem to work. I am using XTrans to test all of my XSLT, which uses the Microsoft engine, since I will ultimately be using this in C# code. I should have stated that the node with the matching guid may be at any level. Any other suggestions?

 
Old February 15th, 2007, 01:32 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please supply your complete input, complete code that "isn't working", the output it produces, and the output that you want.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 16th, 2007, 04:54 PM
Registered User
 
Join Date: Feb 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figured it out. I replaced the string(xxx) with single quotes and it works fine now. Thank you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Select correct child node richieWolf XSLT 4 September 24th, 2008 08:53 AM
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 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.