跳轉到內容

Umbraco/Umbraco 中各種有用的 XSLT 方法

來自華夏公益教科書

在 Umbraco 中使用 XSLT 的基礎知識

[編輯 | 編輯原始碼]

建立變數

<xsl:variable name="myVarName" select="[select-expression/value]"/>

變數在此之後使用 $myVarName(以 $ 為字首)引用。

輸出值

<xsl:value-of select="$myVarName" />

表示式

轉換為字串

string($myVarName)

轉換為數字

number($myVarName)

如果測試

<xsl:if test="$value=3">
</xsl:if>

選擇測試

<xsl:choose>
	<xsl:when test="[expression]">
	</xsl:when>
	<xsl:when test="[expression]">
	</xsl:when>
	<xsl:otherwise>
	</xsl:otherwise>
</xsl:choose>

迴圈遍歷特定文件型別的所有節點(從根節點向下)(此示例包括對該文件型別中的欄位進行排序)

<xsl:for-each select="$currentPage/ancestor-or-self::root//node [@nodeTypeAlias='myDocType']">
   <xsl:sort select="data [@alias = 'myAttributeAlias']" order="descending"/>
</xsl:for-each>

從特定文件型別的第一個父節點獲取節點名稱

<xsl:variable name="myVarName" select="ancestor-or-self::node [@nodeTypeAlias = 'myDocType']/@nodeName"/>

(自 Umbraco 4.5 架構以來,不再使用 @nodeTypeAlias) - 對於 4.5 及更高版本,請使用

<xsl:variable name="myVarName" select="$currentPage/ancestor-or-self::doc-type-alias-goes-here/@nodeName"></xsl:variable>

從使用 @nodeTypeAlias 更改的詳細資訊:http://our.umbraco.org/wiki/reference/xslt/45-xml-schema/no-more-@nodetypealias

從具有特定文件型別的第一個父節點獲取屬性值

<xsl:variable name="myVarName" select="ancestor-or-self::node [@nodeTypeAlias = 'myDocType']/data [@alias = 'myAttributeAlias']"/>

將 for-each 迴圈限制為僅執行五次(如果 position <= 5)(記住,在 XSLT 中,'<' 符號必須寫為 '&lt;'  !) 

<xsl:for-each select="$currentPage/descendant::node">
<xsl:if test="position() &lt;= 5">
</xsl:if>
</xsl:for-each>

迴圈當前節點的前五個後代。

從迴圈中的當前節點獲取屬性值

<xsl:value-of select="data [@alias = 'myAttributeAlias']"/>

建立具有兩個引數的模板

<xsl:template name="myTemplate">
	<xsl:param name="startnode"/>
	<xsl:param name="endnode"/>
	<!-- do something-->
</xsl:template>

呼叫模板

<xsl:call-template name="myTemplate">
	 <xsl:with-param name="startnode" select="23"/>
	 <xsl:with-param name="endnode" select="49"/>
</xsl:call-template>

從宏獲取引數

<xsl:variable name="myParam" select="/macro/parameterAlias"/>
華夏公益教科書