• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • 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 BormansCommented:
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 BormansCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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