I found a similar question to mine, but couldn't figure out a way for my issue.
I have an XML as follows
<name>
<text class="002. AB vs BC"> Sample</text>
<text class="003. DC vs BC"> Sample</text>
<text class="004. CD vs BC"> Sample</text>
<text class="005. AB vs BC"> Sample</text>
<text class="006. AB vs BC"> Sample</text>
<text class="007. EF vs BC"> Sample</text>
<text class="008. CD vs BC"> Sample</text>
<text class="009. DC vs BC"> Sample</text>
<text class="010. AB vs BC"> Sample</text>
<text class="011. EF vs BC"> Sample</text>
<text class="012. AB vs BC"> Sample</text>
</name>
And I need to group all the nodes with similar first word in class attribute as follows
<name>
<group name="AB">
<text class="002. AB vs BC"> Sample</text>
<text class="005. AB vs BC"> Sample</text>
<text class="006. AB vs BC"> Sample</text>
<text class="010. AB vs BC"> Sample</text>
<text class="012. AB vs BC"> Sample</text>
</group>
<group name="EF">
<text class="007. EF vs BC"> Sample</text>
<text class="011. EF vs BC"> Sample</text>
</group>
<group name="CD">
<text class="008. CD vs BC"> Sample</text>
<text class="004. CD vs BC"> Sample</text>
</group>
<group name="DC">
<text class="003. DC vs BC"> Sample</text>
<text class="009. DC vs BC"> Sample</text>
</group>
</name>
How to achieve this?
This is a pretty straightforward grouping problem.
If you're limited to XSLT 1.0, you need to use Muenchian Grouping.
If you're using XSLT 2.0+, you can use xsl:for-each-group.
Examples...
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="class" match="text"
use="substring-before(substring-after(normalize-space(@class), ' '),' ')"/>
<!--identity template-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/name">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each select="text[count(.|key('class', substring-before(substring-after(normalize-space(@class), ' '),' '))[1])=1]">
<xsl:variable name="key" select="substring-before(substring-after(normalize-space(@class), ' '),' ')"/>
<group name="{$key}">
<xsl:apply-templates select="key('class',$key)"/>
</group>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Fiddle: http://xsltfiddle.liberty-development.net/94hvTyR
XSLT 3.0 (you can make this valid 2.0 if you replace the xsl:mode
with the identity template from the 1.0 stylesheet)
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="name">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="text"
group-by="tokenize(normalize-space(@class),'\s+')[2]">
<group name="{current-grouping-key()}">
<xsl:apply-templates select="current-group()"/>
</group>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Fiddle: http://xsltfiddle.liberty-development.net/94hvTyR/1
Note: The output does not have the same order as your example, but I didn't see any logic to the ordering.
With XSLT-2.0 you can use xsl:for-each-group
with these templates:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="text()" />
<!-- identity template -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="name">
<xsl:copy>
<xsl:for-each-group select="text" group-by="tokenize(@class,' ')[2]">
<group name="{current-grouping-key()}">
<xsl:for-each select="current-group()">
<xsl:copy-of select="." />
</xsl:for-each>
</group>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Output is:
<name>
<group name="AB">
<text class="002. AB vs BC"> Sample</text>
<text class="005. AB vs BC"> Sample</text>
<text class="006. AB vs BC"> Sample</text>
<text class="010. AB vs BC"> Sample</text>
<text class="012. AB vs BC"> Sample</text>
</group>
<group name="DC">
<text class="003. DC vs BC"> Sample</text>
<text class="009. DC vs BC"> Sample</text>
</group>
<group name="CD">
<text class="004. CD vs BC"> Sample</text>
<text class="008. CD vs BC"> Sample</text>
</group>
<group name="EF">
<text class="007. EF vs BC"> Sample</text>
<text class="011. EF vs BC"> Sample</text>
</group>
</name>