user5711656
user5711656

Reputation: 3678

how to make group in xslt?

could you please tell me how to make groups in xslt 1.

I am trying to make list alphabetical order with grouping

here is my code

http://xsltransform.net/jxN8Npy

XML

<body>
   <a>d</a>
   <a>c</a>
   <a>d</a>
   <a>c</a>
   <a>abc</a>
   <a>dee</a>
   <a>pu</a>
   <a>gu</a>

</body>

XSLT

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="body">
      <hmtl>
        <head>
          <title>New Version!</title>
        </head>
        <ul>
            <xsl:apply-templates select="a"/> 
        </ul>

      </hmtl>
    </xsl:template>

   <xsl:template match="a">
   <li>
        <xsl:value-of select="."/>
   </li>

   </xsl:template>
</xsl:transform>

Current output

<ul>
      <li>d</li>
      <li>c</li>
      <li>d</li>
      <li>c</li>
      <li>abc</li>
      <li>dee</li>
      <li>pu</li>
      <li>gu</li>
   </ul>

Expected output

<ul>
  <li> A---D</li>
   <li>abc</li>
    <li>c</li>
    <li>cb</li>
     <li>d</li>
     <li>dee</li>
    <li> E---I</li>
      <li>gu</li>
   <li> J---P</li>
      <li>pu</li>
   <li> Q---Z</li>
   </ul>

could you please tell me how to make group

Upvotes: 1

Views: 149

Answers (1)

Christian Mosz
Christian Mosz

Reputation: 543

This gave me your desired output:

<xsl:variable name="lettersAD" select="'ABCDabcd'"/>
<xsl:variable name="lettersEI" select="'EFGHIefghi'"/>
<xsl:variable name="lettersJP" select="'JKLMNOPjklmnop'"/>
<xsl:variable name="lettersQZ" select="'QRSTUVWXYZqrstuvwxyz'"/>

<xsl:template match="a">
    <li><xsl:value-of select="."/></li>
</xsl:template>

<xsl:template match="/">
    <ul>
        <li>A---D</li>
        <xsl:apply-templates select="//a[contains($lettersAD, substring(.,1,1))]">
            <xsl:sort select="text()" data-type="text"/>
        </xsl:apply-templates>
        <li>E---I</li>
        <xsl:apply-templates select="//a[contains($lettersEI, substring(.,1,1))]">
            <xsl:sort select="text()" data-type="text"/>
        </xsl:apply-templates>
        <li>J---P</li>
        <xsl:apply-templates select="//a[contains($lettersJP, substring(.,1,1))]">
            <xsl:sort select="text()" data-type="text"/>
        </xsl:apply-templates>
        <li>Q---Z</li>
        <xsl:apply-templates select="//a[contains($lettersQZ, substring(.,1,1))]">
            <xsl:sort select="text()" data-type="text"/>
        </xsl:apply-templates>
    </ul>
</xsl:template>

There is probably an easier way to archive this though.

Upvotes: 1

Related Questions