Umbraco - xslt variable into data attribute
Asked Answered
T

3

9

I have a value in xslt and I need to put it into the data-time attribute of the p tag

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

this creates an error

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

any idea how I achieve this?

Tier answered 21/9, 2012 at 11:13 Comment(0)
R
23

"Attribute Value Templates" are your friend here

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

The curly braces indicate that this is an Attribute Value Template, and so contains an expression to be evaluated.

Note that an alternate way would be to use the xsl:attribute element

<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> 

This is not so elegant though. You would only really need to do it this way if wanted a dynamic attribute name.

Royalty answered 21/9, 2012 at 11:25 Comment(0)
D
0

Something like this?

<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>
Declivitous answered 21/9, 2012 at 11:36 Comment(0)
M
0

instead of <xsl:attribute> it's also possible to use the short form in '{}' brackets. In your case it would be like this:

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

Monday answered 23/5, 2019 at 9:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.