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 | Calendar | 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 April 14th, 2007, 04:42 AM
Registered User
 
Join Date: Apr 2007
Location: Pune, Maharastra, India.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help to solve problem

I am new to xslt and i have to find the solution of the following problem
I want to use the value of tag that is coming in last among many number of same tag.
 for example
 <abc>
    <xyz>Hello</xyz>
    <pqr>xxx</pqr>
    <xyz>How</xyz>
    <lmn>yyy</lmn>
    <xyz>you</xyz>
    <pqr>zzz</pqr>
    <lmn>yyy</lmn>
 </abc>
I want to store the value of last xyz tag (which value is you) into a variable in xslt.What will we the code for this in xslt.The position of tags are not fix.
Thanks in Advance

pankaj
Reply With Quote
  #2 (permalink)  
Old April 14th, 2007, 05:53 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:variable name="last-xyz" select="string(xyz[last()])"/>

(When you say "the value of the xyz tag", I assume you mean the value of the xyz *element*. Elements have two tags, a start tag and and end tag, and if the "value of a tag" means anything, then the value is "xyz".)

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old April 16th, 2007, 01:05 AM
Registered User
 
Join Date: Apr 2007
Location: Pune, Maharastra, India.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhkay

Thanks a lot.My problem has solved.

pankaj
Reply With Quote
  #4 (permalink)  
Old April 18th, 2007, 02:20 AM
Registered User
 
Join Date: Apr 2007
Location: Pune, Maharastra, India.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mhkay
Actually my proble has not solved.
The code suggested by you is not working fine according to my new requirement.The requirement is as : -
 <abc>
    <xyz>Hello</xyz>
    <xyz>mhkay</xyz>
    <pqr>xxx</pqr>
    <xyz>How</xyz>
    <lmn>yyy</lmn>
    <xyz>you</xyz>
    <pqr>zzz</pqr>
    <lmn>yyy</lmn>
 </abc>
Code suggested by u is giving result as 'mhkay' where as i want result as 'you'.Could u tell how to get it.
Thanks in Advance

pankaj
Reply With Quote
  #5 (permalink)  
Old April 18th, 2007, 06:25 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please show a complete (but small) example that demonstrates your problem so I can see what you are doing wrong.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old April 19th, 2007, 07:27 AM
Registered User
 
Join Date: Apr 2007
Location: Pune, Maharastra, India.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I got the solution of this problem by using following lines of code

<xsl:variable name="last-xyz">
     <xsl:for-each select="//xyz">
          <xsl:if test="position()=last()">
                <xsl:value-of select="."/>
          </xsl:if>
      </xsl:for-each>
</xsl:variable>



pankaj
Reply With Quote
  #7 (permalink)  
Old April 19th, 2007, 07:43 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 fail to see how Michael's solution cannot work, I ran this stylesheet against your example and got the expected answer:
Code:
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/abc">
    <xsl:variable name="xyz-last" select="string(xyz[last()])"/>
    <example>
      <xsl:value-of select="$xyz-last"/>
    </example>
  </xsl:template>
</xsl:stylesheet>
--

Joe (Microsoft MVP - XML)
Reply With Quote
  #8 (permalink)  
Old April 19th, 2007, 08:27 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your 9 lines of code are equivalent to

<xsl:variable name="last-xyz" select="string((//xyz)[last()])"/>

The only difference from my previous solution is the use of "//"; presumably when you tried my solution it didn't work because you were starting from the wrong context node.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
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
Please help me to solve these two problem khatu_jec C# 4 January 3rd, 2008 08:41 AM
Will you please help me to solve this problem....? vis_kapadia Excel VBA 2 November 22nd, 2006 04:57 AM
Solve the problem chandan_tandon General .NET 0 October 4th, 2006 07:00 AM
Solve my problem first? wakil C# 3 February 9th, 2006 08:21 AM
could you solve my problem? Robin1 Javascript 0 September 13th, 2004 10:13 PM



All times are GMT -4. The time now is 08:30 AM.


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