2016-12-12 5 views
0

Ich bin neu in XSL und ich versuche, ein XSL zu erstellen, um eine Tabelle mit Kommentaren zu erstellen. Dies wird ein ständig aktualisierendes xml sein, dem Kommentare hinzugefügt werden.Einen neuen Kindknoten anfügen, eine neue Zeile erstellen?

Ich habe die Basistabelle herausgefunden und bin damit zufrieden. Wenn jedoch ein neuer Kommentar an einen vorhandenen Ansichtsknoten angehängt wird, möchte ich dies als eine andere Werbebuchung anzeigen. Derselbe Ansichtsname, dieselben untergeordneten Knotennamen, jedoch mit anderen untergeordneten Knotenwerten. Im Folgenden finden Sie ein Beispiel für meine XML, XSL und die aktuelle HTML-Ausgabe gefolgt von meiner gewünschten HMTL-Ausgabe. Könnte mir bitte jemand in die richtige Richtung zeigen?

Vielen Dank im Voraus!

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> 

Stromausgang

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

gewünschte Ausgabe

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

Was ist der Sinn all dieser 'div's? –

+0

Ich sehe was du meinst, und wenn du es jetzt ansiehst, haben sie keinen wirklichen Sinn! – HughMcD

Antwort

0

Wenn Sie die Tabelle wollen für jede comment eine Zeile haben, dann erstellen Sie eine Zeile für jeden comment - zum Beispiel:

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