[2.0] Changes to <xsl:value-of> in XSLT 2.0

In XSLT 2.0, <xsl:value-of> has a separator attribute. If the select attribute is a sequence of items, those items are output in sequence, with the value of the separator attribute between them. We’ll look at a couple of examples that use separator here.

First of all, here’s a short XML document that lists different automobile manufacturers and some of the cars they make:

<?xml version="1.0" encoding="utf-8"?>
<!-- cars.xml -->
<cars>
  <manufacturer name="Chevrolet">
    <car>Cavalier</car>
    <car>Corvette</car>
    <car>Impala</car>
    <car>Malibu</car>
  </manufacturer>
  <manufacturer name="Ford">
    <car>Pinto</car>
    <car>Mustang</car>
    <car>Taurus</car>
  </manufacturer>
  <manufacturer name="Volkswagen">
    <car>Beetle</car>
    <car>Jetta</car>
    <car>Passat</car>
    <car>Touraeg</car>
  </manufacturer>
</cars>

Our first <xsl:value-of> element uses the separator attribute to list all of the manufacturers, separated by commas:

<?xml version="1.0" encoding="utf-8"?>
<!-- value-of_2_0.xsl -->
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:value-of select="cars/manufacturer/@name" separator=", "/>
  </xsl:template>

</xsl:stylesheet>

Transforming our XML document with this stylesheet gives us the following results:

Chevrolet, Ford, Volkswagen

The separator attribute is useful in this case because it is inserted after every value except the last. In XSLT 1.0, we have to do something like this: ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.