general exception
general exception

Reputation: 4342

XSLT comma separation of elements

I have the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car>
        <entrydata columnnumber="4" name="Colour">
            <text>Red</text>
        </entrydata>
    </car>
    <car>
        <entrydata columnnumber="4" name="Colour">
            <textlist>
                <text>Yellow</text>
                <text>Blue</text>
            </textlist>
        </entrydata>
    </car>
</cars>

And the following XSLT stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com: xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <records>
      <xsl:apply-templates select="//cars"/>
    </records>
  </xsl:template>
  <!--Top level template -->
  <xsl:template match="cars">
    <!-- Loop through each document (viewentry) and apply create the rows for each one-->
    <xsl:for-each select="car">
      <record>
        <xsl:attribute name="Colour">
          <xsl:value-of select="entrydata[@name='Colour']"/>
        </xsl:attribute>
       </record>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

This produces the following output:

<?xml version="1.0" encoding="UTF-8"?>
<records>
    <record Colour="Red"/>
    <record Colour="YellowBlue"/>
</records>

How would I modify the XSLT file so that the output becomes (note the comma separation of <textlist>):

<?xml version="1.0" encoding="UTF-8"?>
<records>
    <record Colour="Red"/>
    <record Colour="Yellow, Blue"/>
</records>

Upvotes: 2

Views: 2370

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243599

A not so verbose, pure "push style" XSLT 1.0 solution that doesn't use a hardcoded string for the name of the generated attribute:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="cars">
     <records>
       <xsl:apply-templates/>
     </records>
 </xsl:template>
 <xsl:template match="car">
  <record>
   <xsl:apply-templates/>
  </record>
 </xsl:template>
 <xsl:template match="entrydata">
  <xsl:attribute name="{@name}">
   <xsl:apply-templates/>
  </xsl:attribute>
 </xsl:template>
 <xsl:template match="text">
  <xsl:if test="position() >1">, </xsl:if>
  <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<cars>
    <car>
        <entrydata columnnumber="4" name="Colour">
            <text>Red</text>
        </entrydata>
    </car>
    <car>
        <entrydata columnnumber="4" name="Colour">
            <textlist>
                <text>Yellow</text>
                <text>Blue</text>
            </textlist>
        </entrydata>
    </car>
</cars>

the wanted, correct result is produced:

<records>
   <record Colour="Red"/>
   <record Colour="Yellow, Blue"/>
</records>

Explanation:

Proper use of templates, pattern matching, AVTs and the position() function.


II. A simpler, XSLT 2.0 solution

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="cars">
     <records>
       <xsl:apply-templates/>
     </records>
 </xsl:template>
 <xsl:template match="car">
  <record>
   <xsl:apply-templates/>
  </record>
 </xsl:template>
 <xsl:template match="entrydata">
  <xsl:attribute name="{@name}">
   <xsl:value-of select=".//text" separator=", "/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the same XML document (above), the same correct result is produced:

<records>
   <record Colour="Red"/>
   <record Colour="Yellow, Blue"/>
</records>

Explanation:

Proper use of templates, pattern matching, AVTs and the separator attribute of xsl:value-of

Upvotes: 2

Sean B. Durkin
Sean B. Durkin

Reputation: 12729

This XSLT 1.0 style-sheet will do the trick ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <records>
      <xsl:apply-templates select="*/*"/>
    </records>
  </xsl:template>

  <xsl:template match="car">
   <xsl:variable name="colour-list">
     <xsl:for-each select="entrydata[@name='Colour']/text |
                           entrydata[@name='Colour']/textlist/text">
       <xsl:value-of select="concat(.,', ')" />
     </xsl:for-each>  
   </xsl:variable>  
      <record Colour="{substring($colour-list,1,string-length($colour-list)-2)}"/>
  </xsl:template>
</xsl:stylesheet>

But the winner for the simplest solutions is this XSLT 2.0 stylesheet ...

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
 <records>
  <xsl:apply-templates select="*/*"/>
 </records>
</xsl:template>

<xsl:template match="car">
 <record Colour="{string-join(entrydata[@name='Colour']/(text | textlist/text),', ')}"/>
</xsl:template>
</xsl:stylesheet>

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167716

With XSLT 2.0 you could use

<record Colour="{string-join(entrydata[@name='Colour']/textlist/text, ', ')}"/>

With XSLT 1.0 I would do

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com: xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/">
    <records>
      <xsl:apply-templates select="//cars"/>
    </records>
  </xsl:template>
  <!--Top level template -->
  <xsl:template match="cars/car">
    <record>
        <xsl:attribute name="Colour">
          <xsl:apply-templates select="entrydata[@name='Colour']textlist/text"/>
        </xsl:attribute>
       </record>
  </xsl:template>
  <xsl:template match="textlist/text">
    <xsl:if test="position() > 1">
      <xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions