附加一个新的子节点,创建一个新行?

时间:2016-12-12 11:27:46

标签: html xml xslt

我是XSL的新手,我正在尝试创建一个XSL来创建一个评论表。这将是一个不断更新的xml,并添加了注释。

我已经找到了基本表,并对此感到满意,但是如果将新注释附加到现有视图节点,我希望将其显示为另一个行项。相同的视图名称,相同的子节点名称,但具有不同的子节点值。下面是我的XML,XSL和当前HTML输出的示例,后面是我想要的HMTL输出。有谁能指出我正确的方向?

提前致谢!

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>

当前输出

   <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>

所需输出

   <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>

1 个答案:

答案 0 :(得分:0)

如果您希望表格为每个comment添加一行,则为每个comment创建一行 - 例如:

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>