XSLT - How to check for last element

Posted on 2006-04-15
Last Modified: 2010-07-27
I need a method of checking for the last element in an xml document. For example, here you can see I have three elements of type "PersonConsentGiver." I need an XSL method to check to see if it is the last PersonConsentGiver in the list to make sure that I don't put a comma at the end of the last persons name. My XSLT that I am using is written below the XML :


<xsl:template match="Document/PersonConsentGiver" mode="ConsentGivers">
<xsl:value-of select="FirstName"/> <xsl:text> (</xsl:text><xsl:value-of select="LastName"/> <xsl:text>), </xsl:text>
Question by:dwmicksch
    LVL 60

    Expert Comment

    by:Geert Bormans
    Hi dwmicksch,

    you could use functions like position() and last()
    but the way your template is currently set up, those functions would count the whitespace text nodes as well

    if you just pass the used nodeset to the PersonConsentGiver template
    it would not count the white-space nodes, and you would have correct counts
    you can do that with a selective apply-templates

    here is an example
    (I add newlines because you loose them in the selective apply-templates)

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="" version="1.0">
        <xsl:param name="newline"><xsl:text>
        <xsl:template match="Document">
            <xsl:apply-templates select="PersonConsentGiver"/>
        <xsl:template match="PersonConsentGiver">
            <xsl:value-of select="FirstName"/> <xsl:text> (</xsl:text><xsl:value-of select="LastName"/> <xsl:text>)</xsl:text>
            <xsl:if test="not(position() = last())"><xsl:text>,</xsl:text></xsl:if>
            <xsl:value-of select="$newline"/>

    you see, if you pass the correct nodeset, it is just a simple test to add the comma


    LVL 60

    Accepted Solution


    there is a less intrusive solution,
    that checks the following-sibling path

        <xsl:template match="Document/PersonConsentGiver">
            <xsl:value-of select="FirstName"/> <xsl:text> (</xsl:text><xsl:value-of select="LastName"/> <xsl:text>)</xsl:text>
            <xsl:if test="following-sibling::PersonConsentGiver"><xsl:text>,</xsl:text></xsl:if>

    I think you now have the two options that are used the most



    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now