2012-09-21 20 views
5

我在XSLT的值,我需要把它放到p標籤的數據時屬性一把umbraco - XSLT變量爲數據屬性

<xsl:value-of select="current()/eventTime" /> 
<p class="time" data-time="1">Duration: <xsl:value-of select="current()/eventTime" /> hour(s)</p> 

這將創建一個錯誤

<p class="time" data-time="<xsl:value-of select="current()/eventTime" />">Duration: <xsl:value-of select="current()/eventTime" /> hour(s)</p> 

任何想法如何實現這一目標?

回答

15

「屬性值模板」在這裏

<p class="time" data-time="{current()/eventTime}"> 
    Duration: <xsl:value-of select="current()/eventTime" /> hour(s) 
</p> 

大括號表示這是一個屬性值模板,因此包含一個表達式來評估你的朋友。

注意的另一種方法是使用XSL :屬性元素

<p class="time"> 
    <xsl:attribute name="data-time"> 
     <xsl:value-of select="current()/eventTime" /> 
    </xsl:attribute> 
    Duration: <xsl:value-of select="current()/eventTime" /> hour(s) 
</p> 

這不是那麼優雅,但。如果需要動態屬性名稱,您只需要這樣做。

+0

可愛優雅的解決方案謝謝。 – LeBlaireau

0

是這樣的嗎?

<xsl:variable name="eventtime" select="current()/eventTime"/> 

<xsl:element name="p"> 
    <xsl:attribute name="class">time</xsl:attribute> 
    <xsl:attribute name="data-time"> 
    <xsl:value-of select="$eventtime" /> 
    </xsl:attribute> 
    Duration: 
    <xsl:value-of select="$eventtime" /> 
</xsl:element>