Share via


Example 2 of <xsl:if> 

Another way to separate the names by commas is to check a name to see if it is the first. In some circumstances this performs better than Example 1 of <xsl:if> , because the last() function requires that the entire set of names be found and counted, while this approach does not.

XML File (names.xml)

Use the names.xml listing in Example 1 of <xsl:if>, but edit the href attribute to point to ifcomma2.xsl.

XSLT File (ifcomma2.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="namelist/name">
  <xsl:if test="position()!=1">, </xsl:if>
  <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

Albert, Terrance, Will, Sylvia, Timothy, Gordon, James, Robert, Dan, Sasha

This is the processor output:

<?xml version="1.0" encoding="UTF-16"?>Albert, Terrance, Will, Sylvia, Timothy, Gordon, James, Robert, Dan, Sasha