Umbraco - xslt variable in data attribute

I have a value in xslt and I need to put it in 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?

+6
source share
3 answers

"Attribute Value Templates" is here your friend

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

In curly brackets, it is indicated that this is an attribute value template, and therefore contains an expression for evaluation.

Note that an alternative 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> 

It is not so elegant. You just need to do it this way if you wanted the name of a dynamic attribute.

+16
source

Something like that?

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

<xsl:attribute> ' {} '.

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

0

All Articles