【问题标题】:Append a new child node, create a new row?追加一个新的子节点,创建一个新行?
【发布时间】:2016-12-12 19:53:22
【问题描述】:

我是 XSL 的新手,我正在尝试创建一个 XSL 来创建一个 cmets 表。这将是一个不断更新的 xml,其中添加了 cmets。

我已经找到了基本表并且对此很满意,但是如果将新注释附加到现有视图节点,我希望它显示为另一个行项目。相同的视图名称,相同的子节点名称但具有不同的子节点值。下面是我的 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>

【问题讨论】:

  • 所有divs 的意义何在?
  • 我明白你的意思,现在看,它们真的没有任何意义!

标签: html xml xslt


【解决方案1】:

如果您希望表中每个 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多