如果condition为true,则XSLT转换为HTML create table

时间:2013-10-24 13:05:05

标签: html xml xslt transform xslt-1.0

我正在从XML和XSLT转换为HTML。我有这个XSLT代码:

<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”的IF值不同,那么之前的值。所以在第一个表中应该是值为“Stredisko”== 4的行,在第二个表中值为== 1,在第三个表中为值==“”。

Input XML

2 个答案:

答案 0 :(得分: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>

希望有所帮助

答案 1 :(得分:0)

请将您的代码更正为以下逻辑 通过使用previous-sibling

来设置您的条件并获得之前的值
<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