2009-11-05 25 views
3

我是Umbraco CMS的新手。請幫忙。如何從父文檔訪問屬性以在兒童中使用

我有一個Umbraco網站,我在其中創建了一個名爲'Master'的DocumentType。 '主'頁面允許用戶輸入他們正在進行的募集資金的目標和金額。在「主」頁面上,我有一個宏,它會自動執行數學運算以生成將在整個網站中使用的百分比。宏調用以下XSLT

<xsl:for-each select="$currentPage/ancestor-or-self::*"> 
    <xsl:variable name="amount" select="* [@alias = 'FundraisingCurrentAmount']"/> 
    <xsl:variable name="goal" select="* [@alias = 'FundraisingGoal']"/> 
    <xsl:variable name="percentage" select="$amount div $goal * 100"/> 
    <xsl:value-of select="$percentage"/> 
</xsl:for-each> 

這工作,但,我假設,因爲它是一個「的for-each」它也返回2 NaN的結果。我怎樣才能改寫這個(a)清潔劑和(b)使它更好地工作。

我明白ASP.NET Webforms,所以如果你能比較它會幫助。

欣賞幫助。

回答

4

在Umbraco中,您可以擁有所謂的遞歸值。這基本上是查找節點層次結構直到它填充值的頁面值。

這些也可以傳遞給宏。

所以你的情況假設您的宏被稱爲「charityTotaliser」你可以使用下面的宏調用:

<umbraco:macro alias="charityTotaliser" ammount="[$FundraisingCurrentAmount]" goal="[$FundraisingGoal]"runat="server"/> 

的$表示該值是遞歸的。

的XSLT會是這個樣子(未測試只是一個例子):

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]> 
<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxml="urn:schemas-microsoft-com:xslt" 
    xmlns:umbraco.library="urn:umbraco.library" 
    xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" 
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltMath"> 

    <xsl:output method="xml" omit-xml-declaration="yes"/> 

    <xsl:param name="currentPage"/> 

    <!-- Macro parameters --> 
    <xsl:variable name="FundraisingCurrentAmount" select="/macro/FundraisingCurrentAmount"/> 
    <xsl:variable name="FundraisingGoal" select="/macro/FundraisingGoal"/> 

    <xsl:template match="/"> 

     <xsl:value-of select="$FundraisingCurrentAmount div $FundraisingGoal * 100"/> 

    </xsl:template> 

</xsl:stylesheet> 

如果科目編號,您還可以指定要傳遞迴退值(如果遞歸值無法找到):

<umbraco:macro alias="charityTotaliser" ammount="[$FundraisingCurrentAmount], [#FallBackAmmount], 1234" goal="[$FundraisingGoal]"runat="server"/> 

有關宏觀參數的更多信息,你可以看到這篇documentation

1

我只熟悉XSLT,所以從這個角度來說,我建議爲您的for-each select語句添加一個限定條件。我還沒有看到XML,但類似:

<xsl:for-each select="$currentPage/ancestor-or-self::*[FundraisingGoal>0]"> 
. 
. 
. 
</xsl:for-each> 

它應該只通過那些目標數量可以除以。

不知道這是你以後,但我希望這有助於。

+0

我知道這不是*嚴格要求*在這種情況下,但我會選擇使用'>'代替'>',不僅因爲後者打破了堆棧溢出語法突出顯示器。 – Tomalak

+0

同意。我通常首先輸入它>代替>,然後想知道爲什麼XSLT無效。 :) – dredful