2011-11-21 72 views
0

我的問題有點類似於XML to HTML table with XSLT帶有帶動態標題的XSLT的XML到HTML表格

我已經定義了一本字典在XML如下:

<dictionary> 
    <languages> 
     <language>en</language> 
     <language>ja</language> 
    </languages> 
    <entries> 
     <entry id="1"> 
      <en>Test</en> 
      <ja>テスト</ja> 
     </entry> 
     <entry id="2"> 
      <en>Test2</en> 
      <ja>テスト2</en> 
     </entry> 
    </entries> 
</dictionary> 

而且我想在XHTML以下的輸出:

<table> 
    <thead> 
     <tr> 
      <th>en</th> 
      <th>ja</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Test</td> 
      <td>テスト</td> 
     </tr> 
     <tr> 
      <td>Test2</td> 
      <td>テスト2</td> 
     </tr> 
    </tbody> 
</table> 

我改編自XML to HTML table with XSLT答案如下:

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

<xsl:template match="//dictionary/entries"> 
    <table><xsl:apply-templates select="entry"/></table> 
</xsl:template> 

<xsl:template match="entry[1]"> 
    <thead><tr><xsl:apply-templates select="*" mode="header"/></tr></thead> 
    <xsl:call-template name="standardRow"/> 
</xsl:template> 

<xsl:template match="entry" name="standardRow"> 
    <tbody><tr><xsl:apply-templates select="*"/></tr></tbody> 
</xsl:template> 

<xsl:template match="entry/*"> 
    <td><xsl:apply-templates select="node()"/></td> 
</xsl:template> 

<xsl:template match="entry/*" mode="header"> 
    <th><xsl:value-of select="name()"/></th> 
</xsl:template> 
</xsl:stylesheet> 

問題是我可能有如下輸入:

<dictionary> 
    <languages> 
     <language>en</language> 
     <language>ja</language> 
      <language>id</language> 
    </languages> 
    <entries> 
     <entry id="1"> 
      <en>Test</en> 
      <ja>テスト</ja> 
     </entry> 
     <entry id="2"> 
      <ja>テスト2</ja> 
      <en>Test2</en> 
      <id>uji2</id> 
     </entry> 
    </entries> 
</dictionary> 

正如你可能已經明白,XSLT採取第一entry節點定義的列名,並且不產生id列。此外,如果語言順序在entry中更改,則<td>不會按順序顯示。

通過上面的投入,我想下面的輸出:

<table> 
    <thead> 
     <tr> 
      <th>en</th> 
      <th>ja</th> 
      <th>id</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Test</td> 
      <td>テスト</td> 
      <td></td> 
     </tr> 
     <tr> 
      <td>Test2</td> 
      <td>テスト2</td> 
      <td>Uji2</td> 
     </tr> 
    </tbody> 
</table> 

這是使用XSLT,我真的不知道我怎麼能做到這一點我的第一次。我想我可以使用languages節點。請注意,XML輸入格式非常靈活,即使我需要更改格式,我也歡迎任何建議。

回答

1

下面是一個簡單的樣式表:

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

    <xsl:output method="html" indent="yes"/> 

    <xsl:key name="k1" match="entry/*" use="concat(generate-id(..), '|', local-name())"/> 

    <xsl:variable name="languages" select="/dictionary/languages/language"/> 

    <xsl:template match="dictionary"> 
    <xsl:apply-templates select="entries"/> 
    </xsl:template> 

    <xsl:template match="entries"> 
    <table> 
     <thead> 
     <tr> 
      <xsl:apply-templates select="$languages" mode="header"/> 
     </tr> 
     </thead> 
     <tbody> 
     <xsl:apply-templates/> 
     </tbody> 
    </table> 
    </xsl:template> 

    <xsl:template match="language" mode="header"> 
    <th> 
     <xsl:value-of select="."/> 
    </th> 
    </xsl:template> 

    <xsl:template match="entry"> 
    <tr> 
     <xsl:apply-templates select="$languages"> 
     <xsl:with-param name="entry" select="current()"/> 
     </xsl:apply-templates> 
    </tr> 
    </xsl:template> 

    <xsl:template match="language"> 
    <xsl:param name="entry"/> 
    <td> 
     <xsl:value-of select="key('k1', concat(generate-id($entry), '|', .))"/>  
    </td> 
    </xsl:template> 
</xsl:stylesheet> 
+0

我跑測試這個樣式表,它看起來,它完美的作品。接受答案!非常感謝你。關鍵在'變量'和'鍵',我沒有如何使用它們。 –

+1

解決方案的關鍵在於確保在每次要爲「entry」輸出數據並將'entry'作爲參數傳入時處理'$ languages'。您可以使用'xsl:value-of select =「$ entry/* [local-name()= current()]來使用'xsl:key'和'key'函數。 />'而不是''。 –