<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:pa="urn:Applyonline-Product-Attributes"
xmlns:ms="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="pa ms"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="/LenderParam/Products/Product"/>
</xsl:template>
<xsl:template match="/LenderParam/Products/Product">
<xsl:apply-templates select="Product"/>
</xsl:template>
<xsl:template match="Product">
<xsl:variable name="parentProduct" select="@display"/>
<!-- first get all values and sort them -->
<xsl:variable name="f1">
<xsl:for-each select="pa:RepaymentFrequency">
<xsl:sort select="@value"/>
<Freq>
<xsl:attribute name="value">
<xsl:value-of select="@value"/>
</xsl:attribute>
</Freq>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="f1ns" select="ms:node-set($f1)" />
<!-- now filter out duplicates -->
<xsl:variable name="f2">
<xsl:for-each select="$f1ns/Freq">
<xsl:choose>
<!-- if the sibiling before matches then dont add it, this works because we sorted the list so all duplicates will follow one another -->
<xsl:when test="position() != 1">
<xsl:if test="@value != preceding-sibling::*[1]/@value">
<xsl:value-of select="@value"/>
</xsl:if>
</xsl:when>
<!-- the first node has no preceeding sibling so we always add it -->
<xsl:otherwise>
<xsl:value-of select="@value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="f2ns" select="ms:node-set($f2)" />
<xsl:value-of select="$f2ns"/>
<xsl:apply-templates select="Class">
<xsl:with-param name="parentProduct" select="$parentProduct"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="Class">
<xsl:param name="parentProduct"/>
<!-- Only Render this product if it has a malid -->
<xsl:if test="@malid">
<Product>
<xsl:value-of select="$parentProduct"/>
<xsl:text>, </xsl:text><xsl:value-of select="@display"/><xsl:text>-------</xsl:text><xsl:value-of select="@malid"/>
</Product>
</xsl:if>
</xsl:template>
</xsl:stylesheet>