Example 1 of <xsl:if>

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Reference

Example 1 of <xsl:if>

In this example, the names in a group of names are formatted as a comma-separated list.

Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.

XML File (names.xml)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="ifcomma.xsl" ?>
<namelist>
   <name>Albert</name>
   <name>Terrance</name>
   <name>Will</name>
   <name>Sylvia</name>
   <name>Timothy</name>
   <name>Gordon</name>
   <name>James</name>
   <name>Robert</name>
   <name>Dan</name>
   <name>Sasha</name>
</namelist>

XSLT File (ifcomma.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:apply-templates/>
  <xsl:if test="position()!=last()">, </xsl:if>
</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

See Also

Example 2 of <xsl:if> | Example 3 of <xsl:if> | Example 4 of <xsl:if>