Search code examples
xsltxslt-2.0xslt-grouping

How do I make group-by ignore white-space in XSLT?


I have an xml file which I need to group-by using xsl:for-each-group. Everything works fine but the problem comes from when there is elements which have white space at the end of them (e.g. <word>test </word> and <word>test</word>) but I need these to be considered as one group.

Here's the sample xml file:

<u>
  <s>
    <w>this </w>
    <w>is </w>
    <w>a </w>
    <w>test </w>
  </s>
  <s>
    <w>this</w>
    <w>is</w>
    <w>a</w>
    <w>test</w>
  </s>
<u>

Here's the xslt code

<xsl:for-each-group select="bncDoc/stext/div/u/s" group-by="w" >
  <tr>  
    <td style="text-align: center;">
      <xsl:value-of select="current-grouping-key()"/>
    </td>
    <td>
      <xsl:value-of select="count(current-group())"/>
    </td>
  </tr>
</xsl:for-each-group>

Is there any workaround for this?


Solution

  • <xsl:for-each-group select="bncDoc/stext/div/u/s/w" group-by="normalize-space()">
       <!-- ... -->
    </xsl:for-each-group>