簡體   English   中英

如何讓XSL模板不為每個記錄重復表?

[英]how to have XSL template not repeat the table for each record?

模板顯示從數據庫生成的數據集。 數據集中有很多行。

為數據集中的每一行執行<xsl:template match="Photos"> 數據集中的每一行都有一個圖像。

我只想重復<td id="tdImage"> ,其余的只執行一次。

這樣做的全部目的是使圖像水平顯示,而不是垂直顯示。

現在看起來如何

 <xsl:template match="Photos">

<table width = "600">

  <tr style="width:600;">
    <xsl:if test =  "SequenceNumber=1" >
      <td colspan ="5" class ="input">Photos:</td>
    </xsl:if>
  </tr>

  <tr style="width:600;">


        <td id="tdImage">
          <table width ="150">

            <tr>
              <td style ="padding-left:15px">
                <table width ="150">
                  <tr style="width:150px;">
                    <td style="width:135px;">
                      <a href = '{src}'  style="font-size:10px;">
                        <xsl:value-of select="FileName"/>
                      </a>

                    </td>
                  </tr>
                  <tr style="width:150px;">
                    <td  style="width:150px;">

                      <img  type = "hidden"  Width="75" Height="75" src='{src}' />

                    </td>

                  </tr>


                </table>

              </td>
            </tr>
          </table>
        </td>

    </tr>

</table>

提前致謝!

編輯:數據集的示例

    SequenceNumber    |    FileName    |               src
    ---------------------------------------------------------------------
          1           |   flowers.jpg  |   blabla.ashx?SequenceNumber=1
          2           |   light.jpg    |   blabla.ashx?SequenceNumber=2
          3           |   garden.jpg   |   blabla.ashx?SequenceNumber=3
          4           |   candy.jpg    |   blabla.ashx?SequenceNumber=4

我不確定我能否給出准確的答案,但是作為一個入門者,您可能想要一個與主數據集(所有Photos元素的父級)匹配的模板,您可以在其中構造表格行。

如果您只想只顯示前4個,則可以從此開始...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/*">
        <table>
            <tr>
                <td colspan="4">Photos:</td>
            </tr>
            <tr>
                <xsl:apply-templates select="Photos[position() &lt; 5]" />
            </tr>
        </table>
    </xsl:template>

    <xsl:template match="Photos">
        <td>
            <a href = '{src}'  style="font-size:10px;">
                <xsl:value-of select="FileName"/>
            </a>
            <br />
            <img Width="75" Height="75" src='{src}' />
        </td>
    </xsl:template>
</xsl:stylesheet>

注意我簡化了“照片”模板,因為實際上不建議嵌套在表中的表。

另一方面,如果您有多於4張照片,並且希望每行顯示4張照片,則可以選擇出現在每行開頭的第一張照片(即位置1、5、9等的照片),然后從每一個中建立一行。

也嘗試這個XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/*">
        <table>
            <tr>
                <td colspan="4">Photos:</td>
            </tr>
            <xsl:for-each select="Photos[position() mod 4 = 1]">
                <tr>
                    <xsl:apply-templates select="self::*|following-sibling::Photos[position() &lt; 4]" />
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>

    <xsl:template match="Photos">
        <td>
            <a href = '{src}'  style="font-size:10px;">
                <xsl:value-of select="FileName"/>
            </a>
            <br />
            <img Width="75" Height="75" src='{src}' />
        </td>
    </xsl:template>
</xsl:stylesheet>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM