We help IT Professionals succeed at work.
Get Started

Extract Last Node in Variable

deleyd
deleyd asked
on
76 Views
Last Modified: 2019-12-16
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>

  <xsl:template match="/">
    <html>
      <body>
      <xsl:variable name="v">
        <xsl:call-template name="HelloWorld"/>
      </xsl:variable>
      <xsl:variable name="x" select="v/*[position() != last()]"/>
      <xsl:copy-of select="$x"/>          
        </body>
    </html>
  </xsl:template>

  <xsl:template name="HelloWorld">
    <p>Hello world!</p>
    <xsl:text>5</xsl:text>
  </xsl:template>
  
</xsl:stylesheet>

Open in new window

I'm trying to strip off the "5" which is appended to the end. My desired output is
<?xml version="1.0" encoding="utf-8"?><html><body><p>Hello world!</p></body></html>

Open in new window

and I would also like to get that "5" into a variable.

The debugger shows variable v is:

Debugger outputand variable x is empty (my select isn't working).

Update: I see I forgot the $. Change that line to:
<xsl:variable name="x" select="$v/*[position() != last()]"/>

Open in new window

and now I get an exception which says, "To use a result tree fragment in a path expression, first convert it to a node-set using the msxsl:node-set()". Great. (I actually used to know this stuff a few years ago! I've forgotten it all! How do I convert this to a node-set?)
Comment
Watch Question
Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE