iT邦幫忙

1

XSLT 1.0 可以做到動態變數取值嗎?

  • 分享至 

  • xImage

因表格有固定時間要呈現,使用XML+XSLT(1.0)遇到一個問題,目前想法是在XSLT中先訂好時間透過xsl:for-each 將時間動態的佈道畫面上,但不知道是不是因為XSLT1.0的限制導致畫面無法呈現,程式碼如下:
XSLT:`<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="times">

    <!-- Row 3 -->
    <time row="3" cell="1">0015</time>
    <time row="3" cell="9">0815</time>
    <time row="3" cell="17">1615</time>
    
    <!-- Row 4 -->
    <time row="4" cell="1">0030</time>
    <time row="4" cell="9">0830</time>
    <time row="4" cell="17">1630</time>

</times>
</xsl:variable>



<xsl:template match="/">
    <html>
        <head>
            <title>時間測試</title>
        </head>
        <style>

            @media print
            {
				table { page-break-after:auto }
				tr { page-break-inside:avoid; page-break-after:auto }
				td { font-family:DFKai-sb;page-break-inside:avoid; page-break-after:auto }
				thead { display:table-header-group }
            }
			
			table { page-break-after:auto }
			tr { page-break-inside:avoid; page-break-after:auto }
			td { font-family:DFKai-sb;page-break-inside:avoid; page-break-after:auto }
			thead { display:table-header-group }

        </style>
        <body style="width:794px; margin: 0 auto;">              	
            <div style="width:650px;  margin-left: auto;margin-right: auto;"> 
								
				<!--內容標題項目-->	
				<table width="100%" border="0"  cellspacing="0" style="margin-top:5px">
					<tbody>
						<tr>
							<td>
								<table  width="100%" border="1"  cellspacing="0">
									<tbody>											
										<!-- 開始生成行 -->
										<xsl:call-template name="generate-rows">
											<xsl:with-param name="row" select="1"/>
										</xsl:call-template>																	
									</tbody>
								</table>
							</td>								
						</tr>
					</tbody>
				</table>
            </div>
        </body>
    </html>
</xsl:template>



<xsl:template name="generate-rows">
    <xsl:param name="row"/>
    <xsl:if test="$row &lt;= 34">
        <tr>
            <xsl:call-template name="generate-cells">
                <xsl:with-param name="cell" select="1"/>
				<xsl:with-param name="row" select="$row"/>
            </xsl:call-template>
        </tr>
        <xsl:call-template name="generate-rows">
            <xsl:with-param name="row" select="$row + 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

<xsl:template name="generate-cells">
    <xsl:param name="cell"/>
	<xsl:param name="row"/>
    <xsl:if test="$cell &lt;= 24">
	
		<xsl:choose>
			<xsl:when test="$row=1 and $cell mod 8=1">
			 <td style="min-height:20px">日期<br/>時間</td>
			</xsl:when>
			<xsl:otherwise>
			
			<td style="min-height:20px">
				<xsl:value-of select="$times//time[@row=$row and @cell=$cell]"/>
			</td>

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

        <xsl:call-template name="generate-cells">
            <xsl:with-param name="cell" select="$cell + 1"/>
			<xsl:with-param name="row" select="$row"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>
`
請問是不是因為XSLT1.0的限制呢?
PS:因排版關係無法將範例呈現好請多見諒

XSLT 1.0 has certain limitations when it comes to dynamically setting and changing variable values because XSLT 1.0 variables are immutable. Once a variable is assigned a value, it cannot be changed or updated. This means that you cannot increment or dynamically adjust the value of a variable within an xsl:for-each loop or other similar constructs.

However, you can achieve dynamic behavior by using recursive templates or by leveraging other XML structures that act as counters or dynamic elements. Here's a basic approach to how you might dynamically display times on the screen using a recursive template:

Example XML

<times>
<time>08:00</time>
<time>09:00</time>
<time>10:00</time>
<time>11:00</time>
</times>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="times/time" />
</xsl:template>

<xsl:template match="time">
<div>
<xsl:value-of select="." />
</div>
<xsl:if test="following-sibling::time">
<xsl:apply-templates select="following-sibling::time[1]" />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
williams67
iT邦新手 5 級 ‧ 2024-08-28 13:53:10

XSLT 1.0 has certain limitations when it comes to dynamically setting and changing variable values because XSLT 1.0 variables are immutable. Once a variable is assigned a value, it cannot be changed or updated. This means that you cannot increment or dynamically adjust the value of a variable within an xsl:for-each loop or other similar constructs.

However, you can achieve dynamic behavior by using recursive templates or by leveraging other XML structures that act as counters or dynamic elements. Here's a basic approach to how you might dynamically display times on the screen using a recursive template:

Example XML

<times>
<time>08:00</time>
<time>09:00</time>
<time>10:00</time>
<time>11:00</time>
</times>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="times/time" />
</xsl:template>

&lt;xsl:template match=&quot;time&quot;&gt;
    &lt;div&gt;
        &lt;xsl:value-of select=&quot;.&quot; /&gt;
    &lt;/div&gt;
    &lt;xsl:if test=&quot;following-sibling::time&quot;&gt;
        &lt;xsl:apply-templates select=&quot;following-sibling::time[1]&quot; /&gt;
    &lt;/xsl:if&gt;
&lt;/xsl:template&gt;

</xsl:stylesheet>

我要發表回答

立即登入回答