2010-08-03 62 views
1

我正在嘗試使用輸入XML執行簡單的動態<table>。我的XML代碼如下所示:使用XSLT從XML生成動態<table>

<table> 
     <col size="5%">#</col> 
     <col size="55%">Title</col> 
     <col size="10%">Author</col> 
     <col size="10%">Date</col> 
     <col size="10%">Modification</col> 
     <col size="10%">Actions</col> 

     <output> 
      <row>1</row> 
      <row>Title of the entry</row> 
      <row>Administrator</row> 
      <row>dd/mm/yyyy hh:mm</row> 
      <row>dd/mm/yyyy hh:mm</row> 
      <row>Edit Delete</row> 
     </output> 
    </table> 

我生成這樣,因爲我wan't有針對後端面板一個XSLT,這取決於不同部分的變量轉換我的輸出XML到<table>

我這樣做XSLT代碼:

   <table> 
        <tr> 
         <xsl:for-each select="page/index/table/col"> 
          <td> 
           <xsl:attribute name="width"><xsl:value-of select="size" /></xsl:attribute> 
           <xsl:value-of select="." /> 
          </td> 
         </xsl:for-each> 
        </tr> 
        <xsl:for-each select="page/index/table/output"> 
         <tr> 
          <xsl:for-each select="row"> 
           <td> 
            <xsl:value-of select="." /> 
           </td> 
          </xsl:for-each> 
         </tr> 
        </xsl:for-each> 
       </table> 

我生成表,但我不能讓充滿COL /大小的值的屬性寬度。

我該怎麼辦?

+0

好問題(+1)。查看我的答案,找出問題的解決方案+以更簡單的方式徹底重寫轉換。 – 2010-08-03 13:16:52

回答

3

使用

<td width="{@size}"><xsl:value-of select="."/></td> 

在XSLT建議避免使用<xsl:for-each>如果可能的話

而不<xsl:for-each>的完全轉化是:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="table"> 
    <table> 
    <tr> 
     <xsl:apply-templates select="col"/> 
    </tr> 
    <xsl:apply-templates select="output"/> 
    </table> 
</xsl:template> 

<xsl:template match="col"> 
    <td width="{@size}"><xsl:value-of select="."/></td> 
</xsl:template> 

<xsl:template match="output"> 
    <tr> 
    <xsl:apply-templates/> 
    </tr> 
</xsl:template> 

<xsl:template match="row"> 
    <td><xsl:value-of select="."/></td> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉變是在下面的XML文檔(原始的XML片段,包裹在indexpage元件)施加:

<page> 
    <index> 
     <table> 
      <col size="5%">#</col> 
      <col size="55%">Title</col> 
      <col size="10%">Author</col> 
      <col size="10%">Date</col> 
      <col size="10%">Modification</col> 
      <col size="10%">Actions</col> 
      <output> 
       <row>1</row> 
       <row>Title of the entry</row> 
       <row>Administrator</row> 
       <row>dd/mm/yyyy hh:mm</row> 
       <row>dd/mm/yyyy hh:mm</row> 
       <row>Edit Delete</row> 
      </output> 
     </table> 
    </index> 
</page> 

想要的,正確的結果產生

<table> 
    <tr> 
     <td width="5%">#</td> 
     <td width="55%">Title</td> 
     <td width="10%">Author</td> 
     <td width="10%">Date</td> 
     <td width="10%">Modification</td> 
     <td width="10%">Actions</td> 
    </tr> 
    <tr> 
     <td>1</td> 
     <td>Title of the entry</td> 
     <td>Administrator</td> 
     <td>dd/mm/yyyy hh:mm</td> 
     <td>dd/mm/yyyy hh:mm</td> 
     <td>Edit Delete</td> 
    </tr> 
</table> 
+0

+1好答案。在字面結果中使用AVT使得代碼緊湊。 – 2010-08-03 14:43:11

1

的大小屬性的選擇應閱讀@size而不僅僅是size,即:

<xsl:value-of select="@size" /> 
0

嘗試的@size代替size:你正在尋找一個屬性,而不是元素。