2016-11-07 63 views
1

我想創建1個XSL樣式表,可以創建具有數據特定標題和列數的HTML表格代碼。下面是數據的兩個示例性套欲由1個樣式表用於動態XML數據的XSL樣式表

<root> 
     <row> 
     <postId>1</postId> 
     <id>1</id> 
     <name>name 1</name> 
     <email>[email protected]</email> 
     <body>body 1</body> 
     </row> 
     <row> 
     <postId>1</postId> 
     <id>2</id> 
     <name>name 2</name> 
     <email>[email protected]</email> 
     <body>body 2</body> 
     </row> 
</root> 

<root> 
     <row> 
     <id>1</id> 
     <desc>desc 1</desc> 
     <Note>Note 1</Note> 
     </row> 
     <row> 
     <id>2</id> 
     <desc>desc 2</desc> 
     <Note>Note 2</Note> 
     </row> 
     <row> 
     <id>3</id> 
     <desc>desc 3</desc> 
     <Note>Note 3</Note> 
     </row> 
</root> 

處理然後,我想在片材產生類似輸出:

<table> 
    <thead> 
    <th>postId</th><th>id</th><th>name</th><th>email</th><th>body</th> 
    </thead> 
    <tbody> 
    <tr> 
     <td>1</td><td>1</td><td>mane 1</td><td>[email protected]</td><td>body 1</td> 
    </tr> 
    <tr> 
     <td>1</td><td>2</td><td>mane 2</td><td>[email protected]</td><td>body 2</td> 
    </tr> 
    </tbody> 

<table> 
    <thead> 
    <th>id</th><th>desc</th><th>Note</th> 
    </thead> 
    <tbody> 
    <tr> 
     <td>1</td><td>desc 1</td><td>Note 1</td> 
    </tr> 
    <tr> 
     <td>2</td><td>desc 2</td><td>Note 2</td> 
    </tr> 
    <tr> 
     <td>3</td><td>desc 3</td><td>Note 3</td> 
    </tr> 
    </tbody> 
</table> 

我已想出表頭(見下面),但我似乎無法讓身體工作。我無法弄清楚如何顯示一個元素的值,我不知道這個元素的名稱或者是否存在。

<?xml version="1.0"?> 

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:template match="root"> 
    <html> 
    <body> 
     <h2>My Table Data</h2> 
     <table border="1"> 
      <thead> 
      <tr bgcolor="#9acd32"> 
       <xsl:for-each select="row[1]/*"> 
       <th><xsl:value-of select ="name(.)"/></th>   
       </xsl:for-each> 
      </tr> 
      </thead> 
      <tbody> 
      ????? 
      </tbody> 

回答

1

如果您row元素始終具有相同數量的元素,並以相同的順序,你就不必擔心名字,和你?????可以簡單地用這個來代替....

<xsl:for-each select="row"> 
    <tr> 
     <xsl:for-each select="*"> 
     <td> 
      <xsl:value-of select="." /> 
     </td> 
     </xsl:for-each> 
    </tr> 
</xsl:for-each> 

在另一方面,如果row元素可以有不同順序的元素,甚至缺失,則(假設第一行總是完整的),你可以定義一個變量來保存「標題」行單元格

<xsl:variable name="headers" select="row[1]/*"/> 

然後你的????受本...

<xsl:for-each select="row"> 
    <xsl:variable name="currentRow" select="."/> 
    <tr> 
     <xsl:for-each select="$headers"> 
     <xsl:variable name="currentHeaderName" select="name()" /> 
      <td> 
       <xsl:value-of select="$currentRow/*[name() = $currentHeaderName]"/> 
      </td> 
     </xsl:for-each> 
    </tr> 
    </xsl:for-each> 

試試這個XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="root"> 
     <html> 
     <body> 
      <h2>My Table Data</h2> 
      <xsl:variable name="headers" select="row[1]/*"/> 
      <table border="1"> 
       <thead> 
        <tr bgcolor="#9acd32"> 
        <xsl:for-each select="$headers"> 
         <th> 
          <xsl:value-of select="name(.)"/> 
         </th>   
        </xsl:for-each> 
        </tr> 
       </thead> 
       <tbody> 
        <xsl:for-each select="row"> 
        <xsl:variable name="currentRow" select="."/> 
        <tr> 
         <xsl:for-each select="$headers"> 
         <xsl:variable name="currentHeaderName" select="name()" /> 
          <td> 
           <xsl:value-of select="$currentRow/*[name() = $currentHeaderName]"/> 
          </td> 
         </xsl:for-each> 
        </tr> 
        </xsl:for-each> 
       </tbody> 
      </table> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 
+0

非常感謝! –