2013-10-24 60 views
1

我正在從XML和XSLT轉換爲HTML。我有這樣的XSLT代碼:如果條件爲真,則將XSLT轉換爲HTML創建表

<html> 
    <head> 
    <title>Tiskova sestava</title> 
    </head> 
    <body> 
    ... 
    <table rules="GROUPS" frame="BOX"> 
     <THEAD> 
     <tr> 
      <th>Typ</th> 
      .... 
      <th>Středisko</th> 
     </tr> 
     </THEAD> 
     <TBODY> 
     <xsl:for-each select="Report/Polozka"> 
      <tr> 
      <td> 
       <xsl:value-of select="Kategorie"/> 
      </td> 
       ..... 
      <td> 
       <xsl:value-of select="Stredisko"/> 
      </td> 
      </tr> 
     </xsl:for-each> 
     </TBODY> 
    </table> 
    </body> 
</html> 

,這是輸出:enter image description here

但我想創造新的表,如果列「Stredisko」值是不同的,那麼先前的值。所以在第一個表中應該是列值爲「Stredisko」== 4的行,在值爲== 1的第二個表中,並且在值爲==「」的第三個表中。

Input XML

回答

1

試試這個XSL獲得前值。這不是一個完整的答案,但你可以把它作爲一個起點(如果它是確定的,當然):

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html" encoding="utf-8" indent="yes" /> 

<!-- for easy selection of Polozka elements: --> 
<xsl:key name="reports-by-stredisko" match="/Report/Polozka" use="Stredisko" /> 

<xsl:template match="/"> 
    <xsl:for-each select="/Report/Polozka[not(Stredisko = preceding-sibling::Polozka/Stredisko)]"> 
    <table summary="Stredisko: {Stredisko}"> 
     <xsl:for-each select="key('reports-by-stredisko',Stredisko)"> 
     <tr> 
     <td>I_Cislo: <xsl:value-of select="I_Cislo"/></td> 
     <td>Stredisko: <xsl:value-of select="Stredisko"/></td> 
     <td>...</td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 

這XSL生成以下的輸出:

<table summary="Stredisko: 4"> 
<tr> 
    <td>I_Cislo: 29IM00001</td> 
    <td>Stredisko: 4</td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 29IM00001</td> 
    <td>Stredisko: 4</td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 12D3333</td> 
    <td>Stredisko: 4</td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 10DM00004</td> 
    <td>Stredisko: 4</td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 10DM00005</td> 
    <td>Stredisko: 4</td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 10DM00007</td> 
    <td>Stredisko: 4</td> 
    <td>...</td> 
</tr> 
</table> 

<table summary="Stredisko:  "> 
<tr> 
    <td>I_Cislo: 10IM00002</td> 
    <td>Stredisko:  </td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 11IM00002</td> 
    <td>Stredisko:  </td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 12IM00002</td> 
    <td>Stredisko:  </td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 10IM00002</td> 
    <td>Stredisko:  </td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 11IM00002</td> 
    <td>Stredisko:  </td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 12IM00002</td> 
    <td>Stredisko:  </td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 12DM00007</td> 
    <td>Stredisko:  </td> 
    <td>...</td> 
</tr> 
<tr> 
    <td>I_Cislo: 10LM00002</td> 
    <td>Stredisko:  </td> 
    <td>...</td> 
</tr> 
</table> 

<table summary="Stredisko: 1"> 
<tr> 
    <td>I_Cislo: 12DM00006</td> 
    <td>Stredisko: 1</td> 
    <td>...</td> 
</tr> 
</table> 

希望它可以幫助

0

請更正你的代碼如下邏輯 把你的條件,並通過使用前同輩

<xsl:choose> 
<xsl:when test="PLEASE CHECK YOUR CONDITION HERE"> 
    <xsl:value-of disable-output-escaping="yes" select="preceding-sibling::*[1]/YOUR TAG NAME HERE"/> 
    </xsl:when> 
    <xsl:otherwise> 

    </xsl:otherwise> 
</xsl:choose> 

非常感謝 Avnish

+0

請你能寫一個條件的測試?我應該在哪裏添加,我應該在哪裏添加

? –