Below is my XML code –
<Para>
<Desc>....</Desc>
<References>
<BookRef>
<BookName>ABC of HTML</BookName>
<Chapter>1</Chapter>
<BookName>HTML : The Complete Reference</BookName>
<Chapter>1</Chapter>
</BookRef>
</References>
</Para>
<Para>
<Desc>....</Desc>
<References>
<BookRef>
<BookName>ABC of XML</BookName>
<Chapter>2</Chapter>
<BookName>XML : The Complete Reference</BookName>
<Chapter>10</Chapter><Chapter>11</Chapter>
</BookRef>
</References>
</Para>
I need to display the above in tabular format using HTML Table Tag. So that it should look like –
Description of Paragraph (the text between the Desc tags)
**Book Name** **Chapters**
ABC of HTML 1
HTML: The Complete Reference 1
Description of Paragraph (the text between the Desc tags)
**Book Name** **Chapters**
ABC of XML 2
HTML: The Complete Reference 10, 11
The reader can directly jump on the said chapters, I have created hyperlinks.
Below is the XSLT code –
<xsl:template match="References">
<xsl:if test="CaseRef != ''"><br/>
<table border="1" width="100%">
<tr>
<td width="75%">Book Name</td>
<td align="right">Chapters</td>
</tr>
<xsl:for-each select="BookName">
<tr>
<td valign="top">
<xsl:value-of select="."/>
</td>
<td align="right" valign="bottom">
<xsl:for-each select="following::Chapter">
<a id="lnk">
<!-- This code will create a hyperlink to jump directly on the said chapter-->
<xsl:attribute name="href">
<xsl:value-of select="concat(concat('#',.),./@L)"/>
</xsl:attribute>
<xsl:value-of select="."/><xsl:text> </xsl:text>
</a>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:if>
</xsl:template>
I am missing something (might be much more) to get the required output!!
There are couple of problems with your XSLT. Firstly, within the References template you are looping over BookName name elements, but these are nested within BookRef elements, so you should be doing
(Or preferably, use apply-templates to avoid too much nested code)
The next issue is with this loop, where you loop over the chapters
The problem here is that will pick up all following Chapter elements, even those that occur after a following Bookname element. One way to fix this is define a keep, to look-up only those Chapter elements for a given book.
Then, assuming you are positioned on a BookName element, you can get the matching Chapter elements, like so:
Try the following XSLT (Note I have removed the reference to CaseRef that occurred in your original XSLT)
When applied to your XML (assuming it has a single root element), the following is output