Reputation: 13
I’m new to XSL and I’m trying to create a XSL to create a table of comments. This will be a constantly updating xml with comments being added.
I have figured out the basic table and am happy with that, however if a new comment is appended to an existing view node I want this to show as another line item. Same view name, same child node names but with different child node values. Below is an example of my XML, XSL and the current HTML output followed by my desired HMTL output. Could anyone please point me in the right direction?
Thanks in advance!
XML
<?xml version="1.0" encoding="UTF-8" ?>
<exchange xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<viewpoints>
<view name="View-01">
<comments>
<comment id="101" status="new">
<user>User 01</user>
<body>My Comment in View-01</body>
<createddate>
<date year="2016" month="11" day="28" hour="8" minute="1" />
</createddate>
</comment>
</comments>
</view>
<view name="View-02">
<comments>
<comment id="03" status="new">
<user>User 01</user>
<body>Please approve this comment</body>
<createddate>
<date year="2016" month="11" day="28" hour="8" minute="2" />
</createddate>
</comment>
<comment id="07" status="closed">
<user>Supervisor 02</user>
<body>This comment has been approved</body>
<createddate>
<date year="2016" month="11" day="30" hour="16" minute="25" />
</createddate>
</comment>
</comments>
</view>
<view name="View-04">
<comments>
<comment id="12" status="new">
<user>User 02</user>
<body>My Comment in View-04</body>
<createddate>
<date year="2016" month="11" day="30" hour="8" minute="2" />
</createddate>
</comment>
</comments>
</view>
XSL
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0" xmlns:xsl =
"http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<BODY>
<TABLE BORDER="1" >
<h2>Comments</h2>
<tr bgcolor="#9acd32">
<td style="text-align:left">View Name</td>
<td style="text-align:left">Comment Status</td>
<td style="text-align:left">User</td>
<td style="text-align:left">Comment</td>
</tr>
<xsl:apply-templates select="//view"></xsl:apply-templates>
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:template match = "//view">
<div>
<tr>
<td><xsl:apply-templates select="@name"/></td>
<xsl:apply-templates select="comments/comment/@status"/>
<xsl:apply-templates select="comments/comment/user"/>
<xsl:apply-templates select="comments/comment/body"/>
</tr>
</div>
</xsl:template>
<xsl:template match = "view">
<div><td><xsl:value-of select = "." /></td></div>
</xsl:template>
<xsl:template match = "@status">
<div><td><xsl:value-of select = "." /></td></div>
</xsl:template>
<xsl:template match = "user">
<div><td><xsl:value-of select = "." /></td></div>
</xsl:template>
<xsl:template match = "body">
<div><td><xsl:value-of select = "." /></td></div>
</xsl:template>
</xsl:stylesheet>
Current Output
<HTML>
<BODY>
<TABLE BORDER="1">
<h2>Comments</h2>
<tr bgcolor="#9acd32">
<td style="text-align:left">View Name</td>
<td style="text-align:left">Comment Status</td>
<td style="text-align:left">User</td>
<td style="text-align:left">Comment</td>
</tr>
<div>
<tr>
<td>View-01</td>
<div>
<td>new</td>
</div>
<div>
<td>User 01</td>
</div>
<div>
<td>My Comment in View-01</td>
</div>
</tr>
</div>
<div>
<tr>
<td>View-02</td>
<div>
<td>new</td>
</div>
<div>
<td>closed</td>
</div>
<div>
<td>User 01</td>
</div>
<div>
<td>Supervisor 02</td>
</div>
<div>
<td>Please approve this comment</td>
</div>
<div>
<td>This comment has been approved</td>
</div>
</tr>
</div>
<div>
<tr>
<td>View-04</td>
<div>
<td>new</td>
</div>
<div>
<td>User 02</td>
</div>
<div>
<td>My Comment in View-04</td>
</div>
</tr>
</div>
</TABLE>
</BODY>
</HTML>
Desired Output
<HTML>
<BODY>
<TABLE BORDER="1">
<h2>Comments</h2>
<tr bgcolor="#9acd32">
<td style="text-align:left">View Name</td>
<td style="text-align:left">Comment Status</td>
<td style="text-align:left">User</td>
<td style="text-align:left">Comment</td>
</tr>
<div>
<tr>
<td>View-01</td>
<div>
<td>new</td>
</div>
<div>
<td>User 01</td>
</div>
<div>
<td>My Comment in View-01</td>
</div>
</tr>
</div>
<div>
<tr>
<td>View-02</td>
<div>
<td>new</td>
</div>
<div>
<td>User 01</td>
</div>
<div>
<td>Please approve this comment</td>
</div>
</tr>
<tr>
<td>View-02</td>
<div>
<td>closed</td>
</div>
<div>
<td>Supervisor 02</td>
</div>
<div>
<td>This comment has been approved</td>
</div>
</tr>
</div>
<div>
<tr>
<td>View-04</td>
<div>
<td>new</td>
</div>
<div>
<td>User 02</td>
</div>
<div>
<td>My Comment in View-04</td>
</div>
</tr>
</div>
</TABLE>
</BODY>
</HTML>
Upvotes: 0
Views: 64
Reputation: 116982
If you want the table to have a row for each comment
, then create a row for each comment
- for example:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/exchange">
<html>
<body>
<h2>Comments</h2>
<table border="1">
<tr>
<th>View Name</th>
<th>Comment Status</th>
<th>User</th>
<th>Comment</th>
</tr>
<xsl:apply-templates select="viewpoints/view/comments/comment"/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="comment">
<tr>
<td>
<xsl:value-of select="../../@name"/>
</td>
<td>
<xsl:value-of select="@status"/>
</td>
<td>
<xsl:value-of select="user"/>
</td>
<td>
<xsl:value-of select="body"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0