Following is the XML piece of code –
<groups>
<group i=1>
<member t="P.M" c="Y">ABC</member>
<member t="P.L">PQR</member>
<member t="M">XYZ</member>
</group>
<group i=2>
<member t="M" c="Y">ABC</member>
<member t="M">PQR</member>
</group>
<group i=3>
<member t="P.L" c="Y">ABC</member>
<member t="M">PQR</member>
<member t="M">XYZ</member>
</group>
<group i=4>
<member t="M">ABC</member>
<member t="M" c="Y">PQR</member>
</group>
<group i=5>
<member t="M">ABC</member>
<member t="M" c="Y">PQR</member>
<member t="M" c="Y">XYZ</member>
</group>
<group i=6>
<member t="M" dec="Y">ABC</member>
</group>
</groups>
Desired HTML output using XSLT 1.0 –
<U>ABC</U>, P.M, PQR, P.L and XYZ, M
<U>ABC</U> and PQR, MM
<U>ABC</U>, P.L, PQR and XYZ, MM
ABC and <U>PQR</U>, MM
ABC, <U>PQR</U> and <U>XYZ</U>, MM
<U>ABC</U>, M
The partial XSLT solution for the above output is –
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='html' media-type='text/html'/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="group">
<p>
<xsl:apply-templates select="@*|node()"/>
</p>
</xsl:template>
<xsl:template match="member">
<xsl:choose>
<xsl:when test='@c = "Y"'>
<u><xsl:value-of select="."/></u>, <xsl:value-of select='@t'/>
</xsl:when>
<xsl:otherwise>
<b><xsl:value-of select="."/></b>, <xsl:value-of select='@t'/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="member[following-sibling::member]">
<xsl:choose>
<xsl:when test='@c = "Y"'>
<u><xsl:value-of select="."/></u>, <xsl:value-of select='@t'/>
<xsl:text> and </xsl:text>
</xsl:when>
<xsl:otherwise>
<b><xsl:value-of select="."/></b>, <xsl:value-of select='@t'/>
<xsl:text> and </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="member[following-sibling::member[following-sibling::member]]">
<xsl:choose>
<xsl:when test='@c = "Y"'>
<u><xsl:value-of select="."/></u>, <xsl:value-of select='@t'/>
<xsl:text>, </xsl:text>
</xsl:when>
<xsl:otherwise>
<b><xsl:value-of select="."/></b>, <xsl:value-of select='@t'/>
<xsl:text>, </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The above XSLT is giving the output in correct format as required, but if the <member>
are of same type then how to add this type at the end..?
In answer to your immediate question though, to output the correct number of t elements for a member, you could do something like this:
So, it will output a value for each matching @t attribute.
However, I think you need to read up more on grouping, as Mr. Michael Kay suggested in your last question.
In this case, you are grouping by a group attributeand a member attribute. So, you would define a key, like so
Do note the use of the pipe | to concatenate the two parts of the key. You would need to pick a character that could not occur in either part of the key.
Then, you can get the first element of each group like so
And to iterate over all the elements in the group, you can then do this…
So, given the following XML
Using the following XSLT….
The following is output….