[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] function last() not matching the last element tag


Subject: [xsl] function last() not matching the last element tag
From: Ross Buchanan <ross-buchanan@xxxxxxxxxx>
Date: Fri, 26 Jan 2007 10:09:03 -0600

I am running into a problem where I need to find the first paragraph element of a document and the last paragraph element to give a unique class to each. I am able to find the first paragraph with <xsl:template match="/doc/p[1]"> but am not able to match the last element with <xsl:template match="/doc/p[last()]">.

[]$ xsltproc --version
Using libxml 20616, libxslt 10111 and libexslt 809
xsltproc was compiled against libxml 20614, libxslt 10111 and libexslt 809
libxslt 10111 was compiled against libxml 20614
libexslt 809 was compiled against libxml 20614

Simple XML File:
<?xml version="1.0" encoding="utf-8"?>
<doc>
<p>1 Paragraph 1</p>
<p>2 Paragraph 2</p>
<p>3 Paragraph 3</p>
</doc>

XSLT File:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/doc">
 <html><head><title>The Title</title></head><body>
   <xsl:apply-templates />
 </body></html>
</xsl:template>

<xsl:template match="/doc/p">
 <p class="p"><xsl:value-of select="." /></p>
</xsl:template>

<xsl:template match="/doc/p[1]">
 <p class="p firstP"><xsl:value-of select="." /></p>
</xsl:template>

<xsl:template match="/doc/p[last()]">
 <p class="p lastP"><xsl:value-of select="." /></p>
</xsl:template>

</xsl:stylesheet>

Output Desired:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Title</title>
</head>
<body>
<p class="p firstP">1 Paragraph 1</p>
<p class="p">2 Paragraph 2</p>
<p class="p lastP">3 Paragraph 3</p>
</body>
</html>

Output Occuring:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The Title</title>
</head>
<body>
<p class="p firstP">1 Paragraph 1</p>
<p class="p">2 Paragraph 2</p>
<p class="p">3 Paragraph 3</p>
</body>
</html>


Am I using last() incorrectly? I have found a few sites explaining to search for the last element in the way that I am so I am a bit confused.


Thanks,
Ross


Current Thread
Keywords
xml