2014-02-07 45 views
2

我想基於主題參數即時替換我的靜態主題中的class標記屬性。如何將Diazo主題參數插入某個主題的類屬性?

我嘗試這樣做:

<replace attributes="class" css:theme=".conteudo">conteudo-$section</replace> 

這:

<replace css:theme=".conteudo"> 
    <xsl:attribute name="class">conteudo-$section</xsl:attribute> 
    <xsl:value-of select="."/> 
</replace> 

即使這樣:

<xsl:template match="//div[contains(concat(' ', normalize-space(@class), ' '), ' conteudo ')]"> 
    <xsl:attribute name="class"> 
     <xsl:value-of select="substring((body/@class), 'section-', 0)" /> 
    </xsl:attribute> 
</xsl:template> 

因爲我也有其他規則引用.conteudo元素,它會是也很好地瞭解如何處理這些問題的最佳實踐(在所需的轉換occ之後) (即:

<replace 
    css:content-children="#portal-column-content" 
    css:theme-children=".conteudo" /> 
+0

順便說一句,在'manifest.cfg':部分=蟒蛇: 「─」 + context.getPhysicalPath()[LEN(門戶.getPhysicalPath()):] [0] –

+0

爲了避免在爲資源請求(圖像,CSS,js)計算getPhysicalPath時出現AttributeError,我將manifest.cfg更改爲'section = python:not context_state.is_portal_root()和context_state.portlet_assignable )和context.getPhysicalPath()[len(portal.getPhysicalPath()):] [0]' –

+0

條件的第一部分是在上下文爲站點根時避免IndexError。 –

回答

1

您不能在任何地方引用變量,但需要從XPath表達式中進行引用。

您可以通過在孩子之前插入屬性來避免干擾替換孩子節點。

這裏是我想嘗試:

<before css:theme-children=".conteudo"> 
    <xsl:attribute name="class">conteudo-<xsl:value-of select="$section" /></xsl:attribute> 
</before> 
+1

我把我的規則減少到了答案中的規則。即使如此,它仍然給我這個錯誤:*運行時錯誤,元素'屬性'[77:0] * xsl:attribute:如果子元素已經添加到元素,則不能向元素添加屬性。 [0:0] –

+0

完整的錯誤信息:http://pastebin.com/ts6kZ1Ep –

1

正如大衛說,就是做正確的方式。 而子元素之前設置的屬性,你要做的是:

<xsl:template match="//*[contains(@class, 'conteudo')]"> 
    <xsl:copy> 
     <xsl:attribute name="class"><xsl:value-of select="$section" /></xsl:attribute> 
     <xsl:apply-templates select="@*[not(name()='class')]|node()" /> 
    </xsl:copy> 
</xsl:template> 

<!--Identity template copies content forward --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 
+0

我得到了一個'Invalid expression [0:0]'錯誤。詳細信息: '* compilation error,element'apply-templates'[242:0] * XSLT-apply-templates:無法編譯選擇表達式'@ * [not(name()='class'] | node )'[0:0]'完整的錯誤信息:http://pastebin.com/7DLP99xa –

+0

缺少括號:@ * [not(name()='class')] | node()。 – ebrehault

+0

無效的表達式錯誤消失了,但它仍然不起作用。我是否必須將這一點與David的建議結合起來?我試過了,但它失敗了,出現了同樣的錯誤:'如果已經添加了子元素,則無法向元素添加屬性元素' –