Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 5th, 2008, 04:12 PM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default Extracting ancestor by providing the attribute

Here is my xml

<node id="fkh489a3">
    <node id="fkh489e4">
     <label key="">John</label>
    </node>
    <node id="fkh489e5">
     <label key="">Charles</label>
    </node>
    <node id="fkh489e6">
     <label key="">Smith</label>
    </node>
</node>

I have one of the //node/node id specified which is fkh489e4. How can I use this id to print the label (John, Charles, Smith)?
This is what im trying right now and it only prints John but not the rest of the labels can someone tell me what im doing wrong and how to print all the labels? I must print the labels using the ID I have. thanks.


<xsl:for-each select="//node/node[@id = 'fkh489e4']/preceding-sibling::*">
<br/>
<xsl:value-of select="/node/label"/><br/>

</xsl:for-each>
Reply With Quote
  #2 (permalink)  
Old September 5th, 2008, 05:13 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I don't see how this code can possibly "print" anything since (a) there is no element selected by /node/label (that's an absolute path expression that selects from the root of the tree), and (b) the node with id fkh489e4 has no preceding siblings.

It seems to me you want

<xsl:for-each select="//node[node/@id ='fkh489e4']/node">
<br/>
<xsl:value-of select="label"/><br/>

That should work on this example, but you haven't explained the general problem very well so I might have misunderstood your intentions.



Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old September 6th, 2008, 04:45 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I added an element to your XML:
Code:
<data>
  <node id="fkh489a3">
    <node id="fkh489e4">
      <label key="">John</label>
    </node>
    <node id="fkh489e5">
      <label key="">Charles</label>
    </node>
    <node id="fkh489e6">
      <label key="">Smith</label>
    </node>
  </node>
</data>
Then this XSLT does what you want:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="id" select="'fkh489e4'" />
  <xsl:template match="/*">
    <name>
      <xsl:for-each select="node[node[@id = $id]]">
        <xsl:apply-templates select="node" />
      </xsl:for-each>
    </name>
  </xsl:template>

  <xsl:template match="node/node">
    <xsl:value-of select="label"/>
    <xsl:if test="position() != last()">
      <xsl:text>, </xsl:text>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #4 (permalink)  
Old September 8th, 2008, 09:26 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

thanks Kay it worked

Reply With Quote
  #5 (permalink)  
Old September 8th, 2008, 10:05 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

Can I do this ? whats the correct syntax? so I can put pageLink2 in document.write

<script type="text/javascript">
var pageLink2="abc1234";
document.write(<xsl:for-each select="//node/node[@id ='"+pageLink2+"']">
<xsl:value-of select="label"/>
</xsl:for-each>);
</script>

Reply With Quote
  #6 (permalink)  
Old September 8th, 2008, 12:13 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You seem to be muddled about whether you are nesting XPath inside Javascript or vice versa. The obvious answer is that

<xsl:for-each select="//node/node[@id ='"+pageLink2+"']">

isn't well-formed XML so you can't write it. But more subtly, you are generating a <script> element in the target HTML page, which will execute when the page loads and perhaps when the user clicks on some button, and yet you seem to be trying to use the value of the Javascript variable during the transformation. If I have misunderstood what you are trying to do, sorry - it's hard to guess what people intended when the syntax is incorrect.

Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
Reply With Quote
  #7 (permalink)  
Old September 9th, 2008, 08:12 AM
Authorized User
 
Join Date: Sep 2008
Location: , , .
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

no problem I didnt think it was possible I will have to approach this problem differently. thanks again.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to print the label by providing the value? eruditionist XSLT 1 September 29th, 2008 12:26 PM
Extracting a number range from tag attribute group maikm XSLT 6 August 25th, 2008 04:54 PM
extracting attribute value hag XSLT 1 March 3rd, 2008 03:16 AM
parent/ancestor nmnm XSLT 1 April 24th, 2007 05:25 AM
Extracting Subset and Modifying Attribute fauster XSLT 1 September 6th, 2006 08:41 AM



All times are GMT -4. The time now is 09:55 PM.


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