I've the below XML.
<?xml version="1.0" encoding="UTF-8"?>
<main.line>
<main>
<name>Mohamad</name>
<job.title prefix="no">B</job.title>
</main>
<main>
<name>David</name>
<job.title prefix="no">B</job.title>
</main>
<main>
<name>Hashim</name>
<job.title prefix="no">B</job.title>
</main>
<main>
<name>Anthony</name>
<job.title prefix="no">C</job.title>
</main>
</main.line>
Here i'm trying to print the name and job.title in the below way.
If the job.title
is same and there are more than two similar occurrences, the name has to be print with a comma
and the last name should be printed with an and
along with the job.title
concatenated with A
at start.(This is a bit confusing but my output below will make this clear).
If there are two cases then it should be printed with and
but no comma
If there is no such case, the name should be printed directly along with the job.title
.
The desired output is as below.
Mohamad, David and Hashim BA, Anthony C
The XSLT I tried is as below.
<xsl:template match="main.line">
<xsl:for-each select="main ">
<xsl:choose>
<xsl:when test="./job.title=following-sibling::main/job.title">
<xsl:choose>
<xsl:when test="./job.title='A' or 'B' or 'C' or 'D' or 'E'">
<xsl:value-of select="./name"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="following-sibling::main[1]/name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="./job.title"/>
<xsl:text>A</xsl:text>
<xsl:text> </xsl:text>
</xsl:when>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="not(position() = last()) and fn:not(fn:position()=1)">
<xsl:text>, </xsl:text>
</xsl:when>
<xsl:when test="position()=last() and fn:not(fn:position()=1)">
<xsl:text> and </xsl:text>
</xsl:when>
</xsl:choose>
<xsl:value-of select="name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="job.title"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
But the output that i get currently is
Mohamad, David BA David, Hashim BA , Hashim B and Anthony C.
please let me know where did i go wrong and how i can get the desired output.
Thanks