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 September 5th, 2008, 04:12 PM
Authorized User
 
Join Date: Sep 2008
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>
 
Old September 5th, 2008, 05:13 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old September 6th, 2008, 04:45 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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)
 
Old September 8th, 2008, 09:26 AM
Authorized User
 
Join Date: Sep 2008
Posts: 56
Thanks: 0
Thanked 1 Time in 1 Post
Default

thanks Kay it worked

 
Old September 8th, 2008, 10:05 AM
Authorized User
 
Join Date: Sep 2008
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>

 
Old September 8th, 2008, 12:13 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old September 9th, 2008, 08:12 AM
Authorized User
 
Join Date: Sep 2008
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.






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





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