Grouping Items using XSLT

Filed under: Technology — Tags: , , , , , , — Developer42 @ 12:16

Another XSLT problem. How do you group items together using XSLT, when the sort function isn’t available (e.g. the Muenchian Method won’t work).


<xsl:stylesheet version="2.0"  xmlns:xsl="">
  <xsl:output method="xml" indent="yes" encoding="utf-8" /> <!-- keeping utf 8 rather than 16 as this will be big -->
  <xsl:strip-space elements="*"/>

  <!-- ROOT -->
  <xsl:template match="/node()">
      <xsl:copy-of select="@*"/>
      <xsl:for-each select="./node()[not(journalName = preceding-sibling::Row/journalName)]/node()[local-name()=&quot;journalName&quot;]"><!-- for each distinct JournalName -->
        <xsl:variable name="journalName"><xsl:value-of select="."/></xsl:variable>
          <xsl:copy-of select="."/>
          <xsl:apply-templates select="//node()[local-name()=&quot;Row&quot; and journalName=$journalName]" />

  <!-- By default, copy everything as is -->
  <xsl:template match="@* | node()">
      <xsl:apply-templates select="@* | node()"/>
  <!-- but as we already have the journal name, strip it -->
  <xsl:template match="node()[local-name()=&quot;journalName&quot;]" />
  <!-- and demo that we could manipulate other elements if needed -->
  <xsl:template match="node()[local-name()=&quot;otherData&quot;]">
      <xsl:copy-of select="@*" />
      <xsl:attribute name="newAttribute">Demo</xsl:attribute>
      <xsl:value-of select="."/>

Sample Data


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: