[英]Xslt – xsl:with-param dynamically assigned (Correct usage of xsl:with-param without select attribute)
[英]Usage of XSLT Params; <xsl:param> & <xsl:with-param>
請解釋我如何使用最好的 XSLT 參數。 根據<xsl:param>
& <xsl:with-param>
樣本 LOC:
<xsl:call-template name="ABC">
<xsl:with-param name="title" />
</xsl:call-template>
請向我解釋如何最好地使用 XSLT 參數。 在
<xsl:param>
和<xsl:with-param>
<xsl:param>
可以在全局級別的任何地方指定(作為xsl:stylesheet
的子項),或者如果它在模板中,它必須是它的子項並且它必須在xsl:template
任何非xsl:param
子項之前.
這是允許模板或整個轉換(在全局xsl:param
情況下)分別從模板或整個轉換的調用者/發起者接收不同數據的工具。
在模板/轉換的調用者/發起者一側,使用xsl:with-param
指令傳遞xsl:with-param
。 它可以是xsl:apply-templates
或xsl:call-template
的子代。
xsl:param
或xsl:with-param
的name
屬性是強制性的。 它標識參數。
xsl:with-param
的 select 屬性可用於指定任何XPath 表達式,其計算結果將傳遞給被調用/應用的模板。
或者,可以在xsl:with-param
的內容(正文)中指定該值。
xsl:with-param
必須具有select
屬性或正文。 但不是他們兩個。
xsl:param
也可以有一個選擇屬性或主體。 在這種情況下,這些指定參數的默認值,如果調用者沒有指定具有此名稱的參數,則使用它。
最后,這里有一個完整的例子來說明大多數這些概念:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pTarget" select="'love'"/>
<xsl:param name="pReplacement" select="'like'"/>
<xsl:template match="/*">
<xsl:call-template name="replace">
<xsl:with-param name="pPattern" select="$pTarget"/>
<xsl:with-param name="pRep" select="$pReplacement"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
<xsl:call-template name="replace"/>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="text()">
<xsl:with-param name="pPattern" select="$pTarget"/>
<xsl:with-param name="pRep" select="'adore'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="text()" name="replace">
<xsl:param name="pText" select="."/>
<xsl:param name="pPattern" select="'hate'"/>
<xsl:param name="pRep" select="'disapprove'"/>
<xsl:if test="string-length($pText) >0">
<xsl:choose>
<xsl:when test="not(contains($pText, $pPattern))">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($pText, $pPattern)"/>
<xsl:value-of select="$pRep"/>
<xsl:call-template name="replace">
<xsl:with-param name="pPattern" select="$pPattern"/>
<xsl:with-param name="pRep" select="$pRep"/>
<xsl:with-param name="pText" select=
"substring-after($pText, $pPattern)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
當應用於此 XML 文檔時...
<t>Sports stars we really love, love to hate, hate</t>
……結果……
Sports stars we really like, like to hate, hate
Sports stars we really love, love to disapprove, disapprove
Sports stars we really adore, adore to hate, hate
說明:
replace
模板被調用兩次。 在這兩個調用中都省略了pText
參數。 其默認值由被調用的模板使用。
第一個調用提供了模式和替換參數,所以"love"
被"like"
替換。
請注意,傳遞了全局參數$pTarget
和$pReplacement
的值。 如果轉換的發起者決定為這些全局參數傳遞其他值(不是此代碼中使用的默認值),則這些值將傳遞給replace
模板,而不是默認值"love"
和"like"
。
第二個調用根本不提供任何參數值,因此使用replace
模板中的所有默認值——字符串"hate"
替換為字符串"disapprove"
。
請注意, replace
模板會遞歸調用自身,因此所有出現的模式都將被替換替換。
此外,遞歸調用的pText
參數值不是靜態的,而是動態計算的。
第三次從外部啟動replace
模板是通過xsl:apply-templates
。 這里我們還展示了一個模板可以同時具有match
和name
屬性,並且可以使用xsl:apply-templates
和xsl:call-template
來啟動這樣的模板是可能的。
它用於傳遞在另一個模板中定義的參數:
<xsl:param name="globalParam"></xsl:param>
<xsl:call-template name="ABC">
<xsl:with-param name="title" select="'A Title'" />
</xsl:call-template>
<xsl:template name="ABC">
<xsl:param name="title"/>
<xsl:value-of select="$title" />
<xsl:value-of select="$globalParam" />
</xsl:template>
我有以下使用 xsl-template 的代碼,如下所示:
<xsl:template name="closureRecords">
<xsl:param name="recordStackToBeGeneratedLocal" /> <!-- it is java stack contains list of values which i want to procees it-->
<xsl:if test="not(stack:empty($recordStackToBeGeneratedLocal))">
<xsl:variable name="dummstack" select="stack:push($recordStackToBeGeneratedLocal, 'helpppppp')"/>
pop recordStackToBeGenerated : <xsl:value-of select="stack:pop($recordStackToBeGeneratedLocal)"/> --> does not peek the stack .. prints all values
pop1 recordStackToBeGenerated : <xsl:value-of select="stack:peek($recordStackToBeGeneratedLocal)"/> --> does not peek the stack .. prints all values
recordStackToBeGenerated : <xsl:value-of select="(stack:empty($recordStackToBeGeneratedLocal))"/> --> false
</xsl:if>
</xsl:模板>
請讓我知道為什么 push/pop/peek 操作沒有做任何事情。 讓我知道我是否必須使用模板參數並使用不同的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.