• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

XSLT - How to check for last element

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 :

 <PersonConsentGiver>
    <FirstName>First</FirstName>
    <LastName>Last</LastName>
 </PersonConsentGiver>
 <PersonConsentGiver>
    <FirstName>First</FirstName>
    <LastName>Last</LastName>
 </PersonConsentGiver>
<PersonConsentGiver>
    <FirstName>First</FirstName>
    <LastName>Last</LastName>
 </PersonConsentGiver>
 

<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>
</xsl:template>
0
dwmicksch
Asked:
dwmicksch
  • 2
1 Solution
 
Geert BormansInformation ArchitectCommented:
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="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:param name="newline"><xsl:text>
</xsl:text></xsl:param>
    <xsl:template match="Document">
        <xsl:apply-templates select="PersonConsentGiver"/>
    </xsl:template>
    <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"/>
     </xsl:template>
</xsl:stylesheet>

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


Cheers!

Gertone
0
 
Geert BormansInformation ArchitectCommented:
dwmicksch,

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>
     </xsl:template>

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

cheers

Geert
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now