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
 
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
 
Old April 14th, 2007, 05:53 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

<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
 
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
 
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
 
Old April 18th, 2007, 06:25 AM
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

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
 
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
 
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)
 
Old April 19th, 2007, 08:27 AM
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

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




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





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