Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

last Function (XPath)

Returns a number equal to context size of the expression evaluation context.

          number last()

The following finds the last <author> child of each <book> element:


In this example we illustrate how to use the last() function to select the last y elment contained in each x element of the following XML document.

XML FILE (test.xml)

<?xml version="1.0"?>
<!DOCTYPE test [
  <!ELEMENT test (x+)>
  <!ELEMENT x (x+| y+)>
     a ID #REQUIRED>
    <x a="a11">
      <x a="a21">
        <x a="a31">
    <x a="a12">
      <x a="a22">
    <x a="a13">
    <x a="a14">

XSLT File (test.xsl)

Pay attention to the instruction in bold in this XSLT stylesheet.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"

  <xsl:output method="xml" omit-xml-declaration="yes"/>

  <!-- Suppress text nodes not covered in subsequent  
      template rule -->
  <xsl:template match="text()"/>

  <xsl:template match="*">
    <xsl:element name="{name()}">
      <xsl:apply-templates select="*|@*"/>
      <xsl:if test="text()">
         <xsl:value-of select="."/>

  <xsl:template match="@*">
    <xsl:attribute name="{name()}">
      <xsl:value-of select="."/>

  <xsl:template match="/test">
    <xsl:apply-templates select="//x/y[last()]"/>



The XSLT stylesheet, when applied to the XML file above results in the following node-set:





Build Date:

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.