I have a XML similar to this
<profiles>
<profile>
<customer>customer a </customer>
<collateral>
<summary>summary a</summary>
<related>
<solutions>sol1,sol2</solutions>
</related>
<collateral>
</profile>
<profile>
<customer>customer b </customer>
<collateral>
<summary>summary b</summary>
<related>
<solutions>sol1</solutions>
</related>
<collateral>
</profile>
<profile>
<customer>customer c </customer>
<collateral>
<summary>summary c</summary>
<related>
<solutions>sol2,sol3</solutions>
</related>
<collateral>
</profile>
</profiles>
Desired output
<div id="#sol1">
customer a,summary a
customer b, summary b
</div>
<div id="#sol2">
customer a,summary a
customer c,summary c
</div>
..............
Iam aware of Muenchian way of grouping, but not sure how I can accomplish, if I have comma separated groub-by element values. Any help will be appreciated.
While this is straight-forward in XSLT 2.0, in XSLT a two-pass transformation can produce the wanted results:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kSolByVal" match="solution" use="."/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:apply-templates/>
</xsl:variable>
<xsl:apply-templates select=
"ext:node-set($vrtfPass1)
/*/*/*/*
/solutions/solution
[generate-id()
=
generate-id(key('kSolByVal', .)[1])
]"
mode="pass2"/>
</xsl:template>
<xsl:template mode="pass2" match="solution">
<div id="#{.}">
<xsl:apply-templates mode="pass2"
select="key('kSolByVal', .)/../../../.."/>
</div>
</xsl:template>
<xsl:template match="profile" mode="pass2">
<xsl:if test="position() = 1">
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:value-of select=
"concat(customer, ', ', */summary, '
')"/>
</xsl:template>
<xsl:template match="solutions">
<solutions>
<xsl:apply-templates select="." mode="split"/>
</solutions>
</xsl:template>
<xsl:template match="solutions" name="split" mode="split">
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
<xsl:variable name="vText1"
select="concat($pText, ',')"/>
<xsl:variable name="vPart" select=
"substring-before($vText1, ',')"/>
<solution>
<xsl:value-of select="$vPart"/>
</solution>
<xsl:call-template name="split">
<xsl:with-param name="pText"
select="substring($pText, string-length($vPart)+2)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document (corrected for well-formedness):
<profiles>
<profile>
<customer>customer a </customer>
<collateral>
<summary>summary a</summary>
<related>
<solutions>sol1,sol2</solutions>
</related>
</collateral>
</profile>
<profile>
<customer>customer b </customer>
<collateral>
<summary>summary b</summary>
<related>
<solutions>sol1</solutions>
</related>
</collateral>
</profile>
<profile>
<customer>customer c </customer>
<collateral>
<summary>summary c</summary>
<related>
<solutions>sol2,sol3</solutions>
</related>
</collateral>
</profile>
</profiles>
the wanted, correct result is produced:
<div id="#sol1">
customer a , summary a
customer b , summary b
</div>
<div id="#sol2">
customer a , summary a
customer c , summary c
</div>
<div id="#sol3">
customer c , summary c
</div>
Explanation:
We carry out the transformation in two passes. Pass2 is applied on the result of applying Pass1 on the provided XML document.
Pass 1 is essentially the identity rule overriden for any solutions
element. The processing of a solutions
element consists in recursive splitting of its string value. The final result of Pass1 is the following:
--
<profiles>
<profile>
<customer>customer a </customer>
<collateral>
<summary>summary a</summary>
<related>
<solutions>
<solution>sol1</solution>
<solution>sol2</solution>
</solutions>
</related>
</collateral>
</profile>
<profile>
<customer>customer b </customer>
<collateral>
<summary>summary b</summary>
<related>
<solutions>
<solution>sol1</solution>
</solutions>
</related>
</collateral>
</profile>
<profile>
<customer>customer c </customer>
<collateral>
<summary>summary c</summary>
<related>
<solutions>
<solution>sol2</solution>
<solution>sol3</solution>
</solutions>
</related>
</collateral>
</profile>
</profiles>
.3. We then apply templates (in mode="pass2"
) on the result of Pass1. This is a typical and traditional Muenchian grouping.